我要学习新的Q语言和KDB+数据库。在记忆中,我很难将另一张桌子颠倒为现有的展开桌子。
我有一个名为temp
的下表,然后在`:temp
中保存为展开表
col1 col2 col3
--------------
a 0 1
b 1.5 2.5
c 1.1 2.2
d 0.99 0.98
展开表的目录结构:
temp
| .d
| col1
| col2
| col3
| sym
假设我有另一张桌子new
new: ([] col1:`e`f`g; col2:1.0 1.1 1.1; col3: 2.25 2.20 2.70)
我的问题是:如何将new
追加到`:temp
我读过https://code.kx.com/q/ref/upsert/,但给出的示例只有一条记录(而不是整张表(被追加销售。我尝试了`:temp upsert (`sym?new)
,导致type
错误。
编辑(我运行的所有命令(
q)temp: ([] col1:`a`b`c`d; col2:0.0 1.5 1.1 0.99; col3: 1.0 2.5 2.2 0.98)
q)`:temp/ set .Q.en[`:.;temp]
`:temp/
q)new: ([] col1:`e`f`g; col2:1.0 1.1 1.1; col3: 2.25 2.20 2.70)
q)`:temp upsert new
'type
[0] `:temp upsert new
^
您需要枚举任何符号类型的列,或者"手动";使用枚举扩展
q)`:temp/ set([]col1:`:sym?`a`b;col2:1 2f;col3:10 20f);
q)`:temp upsert([]col1:`:sym?`e`f`g;col2:1.0 1.1 1.1;col3:2.25 2.20 2.70);
q)get`:sym
`a`b`e`f`g
q)get`:temp
col1 col2 col3
--------------
a 1 10
b 2 20
e 1 2.25
f 1.1 2.2
g 1.1 2.7
或者使用诸如.Q.en 之类的辅助功能之一
q)`:temp/ set .Q.en[`:.;([]col1:`A`B;col2:1 2f;col3:10 20f)];
q)`:temp upsert .Q.en[`:.;([]col1:`e`f`g;col2:1.0 1.1 1.1;col3:2.25 2.20 2.70)];
q)get`:sym
`A`B`e`f`g
q)get`:temp
col1 col2 col3
--------------
A 1 10
B 2 20
e 1 2.25
f 1.1 2.2
g 1.1 2.7
当向诸如之类的持久化文件进行追加销售时,还需要注意其他事项
- 任何应用了属性的文件都将通过upstart操作删除这些属性(排序的除外,如果列表保持升序,则会保留排序的属性(
- 如果任何具有属性的文件被压缩,则除非您采取措施确保不压缩,否则它们将被重写
q)(`:groupedVector;17;2;6)set `g#1 2 3;
q)-21!`:groupedVector
compressedLength | 113
uncompressedLength| 240
algorithm | 2i
logicalBlockSize | 17i
zipLevel | 6i
q)attr get`:groupedVector
`g
q)
q)`:groupedVector upsert 4 5;
q)get `:groupedVector
1 2 3 4 5
q)-21!`:groupedVector // compression lost
q)attr get`:groupedVector // attribute lost
`
- 这取决于开发人员来确保磁盘上的列文件之间的兼容性&要附加的数据-否则您可能会以不同长度的列文件结束,例如
q)`:temp/ set([]char:"ab";long:1 2);
q)`:temp upsert ("c";3);
q)`:temp upsert ("d";4i);
'type
[0] `:temp upsert ("d";4i);
^
q)get`:temp/char
"abcd"
q)get`:temp/long
1 2 3
如果已经将temp
保存为展开表,则可以使用以下命令向其追加new
:
q) new:([]col1:`e`f`g;col2:1.0 1.1 1.1;col3:2.25 2.20 2.70)
q) `:temp upsert new
`:temp
q) get`:temp
col1 col2 col3
--------------
a 0 1
b 1.5 2.5
c 1.1 2.2
d 0.99 0.98
e 1 2.25
f 1.1 2.2
g 1.1 2.7
然后您可以看到所需的结果。如果您离开q会话并将其加载到一个新会话中,您将看到它已被保存。