对多个 txt 做 JOIN 关联

【问题】

Need a help. I have a big text file which has location data (represented numerically) and needs to be replaced with its corresponding location. These two files are separate text files.

Could you help with the java utility on how to replace the numeric values in one file using location values for the other.

Below is an example of the contents of file 1 and file 2. File 1 has numeric data and text. The numeric data in the first column needs to be replaced with the corresponding entry from file2. Thus, the file 1 needs to be looped for each entry in file 2.
Text File1:
19922973 @Uniquehope was good

Test File2:
19922973 Chicago, IL

Need to replace 19922973 with Chciago, IL. Please provide your inputs.

Thanks Saurabh

【回答】

这个类似于 SQL 语句中的 join,但 JAVA 本身不提供结构化计算函数,实现起来非常麻烦,如果文件太大而无法读入内存,代码将更加复杂。可以考虑用集算器辅助 JAVA 实现文本文件的连接,SPL 代码如下:



A

1

=file("D:\\file1.txt").import()

2

=file("D:\\file2.txt").import()

3

=join@1(A1:f1,_1; A2:f2,_1)

4

=A3.new(f2._2,f1._2)

5

=file("D:\\result.txt").export(A4)

A1/A2:分别从文本文件中读取数据

A3:函数 join 执行连接运算,并将两个表改名为 f1 和 f2。其中函数选项 @1 表示左连接,即例子中的要求:将 A2 对齐到 A1 中

1png

A4:从连接的表中取得需要的字段,组成新的序表

2png

A5:将 A4 的数据输出到新的文件

写好的脚本如何在 JAVA 应用程序中调用,可以参考Java 如何调用 SPL 脚本