首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用PyQ和Python连接到KDB服务器?

如何使用PyQ和Python连接到KDB服务器?
EN

Stack Overflow用户
提问于 2020-02-14 20:16:18
回答 1查看 1.1K关注 0票数 0

我使用的是Python2.7,我已经在Debian10下安装了PyQ,Q (x64版本)设置正确。

问题是如何连接到KDB服务器(我有凭据(IP、端口、用户和密码))?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-14 20:45:44

启动pyq会话,切换到Q解释器,然后使用hopen

代码语言:javascript
运行
复制
Using the q interpreter in PyQ:
>>> q()
q)h:hopen `:localhost:1234 // `:host:port:user:pass
q)h"2+2"
q)4

编辑-来自Python和Creating A Pandas.DataFrame的进一步示例:

代码语言:javascript
运行
复制
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

使用qPython:

代码语言:javascript
运行
复制
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()

有关如何从表中选择特定数据的信息,请参阅qSQL。kdb中的表可能非常大,选择整个表可能是不明智的。情商。

代码语言:javascript
运行
复制
PyQ:
tbl2 = q("h\"select from trades where date = 2020.02.14\"")
qPython:
df = q.sendSync('select from trades where date = 2020.02.14')

你打算在q中做任何数据处理客户端吗?如果只想从kdb服务器获取数据以便与python一起使用,qPython可能是更好的选择。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60226208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档