Cookbook/IntegratingWithJ

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 pages at code.kx.com/q/interfaces/j-client-for-q/ and code.kx.com/q/interfaces/q-client-for-j/.

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.

Contents

JQ: J Client for Q

JQ is a socket interface to Q from J, for example to provide a GUI for a Q server. JQ was written in J602 for KDB+2.4, and has been tested on w32, l32 and l64.

A sample session is given below. For more information see:

Sample Session

Start a Q session, listening on a given port. Load J, then:

   load 'qclient.ijs'           NB. load JQ client application

Create a client instance, defining cover functions in z:

   1 conew 'cqclient'
+-+
|1|
+-+
   connect 'robot';5001;'elmo'  NB. connect to robot on port 5001 with user elmo

   cmd 'trade:([]time:();sym:();price:();size:())' NB. create trade database

Send a command string to insert a record:

   cmd '`trade insert(09:30:00;`a;10.75;100)'

   get 'trade' NB. get database
+-----+----+------+-----+
|`time|`sym|`price|`size|
+-----+----+------+-----+
|34200|`a  |10.75 |100  |
+-----+----+------+-----+

Get database in extended format. Each item is a pair: Qdatatype;value. The display shows that trade is the flip (type 98) of a dict (type 99). The dict is a pair: varchar, object list.

   getx 'trade'
+--+------------------------------------------------------------------------------+
|98|+--+-------------------------------------------------------------------------+|
|  ||99|+----------------------------+------------------------------------------+||
|  ||  ||+--+-----------------------+|+-+--------------------------------------+|||
|  ||  |||11|`time `sym `price `size|||0|+----------+-------+---------+-------+||||
|  ||  ||+--+-----------------------+|| ||+--+-----+|+--+--+|+-+-----+|+-+---+|||||
|  ||  ||                            || |||18|34200|||11|`a|||9|10.75|||6|100||||||
|  ||  ||                            || ||+--+-----+|+--+--+|+-+-----+|+-+---+|||||
|  ||  ||                            || |+----------+-------+---------+-------+||||
|  ||  ||                            |+-+--------------------------------------+|||
|  ||  |+----------------------------+------------------------------------------+||
|  |+--+-------------------------------------------------------------------------+|
+--+------------------------------------------------------------------------------+

Insert records using the insert function. The result is the record number:

   execr 'insert';(s:<'trade');<('09:31:12';(s:<'a');11.01;200)
1
   execr 'insert';(s:<'trade');<('09:31:15';(s:<'bc');10.80;150)
2

The next example has a numeric time field, and integer price field. Extended format ensures the correct datatypes are sent:

   execr 'insert';(s:<'trade');<((,:18;34301);(s:<'bc');(,:9;22);500)
3

The utility col, defined below, formats cells into columns:

   col=: ,.@:>L:0

   col getf 'trade'
+--------+----+------+-----+
|`time   |`sym|`price|`size|
+--------+----+------+-----+
|09:30:00|`a  |10.75 |100  |
|09:31:12|`a  |11.01 |200  |
|09:31:15|`bc | 10.8 |150  |
|09:31:41|`bc |   22 |500  |
+--------+----+------+-----+

The next result is displayed as a pair - key columns and data columns.

   col cmdr 'select sum size by sym from trade'
+------+-------+
|+----+|+-----+|
||`sym|||`size||
|+----+|+-----+|
||`a  |||300  ||
||`bc |||650  ||
|+----+|+-----+|
+------+-------+

Send command string to load the sp.q database:

   cmd '\l sp.q'

Query the sp.q database:

   col cmdr 'select sum qty by p.color from sp'
+--------+------+
|+------+|+----+|
||`color|||`qty||
|+------+|+----+|
||`blue ||| 900||
||`green|||1000||
||`red  |||1200||
|+------+|+----+|
+--------+------+

   col get 's'
+-----+------------------------+
|+---+|+------+-------+-------+|
||`s |||`name |`status|`city  ||
|+---+|+------+-------+-------+|
||`s1|||`smith|20     |`london||
||`s2|||`jones|10     |`paris ||
||`s3|||`blake|30     |`paris ||
||`s4|||`clark|20     |`london||
||`s5|||`adams|30     |`athens||
|+---+|+------+-------+-------+|
+-----+------------------------+

QJ: J Engine within Kdb+

QJ is a c extension for kdb+,working as a frontend of J engine dll,so we can use the J engine and the J Application Library within kdb+.It has been tested on l64 with j701beta.

Install

load 'pacman'
'update' jpkg ''
'install' jpkg 'all'

Function List

Datatype Map

Sample Usage

Start a Q session, then load qj.q

q)\l qj.q

Load J Runtime

q)initj["/opt/j64-701/bin"];

Set data to J

q)q2j["a";(01b;2 3 4j;5;1 0n 0w -0w -2f;"abcd")]

Get data from J

q)2jq["a"]
01b
2 3 4j
5j
1 0n 0w -0w -2
"abcd"

Eval J sentences:

q)jedo "i.2 3 4"
0 1 2  3    4 5 6  7    8 9 10 11
12 13 14 15 16 17 18 19 20 21 22 23

Call J monads:

q)jemv["q:"] 1234567890j
2 3 3 5 3607 3803j

Call J dyads:

q)jedv["e.";"cat";"abcd"]
110b

Use J plot library:

q)jedo "load 'plot'";
q)jemv["plot"] 100?1f;
q).q.plot:jedv["plot"];
q)"type bar" plot 10?1f;

Use J fftw library:

q)jedo "load 'math/fftw'";
q)jemv["fftw"]  til 8
28 -4       -4 -4       -4 -4        -4 -4
0  9.656854 4  1.656854 0  -1.656854 -4 -9.656854

Use J tara library to read excel files:

q)jedo "load 'tables/tara'";
q)\wget http://www.iso15022.org/MIC/ISO10383_MIC_v1_78.xls -O /tmp/10383.xls
q){flip (`$x[0])!flip 1_x} trim jedo "\":>readexcel '/tmp/10383.xls'"
COUNTRY     CC   MIC    INSTITUTION DESCRIPTION              ACR     CITY    ..
-----------------------------------------------------------------------------..
"ALBANIA"   "AL" "XTIR" "TIRANA STOCK EXCHANGE"              ""      "TIRANA"..
"ALGERIA"   "DZ" "XALG" "ALGIERS STOCK EXCHANGE"             ""      "ALGIERS..
"ARGENTINA" "AR" "BACE" "BUENOS AIRES CEREAL EXCHANGE"       ""      "BUENOS ..
..
Personal tools
Namespaces
Variants
Actions
Navigation
Print/export
Toolbox