转换来自 json 的表的列类型

问题描述 投票:0回答:1

我解析了一个 json 表,最终得到了许多混合类型的列。

([]col1:("tet1";"test2";());col2:(234j;0b;33j);col3:("sda";0b;"");col4:(0b;"";"dadd"))

我知道:

 col1 - symbol
 col2 - long
 col3 - float 
 col4 - string 

如何摆脱非法字符并解析列。 请记住,应避免执行每一项操作(1 亿行 x 220 列)

kdb
1个回答
0
投票

doing each is to be avoided
当处理具有混合内容的
0h
类型的混合列表时,不可避免地需要迭代器。下面使用每个右边
/:

您可以借鉴以下内容中的一些演员助手技巧: https://kx.com/blog/kdb-q-insights-parsing-json-files/

q)t:([]col1:("tet1";"test2";());col2:(234j;0b;33j);col3:("sda";0b;"");col4:(0b;"";"dadd"))

q)generalHelper:{[t;d]![t;();0b;key[d]!{(x;y)}'[value d;key d]]}

q)cleanseRules:`long`symbol`string`float!(
    {@[x;where 0b~/:x;:;0N]};
    {i:where ()~/:x;`$@[x;i;:;count[i]#enlist ""]};
    {i:where 0b~/:x;@[x;i;:;count[i]#enlist ""]};
    {i:where 0b~/:x;"F"$@[x;i;:;count[i]#enlist ""]}
 )

q)colRules:`col1`col2`col3`col4!`symbol`long`float`string

q)generalHelper[;cleanseRules colRules] t
col1  col2 col3 col4
----------------------
tet1  234       ""
test2           ""
      33        "dadd"

这些需要根据您确切的传入 JSON 数据和结果要求进行调整。

© www.soinside.com 2019 - 2024. All rights reserved.