Reputation: 77
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))?
Upvotes: 1
Views: 4396
Reputation: 2775
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.
Upvotes: 2