From Kx Wiki
Jump to: navigation, search

The wiki is moving to a new format and this page is no longer maintained. You can find the new page at

The wiki will remain in place until the migration is complete. If you prefer the wiki to the new format, please tell the Librarian why.

A recommended alternative to this page is Cookbook/IPCInANutshell

Basic client-server computing with kdb+

A kdb+ server can listen for connections on a port. Clients can then send requests to the server via that port.

A kdb+ process starts listening to a port either at startup, via a command-line argument

q -p 5001

or at a later time, using the command \p

\p 5001

To stop listening, you can ask the server to listen on port zero, like this

\p 0

Clients can be other kdb+ processes, or they can be written in C, Java, C#, etc. This is an example of a Java client:

public class KDBClient {

    public static void main(String[] args) {
	    // create kdb+ server object
	    c kdbServer = new c("localhost",5001);
	    // create a row (array of objects)
	    Object[] row= {new Time(System.currentTimeMillis()%86400000), "IBM", new Double(93.5), new Integer(300)};
	    // insert the row into the trade table
            kdbServer.ks("insert","trade", row);
	    // send a sync message (see below for an explanation)
	    // execute a query in the server that returns a table
	    Flip table = td(kdbServer.k("select sum size by sym from trade"));
	    // read the data from the Flip object ...
	    // close connection to kdb+ server
	} catch (Exception e) {

This client does not need a reply after the insert, so it sends an asynchronous message using method ks. For the select, it expects a table as a result, and sends a synchronous message using method k.

Information.png The parameters to methods k and ks, and its result are arbitrarily nested arrays (Integer, Double, int[], !DateTime, etc).

More information on interfacing kdb+ to other languages can be found in the cookbook.

A kdb+ client process connects to a server using hopen:

h: hopen `:localhost:5001

The syntax of the argument to hopen is


where 'host' is the hostname of the server. In this example, the client is in the same machine as the server, so we use 'localhost'.

We have assigned the handle of the connection to the variable h. To close the connection, write

hclose h

From a kdb+ client, a synchronous message is sent simply like this:

h "select sum size by sym from trade"
sym  | amount
-----| ---------
amd  | 324928400
amzn | 326589900
goog | 324356900
ibm  | 324553500
intel| 324721900
msft | 324377400

To send an asynchronous message, use the handle value, negated

(neg h) "insert[`trade](10:30:01.000; `intel; 88.5; 1625)"

Execution of this message returns immediately with no result.

Messages can also be created as lists instead of strings. For instance:

(neg h) (insert; `trade; (10:30:01.000; `intel; 88.5; 1625))

In most realistic situations the data to be inserted is not constant, but is either generated algorithmically or received from an external source. Consequently, the list message format is the more generally useful one because it does not require formatting the data into strings.

If you want to send a bunch of async messages, and then wait for them to complete processing, you can chase them with

h ""

which will cause the client block until the server sends a null reply to that message.

Personal tools