QStudio
Query kdb+ Servers and Chart Results.
Examples of querying, subscribing and a java feedhandler are available to download and are detailed below:
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)"
The Interfacing kdb with Java page on the code KX website documents the API. Useful snippets include:
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 |
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. |
See the TableQueryExample.java in the java example code. This is an almost a direct copy from code.kx.com. The steps are:
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);
See the SubscriberExample.java in the java example code. This demonstrates connecting a tickerplant and subscribing to the trade table. The steps are:
1
2
3
4
5
6
c con = new c(host, port);
con.k(".u.sub[`trade;`]");
while (true) {
try {
Object r = con.k();
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:
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.
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);
}
}
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 {
Query kdb+ Servers and Chart Results.