动态查询大文本

【问题】
I am trying to extract certain records from a 4.5GB csv file and am running into some problems. (i) OpenOffice will only import 1 million records, and this file has more (ii) Even when working with only 1 million records, my 4GB RAM computer can’t handle it.

【回答】
用 JAVA 可以对大文本实现流式读取,但代码比较难写。这种情况可以试试 SPL,代码更加简洁。

比如从文件 emplyee.csv 中查询出 1981 年 1 月 1 日(含)之后出生的女员工,代码如下:



A

1

=file("D:/emplyee.csv").cursor@tc()

2

=A1.select(BIRTHDAY>=date("1981-01-01") && GENDER=="M")

3

=A1.fetch()

如果条件是不定的,可以将 A2 的代码改为:A1.select(${where}),这样就可以将查询条件写在 where 参数里,实现动态查询。如果查询结果较多,内存放不下,还可以将 A3 改为 file(“D:/result.txt”).export(A2),这可以将计算结果直接输出到文件中。

当然,除了上面这种写法外,集算器里还能对 SQL 添加条件,查询 csv 文件,如 =connect().query("select * from D:/emplyee.csv where"+where)

如果想提高性能,还可以使用多线程并行查询,具体内容可以参考集算器教程 并行计算小节。