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

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

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.

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:

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

FeedDemo.java

FeedHandler.java