Skip to content

Latest commit

 

History

History
65 lines (51 loc) · 1.65 KB

README.md

File metadata and controls

65 lines (51 loc) · 1.65 KB

ksql-math-udf

UDF which implements basic math functions for ksql

Inspiration for this library was this github issue

Usage

Getting the jar

  1. Either download from releases
wget https://github.com/tokenanalyst/ksql-math-udf/releases/download/basic-math/ta-math-udfs.jar
  1. Bulding from source
$ # get source code
$ git clone https://github.com/tokenanalyst/ksql-math-udf.git
$ # install sbt (https://www.scala-sbt.org/1.0/docs/Setup.html)
$ sudo apt-get install agt
$ # build the binary
$ sbt assembly

Using the JAR in ksql

  1. Create a directory for UDF jar's and put the jar file there
$ mkdir \var\lib\ksql-ext\
$ mv ta-math-udfs.jar \var\lib\ksql-ext\
  1. Update the location in ksqlserver.properties
$ echo ksql.extension.dir=/var/lib/ksql-ext >> /etc/ksql/ksql-server.properties
  1. Restart KSQL server
$ systemctl restart confluent-ksql
  1. Start ksql and show functions to see if the new functions have been added
$ ksql
ksql> show functions;

 Function Name           | Type      
-------------------------------------
 SQUARE                  | SCALAR    
 POW                     | SCALAR    
 SQRT                    | SCALAR  
  1. The functions are now ready to use
ksql> SELECT key, sqrt(value) as sqrt_value from STREAM_NAME;
ksql> CREATE table MEAN_TABLE as SELECT sum(value)/count() as mean, key from STREAM_NAME group by key; 
ksql> SELECT STREAM_NAME.key, sqrt(sum(square(value - mean))/count()) as stdev_value \
      from STREAM_NAME left join MEAN_TABLE \
      STREAM_NAME.key = MEAN_TABLE.key \
      group by key;