所理解的问题定义.
假设您的文件包含
ID,Name,Age
1,Jim,20
2,Tim,30
3,Kim,40
正如您所说,可以添加/更新行,因此文件变为
ID,Name,Age
1,Jim,20 -- to be discarded
2,Tim,35 -- to be updated
3,Kim,40 -- to be discarded
4,Zim,30 -- to be inserted
现在的需求是通过在两个sql查询或包含两个sql语句的1个批量查询中仅插入/更新以上2条记录来更新数据库。
我正在做以下假设 here
- 您无法修改现有的文件创建过程。
- 您正在使用一些批处理[从文件读取-在内存中处理-在数据库中写入]
上传数据库中的数据。
将记录 [Name,Age] 的哈希值与 ID 存储在内存映射中,其中 ID 是键,值是哈希 [如果需要可扩展性,请使用 hazelcast ]。
您的批处理框架加载数据[再次假设将一行文件视为一条记录],需要根据内存中映射中的 ID 检查计算的哈希值。第一次创建也可以使用批处理框架来读取文件。
If (ID present)
--- compare hash
---found same then discard it
—found different create an update sql
In case ID not present in in-memory hash,create an insert sql and insert the hashvalue
您可能会使用 spring-batch 和 hazelcast 进行并行处理、块处理和内存中数据分区。
http://www.hazelcast.com/ http://www.hazelcast.com/
http://static.springframework.org/spring-batch/ http://static.springframework.org/spring-batch/
希望这可以帮助。