PyQ - q.upsert 和 q.set 追加到展开的 kdb 数据库
PyQ - q.upsert and q.set append to splayed kdb database
当我在pyq中运行下面的代码...
from bittrex.bittrex import Bittrex, API_V2_0
import time, json
from pyq import q, K
get_bittrex = Bittrex(None, None)
starttime = time.time()
market_result = get_bittrex.get_market_summaries()['result']
while True:
for res in market_result:
market_name = res['MarketName']
ask = str(res['Ask'])
bid = str(res['Bid'])
last = str(res['Last'])
timeStamp = str(res['TimeStamp'])
if market_name in ['USDT-BTC', 'USDT-ETH', 'USDT-LTC']:
mkt = {"Name": market_name,"Bid": bid,"Ask": ask,"Time":timeStamp}
q.set(':alpha', [mkt])
q.upsert(':alpha', mkt)
q.get(':alpha').show()
time.sleep(10.0)
它 returns 每 10 秒到控制台...
Name Bid Ask Time
--------------------------------------------------------------
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
Name Bid Ask Time
------------------------------------------------------
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
Name Bid Ask Time
----------------------------------------------------------
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
Name Bid Ask Time
--------------------------------------------------------------
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
Name Bid Ask Time
------------------------------------------------------
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
Name Bid Ask Time
----------------------------------------------------------
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
为什么每行打印 2 次?此外,我如何修改它以便将每个 market_name
添加到相同的 table,即它当前正在将每个 market_name
打印到它自己的 table 而不是附加到现有的 table table.
理想的输出如下...
Name Bid Ask Time
--------------------------------------------------------------
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
你在循环中所做的相当于下面的q码:
q)`:alpha set enlist`a`b!1 2
q)`:alpha upsert`a`b!1 2
q)show get`:alpha
a b
---
1 2
1 2
这里没有什么奇怪的。 set
命令在文件 alpha
中保存 1 行 table(回想一下,在 q 中 table 是一个字典列表),然后 upsert
命令附加一行与保存的行相同,最后 get
读取结果 2 行 table 回来。
您可能想要的是在循环外初始化 table,在循环中 运行 一系列更新并在循环后显示结果。
请注意,您可以像这样初始化 table:
>>> q.set(':alpha', q('!', ["Name","Bid","Ask","Time"], ()).flip)
k('`:alpha')
>>> q.get(':alpha').show()
Name Bid Ask Time
-----------------
最后,如果您的目标是保存提要,则每条消息一到达就将其写入磁盘很少是个好主意。大多数 feed handlers 在内存中收集一天的市场数据,并在一天结束时将其保存在 HDB 中。
当我在pyq中运行下面的代码...
from bittrex.bittrex import Bittrex, API_V2_0
import time, json
from pyq import q, K
get_bittrex = Bittrex(None, None)
starttime = time.time()
market_result = get_bittrex.get_market_summaries()['result']
while True:
for res in market_result:
market_name = res['MarketName']
ask = str(res['Ask'])
bid = str(res['Bid'])
last = str(res['Last'])
timeStamp = str(res['TimeStamp'])
if market_name in ['USDT-BTC', 'USDT-ETH', 'USDT-LTC']:
mkt = {"Name": market_name,"Bid": bid,"Ask": ask,"Time":timeStamp}
q.set(':alpha', [mkt])
q.upsert(':alpha', mkt)
q.get(':alpha').show()
time.sleep(10.0)
它 returns 每 10 秒到控制台...
Name Bid Ask Time
--------------------------------------------------------------
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
Name Bid Ask Time
------------------------------------------------------
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
Name Bid Ask Time
----------------------------------------------------------
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
Name Bid Ask Time
--------------------------------------------------------------
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
Name Bid Ask Time
------------------------------------------------------
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
Name Bid Ask Time
----------------------------------------------------------
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
为什么每行打印 2 次?此外,我如何修改它以便将每个 market_name
添加到相同的 table,即它当前正在将每个 market_name
打印到它自己的 table 而不是附加到现有的 table table.
理想的输出如下...
Name Bid Ask Time
--------------------------------------------------------------
USDT-BTC 15475.00000001 15530.99999999 2017-12-09T02:46:52.547
USDT-ETH 454.00000001 454.9999 2017-12-09T02:46:52.017
USDT-LTC 133.76999998 133.98999999 2017-12-09T02:46:49.703
你在循环中所做的相当于下面的q码:
q)`:alpha set enlist`a`b!1 2
q)`:alpha upsert`a`b!1 2
q)show get`:alpha
a b
---
1 2
1 2
这里没有什么奇怪的。 set
命令在文件 alpha
中保存 1 行 table(回想一下,在 q 中 table 是一个字典列表),然后 upsert
命令附加一行与保存的行相同,最后 get
读取结果 2 行 table 回来。
您可能想要的是在循环外初始化 table,在循环中 运行 一系列更新并在循环后显示结果。
请注意,您可以像这样初始化 table:
>>> q.set(':alpha', q('!', ["Name","Bid","Ask","Time"], ()).flip)
k('`:alpha')
>>> q.get(':alpha').show()
Name Bid Ask Time
-----------------
最后,如果您的目标是保存提要,则每条消息一到达就将其写入磁盘很少是个好主意。大多数 feed handlers 在内存中收集一天的市场数据,并在一天结束时将其保存在 HDB 中。