Kdb C API Query Subscribe Example
Examples of connecting, querying and subscribing using the C API for linux are available to download and are detailed below:
Contents
Useful Code KX Information
We will use the two methods shown below to connect and query kdb:
The Interfacing Kdb with C page on the code KX website details the full API specificiation.
Method Calls
Signature | Notes |
---|---|
int c = khpu("localhost", 1234,"myusername:mypassword"); | Connect to a kdb server on the localhost port 1234. |
result=k(handle,"([]a:til 10;b:reverse til 10;c:10?`4)",(K)0) | Send a synchronous message in string format to a server and return the result. |
Querying 2.6/2.8 kdb from C in Linux
Code KX has a simple example of querying that relies on a q server to be running on port 12001.
To compile and run the example for kdb < version 3, do the following in linux:
-lpthread
- links in the posix threads library that is needed
-m64
- generates code for a 64 bit environment, setting int to 32 bits and long and pointer types to 64 bits
The svn uses -r as you need the old revisions of the library/header, these can also be downloaded
qc-eg.zip and here
Querying 3.0 kdb from C in Linux
The main difference with kdb version 3.0+ is that we must make sure to specify the environment variable KXVER.
Kdb must be running on the local machine port 12001.
C Connect / Query Code
The major steps involved in kx's example csv.c to connect and query a kdb server are:
- Connect to kdb server.
khpu(host, port, "user:pass")
- Send query synchronously.
result=k(handle,"select...",(K)0)
- Close handle.
kclose(handle)
- Check the result type then loop through rows/columns. using
K
.