Skip to content

KdbQuery

Jas edited this page Mar 14, 2017 · 1 revision

KdbSyncQuery and KdbAsyncQuery

These classes allow you to run queries on a kdb+ process either synchronously or asynchronously as required.

The asynchronous query class does an asynchronous call and then blocks for the result. We are looking at adding support to return a Future.

Other supported features:

  • Auto reconnect on query if disconnected
  • try-with-resources
  • Execution time logging

Usage

There are 2 ways to construct a query object:

  • KdbQuery(KdbProcess): The query object itself will create a new connection to the specified process
  • KdbQuery(KdbConnection): The query object uses the existing specified connection to perform the query. Note that connections should not be shared between multiple threads

There are 2 query functions which are supported:

  • query(String): Complete function call as a string
  • query(String, KdbDict): Function call with single dictionary argument (generally used when querying Gateways)

The query functions either return an Object or throw QueryExectionFailedException if either a kdb exception or a low level I/O exception occurs during the query.

If you know the returned object will be a dictionary or table, you can use the following conversion functions:

  • KdbDict.fromObject: For Dict results
  • KdbTable.fromObject: For Table results

Examples

Simple Function

public void simpleQuery() throws QueryExecutionFailedException() {
    KdbQuery query = new KdbSyncQuery(new KdbProcess("localhost", 1234));
    Object result = query(".z.d");
    
    System.out.println("kdb time: " + result);
    query.close();
}

Function with Argument

public KdbTable queryWithArgs throws QueryExecutionFailedException() {
    KdbQuery query = new KdbSyncQuery(new KdbProcess("localhost", 1234));
    KdbDict args = new KdbDict()
                             .add("sym", "VOD.L");

    Object result = query("getTodaysTradesFor", args);
    query.close();

    return KdbTable.fromObject(result);
}

Try-with-resources

public KdbTable queryWithArgsAndTry() {
    KdbDict args = new KdbDict()
                             .add("sym", "VOD.L");

    try(KdbQuery query = new KdbSyncQuery(new KdbProcess("localhost", 1234))) {
        Object result = query("getTodaysTradesFor", args);
        return KdbTable.fromObject(result);
    } catch (QueryExecutionFailedException e) {
        return null;
    }
}
Clone this wiki locally