Search code examples
pythonpython-3.xkdbpyq

Open connection handle and update kdb table with hopen in PyQ


I'd like to use PyQ to parse a json object and then update a keyed kdb table running on a different port by pushing the parsed data through an open handle to the keyed table.

To open a handle and update the kdb table in q would require the following steps...

On the server; open the port, define a table and a function

q)\p 5000
q)t:([keycol:`aa`bb`cc]col2:10 20 30; col3: 1.1 2.2 3.3)
q)f:{[x;y]update col2: y from t where keycol=x}
q)t
keycol| col2 col3
------| ---------
aa    | 10   1.1
bb    | 20   2.2
cc    | 30   3.3

On the client; open the connection handle, call the function, close the connection handle...

q)h:hopen `::5000
q)h (`f; `aa; 99)
keycol| col2 col3
------| ---------
aa    | 99   1.1
bb    | 20   2.2
cc    | 30   3.3
q)hclose h

What is the correct syntax to perform this operation in PyQ?


Solution

  • Your client commands can be translated to PyQ as follows:

    >>> h = q.hopen('::5000')
    >>> h(('f','aa',99)).show()
    keycol| col2 col3
    ------| ---------
    aa    | 99   1.1
    bb    | 20   2.2
    cc    | 30   3.3
    >>> h.hclose()
    k('::')
    

    Note that this will not update the table on the server unless you change f to use `t instead of t.