Kdb+/q:如何批量插入带有索引的 KDB+ table?
Kdb+/q: How to bulk insert into a KDB+ table with an index?
我正在尝试将多条记录同时批量插入到 KDB+ 数据库中:
> trades:([]time:`datetime$();side:`symbol$();qty:`float$();price:`float$();exch:`symbol$();sym:`symbol$())
> t: .z.z / intentionally the same time
> `trades insert (t t;`buy `sell;10 10;10 10;`exch `exch;`sym `sym)
但是它会在 sym 列中引发错误
'sym
[0] `depths insert (t t;`buy `sell;10 10;10 10; `exch `exch;`sym `sym)
^
不知道我在这里做错了什么,但它似乎是值不变的,即无论提供的值如何,它总是在最后一列引发错误。
有人可以告诉我应该如何使用上面描述的时间索引将批量记录插入 kdb+。
谢谢
在你原来的插入语句中,你在中间有空格
`sym `sym
,
`exch `exch
和 `buy `sell
。符号之间的空格使其成为应用程序或索引,而不是您想要的列表。
此外,因为您已将 qty
和 price
指定为
float
,您必须在插入时将数字指定为 float
trades
table。
以下行应该完成您打算做的事情:
`trades insert (2#t;`buy`sell;10 10f;10 10f;`exch`exch;`sym`sym)
最后,我建议将 qty
列的架构更改为 int/long
,因为数量通常不需要小数点。
希望这对您有所帮助!
丹尼尔赚到了钱。为了扩展他的答案,q
会将 space 分隔的列表整理成一个数值对象,即使这样,类型规范也必须只存在于最后一项。有关列表创建的更多详细信息,请参见 here.
q)a:10f 10f
'10f
q)a:10 10f
其次,学习kdb的人在追加到表时经常遇到类型错误是很常见的。这种情况下的问题是 kdb 没有将同质原子列表提升为更广泛的类型(即 expected behaviour)。以下是一个有用的小 lambda,可让您知道在执行 insert
或 upsert
操作时出错的地方:
q)trades:([]time:`datetime$();side:`symbol$();qty:`float$();price:`float$();exch:`symbol$();sym:`symbol$())
q)rows:(t,t;`buy`sell;10 10;10 10;`exch`exch;`sym`sym)
q)insertTest:{[tab;rows] m:0!meta tab; wh: where not m[`t] ~' rt:.Q.ty each rows; @[flip;;enlist] `item`currType`expectedType!(m[`c] wh;rt wh; m[`t] wh)}
item currType expectedType
---------------------------
qty j f
price j f
我正在尝试将多条记录同时批量插入到 KDB+ 数据库中:
> trades:([]time:`datetime$();side:`symbol$();qty:`float$();price:`float$();exch:`symbol$();sym:`symbol$())
> t: .z.z / intentionally the same time
> `trades insert (t t;`buy `sell;10 10;10 10;`exch `exch;`sym `sym)
但是它会在 sym 列中引发错误 'sym
[0] `depths insert (t t;`buy `sell;10 10;10 10; `exch `exch;`sym `sym)
^
不知道我在这里做错了什么,但它似乎是值不变的,即无论提供的值如何,它总是在最后一列引发错误。 有人可以告诉我应该如何使用上面描述的时间索引将批量记录插入 kdb+。
谢谢
在你原来的插入语句中,你在中间有空格
`sym `sym
,
`exch `exch
和 `buy `sell
。符号之间的空格使其成为应用程序或索引,而不是您想要的列表。
此外,因为您已将 qty
和 price
指定为
float
,您必须在插入时将数字指定为 float
trades
table。
以下行应该完成您打算做的事情:
`trades insert (2#t;`buy`sell;10 10f;10 10f;`exch`exch;`sym`sym)
最后,我建议将 qty
列的架构更改为 int/long
,因为数量通常不需要小数点。
希望这对您有所帮助!
丹尼尔赚到了钱。为了扩展他的答案,q
会将 space 分隔的列表整理成一个数值对象,即使这样,类型规范也必须只存在于最后一项。有关列表创建的更多详细信息,请参见 here.
q)a:10f 10f
'10f
q)a:10 10f
其次,学习kdb的人在追加到表时经常遇到类型错误是很常见的。这种情况下的问题是 kdb 没有将同质原子列表提升为更广泛的类型(即 expected behaviour)。以下是一个有用的小 lambda,可让您知道在执行 insert
或 upsert
操作时出错的地方:
q)trades:([]time:`datetime$();side:`symbol$();qty:`float$();price:`float$();exch:`symbol$();sym:`symbol$())
q)rows:(t,t;`buy`sell;10 10;10 10;`exch`exch;`sym`sym)
q)insertTest:{[tab;rows] m:0!meta tab; wh: where not m[`t] ~' rt:.Q.ty each rows; @[flip;;enlist] `item`currType`expectedType!(m[`c] wh;rt wh; m[`t] wh)}
item currType expectedType
---------------------------
qty j f
price j f