Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Franz Wong
Franz Wong

Posted on • Edited on

Setup kdb+tick with Java feed handler

[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.

Alt Text

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 cloneKxSystems/kdb+tick.

# Execute this in 'kdb-tick-java-fh-example' foldergit clone https://github.com/KxSystems/kdb-tick.git

We will add schema filesym.q.

# Execute this in 'kdb-tick' foldertouchtick/sym.q

Here is the content ofsym.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.

Alt Text

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' folderq tick.q sym$(pwd)/OnDiskDB-p 5000

Start RDB

# Execute this in 'kdb-tick' folderq 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 filec.java.

# Execute this in 'kdb-tick-java-fh-example' foldergit 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 handlermkdir-p feed-handler-java/src/main/java/kxcpjavakdb/src/kx/c.java feed-handler-java/src/main/java/kx# Create feed handlermkdir-p feed-handler-java/src/main/java/fhtouchfeed-handler-java/src/main/java/fh/FeedHandler.java

Here is content ofFeedHandler.java

packagefh;importkx.c;publicclassFeedHandler{publicstaticvoidmain(String[]args)throwsException{cc=null;try{System.out.println("Try to connect to tickerplant");c=newc("localhost",5000);System.out.println("Connected to tickerplant");Object[]row={newc.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(Exceptione){throwe;}finally{c.close();}}}

This is directory structure of 'feed-handler-java' folder.

Alt Text

We can build our feed handler.

# Execute this in 'feed-handler-java' folderjavac\-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' folderjava-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 inputexit 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.

References

Building real-time tick subscribers
Using Java with kdb+

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Hong Kong
  • Joined

More fromFranz Wong

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp