1

I am using Python 2.7 and I have installed PyQ, with q (x64 version) set up correctly, under Debian 10.

The question is how to connect to a KDB server (I have the credentials (IP, port, user and password))?

Matthew
  • 67
  • 2
  • 11
  • 1
    Hey Mathew, please include what you have tried so far in your question. – karuhanga Feb 14 '20 at 12:25
  • I was thinking to use socket instruction, but I am not familiar with q (and unfortunately I could not find suitable examples/tutoriasl), and I preferred to ask. – Matthew Feb 14 '20 at 13:29

1 Answers1

2

Start a pyq session, switch to the q interpreter then use hopen

Using the q interpreter in PyQ:
>>> q()
q)h:hopen `:localhost:1234 // `:host:port:user:pass
q)h"2+2"
q)4

Edit - Further example from within Python and Creating A Pandas.DataFrame:

I have the following table defined on my q server process:

q)tbl:([]col1:`a`b`c;col2:til 3)
q)tbl
col1 col2
---------
a    0   
b    1   
c    2 

Then from my client PyQ interpreter:

from pyq import q
import numpy as np # Numpy is needed as a middle man for interpreting kdb objects to python.
import pandas as pd
import datetime # if your kdb table has dates and times

q("h:hopen `:localhost:1234")
tbl2 = q("h\"tbl\"") # need to escape the quotes around tbl
tblNP = np.array(tbl2)
df = pd.DataFrame(tblNP)
df
  col1  col2
0    a     0
1    b     1
2    c     2

Using qPython:

from qpython import qconnection
import pandas as pd

if __name__ == '__main__':
    # create connection object
    q = qconnection.QConnection(host='localhost', port=1234, pandas=True)
    # initialize connection
    q.open()

    # simple query execution via: QConnection.sendSync
    df = q.sendSync('tbl')
    # close connection
    q.close()

See qSQL for how to select specific data from your tables. Tables in kdb can be very large and may be unwise to select the entire thing. e.q.

PyQ:
tbl2 = q("h\"select from trades where date = 2020.02.14\"")
qPython:
df = q.sendSync('select from trades where date = 2020.02.14')

Are you planning on doing any data processing client side in q? If only looking to get data from the kdb server for use with python, qPython may be a better option.

Matt Moore
  • 2,705
  • 6
  • 13
  • Thank you for your answer. I am able to proceed until `q)h:hopen `:localhost:1234 // `:host:port:user:pass` but then at `q)h"show `test"` there is no reply from the server. Also, if I put the query there, the server does not return anything. Yes, I want to get some data from the kdb server and process the data locally at the client. – Matthew Feb 14 '20 at 13:30
  • The problem with qPython is the same: I do not know how to structure the request to the kdb server with qPython. – Matthew Feb 14 '20 at 13:34
  • Well, I tested again the query (obviously instead of test), but the server returned `'lenght` statement, instead of some kind of data. – Matthew Feb 14 '20 at 13:40
  • 1
    Hi sorry, just realised my example wasn't the best. Try h"2+2" and you should see 4 returned to the client – Matt Moore Feb 14 '20 at 13:42
  • Great, it helped. What is distracting me is the need to use the q language to address the server. How to put the server answer in a Python variable for processing in Python? – Matthew Feb 14 '20 at 13:53
  • See my update above, I really think for your use case, qPython is a better option. PyQ is more geared towards kdb devs who will prefer to maximise the use of kdb in the interpreter. qPython would be better if all you want is kdb server data -> python variable. If your new to q, both will be difficult in terms of the kdb side but that's what stackoverflow is for. Just tag any further questions appropriately and you will get the help you need. – Matt Moore Feb 14 '20 at 15:10
  • @MattMoore Is it possible to import methods written in PyQ from any other Python code? Since PyQ runs using a different interpreter (not Python). – Chankey Pathak May 13 '21 at 12:32
  • Hi, I'm not sure I understand the question. I come from a primarily kdb/q background with basic python understanding – Matt Moore May 13 '21 at 19:34
  • you should be able to import any python code you want and use it in the PyQ interpreter provided it is compatible with the python version – Matt Moore May 13 '21 at 19:46
  • @MattMoore No, my question is that if I write a module using PyQ then how can I make sure that this module is importable by my users that don't use PyQ but Python interpreter. – Chankey Pathak May 14 '21 at 01:03
  • Honestly not sure, I imagine if you are relying on q code then it won't work in the python interpreter – Matt Moore May 21 '21 at 09:39