[I started learning kdb few weeks ago, so I would like to ]
kdb+tick is used to capture the ticks and store them. The ticks are provided by the feed handler.
Here is the architecture diagram from Kx.
We will setup a simple kdb+tick first and then we will create our feed handler in Java. We assume you have already installed kdb+.
Let's create our workspace.
mkdir kdb-tick-java-fh-example
Setup kdb+tick
Next, we need to clone KxSystems/kdb+tick.
# Execute this in 'kdb-tick-java-fh-example' folder
git clone https://github.com/KxSystems/kdb-tick.git
We will add schema file sym.q
.
# Execute this in 'kdb-tick' folder
touch tick/sym.q
Here is the content of sym.q
.
quote:([]time:`timespan$();sym:`symbol$();bid:`float$();ask:`float$();bsize:`int$();asize:`int$())
trade:([]time:`timespan$();sym:`symbol$();price:`float$();size:`int$())
This is directory structure of 'kdb-tick' folder.
We need to start both tickerplant and RDB. We will keep the processes in foreground for this tutorial, so you need 2 terminals to hold them. In production, you should send them to background.
Start tickerplant
# Execute this in 'kdb-tick' folder
q tick.q sym $(pwd)/OnDiskDB -p 5000
Start RDB
# Execute this in 'kdb-tick' folder
q tick/r.q localhost:5000 localhost:5002 -p 5001
Create feed handler with Java
Before we create our feed handler, we need to get Java driver for kdb. I can't find that in any Maven repository, so we need to get it from Kx's Git repository. It is just a single file c.java
.
# Execute this in 'kdb-tick-java-fh-example' folder
git clone https://github.com/KxSystems/javakdb.git
Now we create our feed handler.
# Execute this in 'kdb-tick-java-fh-example' folder
# Copy c.java to our feed handler
mkdir -p feed-handler-java/src/main/java/kx
cp javakdb/src/kx/c.java feed-handler-java/src/main/java/kx
# Create feed handler
mkdir -p feed-handler-java/src/main/java/fh
touch feed-handler-java/src/main/java/fh/FeedHandler.java
Here is content of FeedHandler.java
package fh;
import kx.c;
public class FeedHandler {
public static void main(String[] args) throws Exception {
c c = null;
try {
System.out.println("Try to connect to tickerplant");
c = new c("localhost", 5000);
System.out.println("Connected to tickerplant");
Object[] row = { new c.Timespan(), "MSFT", 174.57, 300L };
System.out.println("Try to insert record to 'trade' table");
c.k(".u.upd", "trade", row);
System.out.println("Record inserted to 'trade' table");
} catch (Exception e) {
throw e;
} finally {
c.close();
}
}
}
This is directory structure of 'feed-handler-java' folder.
We can build our feed handler.
# Execute this in 'feed-handler-java' folder
javac \
-sourcepath "src/main/java" \
-d "target/classes" \
src/main/java/fh/FeedHandler.java
After our feed handler is built, we can execute it.
# Execute this in 'feed-handler-java' folder
java -cp "target/classes" fh.FeedHandler
We go back to the terminal of our running RDB to verify the result.
select from trade
You should see something like this.
time sym price size
-------------------------------------
0D10:58:46.733000000 MSFT 174.57 300
To exit q process, you can input exit 0
or simply \\
.
We have created our feed handler. You can check the reference links at the bottom to get more information about kdb+tick and Java driver for kdb.
Top comments (0)