Examples of querying, subscribing and a java feedhandler are available to download and are detailed below:

Download Example Java Code

Example of kdb Java API

Commands to run

java -cp kdb-java-api-examples.jar com.timestored.kdb.examples.TableQueryExample "([] a:til 10; b:10?100)"
 1
2
3
4
5
6
 
# query a table example 
# assumes kdb server on localhost port 5001
wget -q http://www.timestored.com/kdb-guides/kdb-java-api-examples.jar
java -cp kdb-java-api-examples.jar com.timestored.kdb.examples.TableQueryExample
java -cp kdb-java-api-examples.jar com.timestored.kdb.examples.TableQueryExample "([] a:til 10; b:10?100)"
 

Useful Code KX Information

The Interfacing kdb with Java page on the code KX website documents the API. Useful snippets include:

Connecting

Signature Notes
public c(String host, int port, String usernameAndPassword) throws KException, IOException Throws a KException if access is denied by the kdb+ server. The username and password should be of the format "username:password"
public c(String host, int port) throws KException, IOException Uses the user.name property as the login name and password. It throws a KException if access is denied by the kdb server

Method Calls

Comm Type Method Description
Asynchronous public void ks(String s) throws IOException Send string query.
public void ks(Object x) throws IOException Send K object.
Synchronous public Object k(String s) throws KException, IOException Send string query, wait and return response.
public Object k(Object x) throws KException, IOException Send K object, wait and return response
public Object k() throws KException, IOException Retrieve waiting messages without sending a query.

Querying

See the TableQueryExample.java in the java example code. This is an almost a direct copy from code.kx.com. The steps are:

  1. Connect
  2. Send query - either string or K object
  3. Cast the returned result then parse
    String query="([]date:.z.D;time:.z.T;sym:n?`8;price:`float$n?500.0;size:n?100;r:til (n:100))";
 1
2
3
4
5
6
 
c.Flip tableResult = null;
c c = null;
try {
    c = new c("localhost", 5001,"username:password");
    String query="([]date:.z.D;time:.z.T;sym:n?`8;price:`float$n?500.0;size:n?100;r:til (n:100))";
    tableResult = (c.Flip) c.k(query);

Subscribing to Kdb tickerplant

See the SubscriberExample.java in the java example code. This demonstrates connecting a tickerplant and subscribing to the trade table. The steps are:

  1. Connect
  2. Subscribe to all syms on the trade table
  3. loop forever, calling k() to receive incoming data.
    Parse and print the first line of each table update.
                s += " " + colNames[i] + ":" + c.at(colData[i], 0).toString();
 1
2
3
4
5
6
 
c con = new c(host, port);
con.k(".u.sub[`trade;`]"); 
 
while (true) {
    try {
        Object r = con.k();

Feedhandler - sending data to tickerplant

Demonstrate creating feedhandler, making it listen to incoming data and forwarding to the kdb server. Before running start a fresh kdb server on port 5000 and enter the below:

trade:([]time:`time$();sym:`symbol$();price:`float$();size:`int$();stop:`boolean$();cond:`char$();ex:`char$())
1
2
3
4
 
trade:([]time:`time$();sym:`symbol$();price:`float$();size:`int$();stop:`boolean$();cond:`char$();ex:`char$())
.u.upd:insert; / Allows using the same commands as a kdb tickerplant.
 
        

Create our feed handler that forwards data to a q process on localhost port 5000 and attach to a fake feed.

FeedDemo.java

    public static void main(String... args) throws KException, IOException {
1
2
3
4
5
6
 
public class FeedDemo {
    public static void main(String... args) throws KException, IOException {
            FeedHandler feedHandler = new FeedHandler("localhost", 5001);
            FakeFeed.INSTANCE.addListener(feedHandler);
    }
}

FeedHandler.java

 * Implements {@link FeedListener} so can listen to feeds which it will then
 1
2
3
4
5
6
 
/**
 * Implements {@link FeedListener} so can listen to feeds which it will then
 * parse to a K object and forward to a kdb server.
 */
public class FeedHandler implements FeedListener {