【发布时间】:2020-06-28 18:38:33
【问题描述】:
对不起,我是 Q 新手,找不到解决方案。下面的代码将一个四列 CSV 文件附加到 KDB+ 数据库。此代码运行良好,但现在我的数据库很大,它会引发 WSFULL 错误。也许有一种更节省内存的方式来编写它。请帮忙:
// FILE_LOADER.q
\c 520 500
if [(count .z.x) < 1;
show `$"usage: q loadcsv.q inputfile destfile
where inputfile and destfile are absolute or relative paths to
the files. Inputfile has the following fields:
DATE, TICKER, FIELD, VALUE. DATE is of type date,
TICKER and FIELD are strings, and VALUE is converted to a float.
Any string VALUEs will show up as nulls.";
exit 1
]
f1: hsym `$.z.x[0]
f2: hsym `$.z.x[1]
columns: `DATE`TICKER`FIELD`VALUE
if [() ~ key f1; show ("Input file '",.z.x[0],"' not found");exit 1]
x: .Q.fsn[{f2 upsert flip columns!("DSSF";",")0:x};f1;4194000000]
show ("loaded ",(string x)," characters into the kdb database")
exit 0
【问题讨论】:
-
你读过code.kx上.Q.fsn的定义了吗? code.kx.com/q/ref/dotq/#qfsn-streaming-algorithm - “.Q.fsn 几乎与 .Q.fs 相同,但需要一个额外的参数 z,即读取块的字节大小。这对于平衡负载速度和 RAM 使用特别有用”。较低的值 = 更多的时间但使用较少的 RAM,较高的值(您拥有)= 更快但更高的 RAM 使用率
标签: csv out-of-memory kdb