java高效去重
假设您有一个很大的文本文件。每行包含一个电子邮件 ID 和一些其他信息(比如一些产品 ID)。假设文件中有数百万行。您必须将此数据加载到数据库中。如何有效地重复数据删除(即消除重复数据)?
Lets say you have a large text file. Each row contains an email id and some other information (say some product-id). Assume there are millions of rows in the file. You have to load this data in a database. How would you efficiently de-dup data (i.e. eliminate duplicates)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
疯狂的行数
无法容纳内存中的所有行
适合内存
哦,如果我是你,我无论如何都会对数据库施加唯一约束......
Insane number of rows
Unable to fit all rows in memory
Fits in memory
Oh and if I were you, I will put the unique constraint on the DB anyways...
我将从显而易见的答案开始。创建一个哈希图,并将电子邮件 ID 作为键,将其余信息放入值(或创建一个对象来保存所有信息)。当您到达新行时,检查该键是否存在,是否移动到下一行。最后使用 HashMap 写出所有 SQL 语句。我确实同意 eqbridges 的观点,即如果您有“无数”行,那么内存限制将很重要。
I will start with the obvious answer. Make a hashmap and put the email id in as the key and the rest of the information in to the value (or make an object to hold all the information). When you get to a new line, check to see if the key exists, if it does move to the next line. At the end write out all your SQL statements using the HashMap. I do agree with eqbridges that memory constraints will be important if you have a "gazillion" rows.
您有两个选择,
用 Java 实现:您可以将诸如
HashSet
之类的东西放在一起进行测试 - 如果集合中不存在每个项目,则为每个项目添加电子邮件 ID。在数据库中执行:在表上放置唯一约束,这样dups就不会添加到表中。这样做的一个额外好处是,您可以重复该过程并从以前的运行中删除重复项。
You have two options,
do it in Java: you could put together something like a
HashSet
for testing - adding an email id for each item that comes in if it doesnt exist in the set.do it in the database: put a unique constraint on the table, such that dups will not be added to the table. An added bonus to this is that you can repeat the process and remove dups from previous runs.
看看 Duke (https://github.com/larsga/Duke) 快速重复数据删除和记录用java编写的链接引擎。它使用Lucene进行索引并减少比较次数(以避免不可接受的笛卡尔积比较)。它支持最常见的算法(编辑距离、jaro winkler 等),并且具有极强的可扩展性和可配置性。
Take a look at Duke (https://github.com/larsga/Duke) a fast dedupe and record linkage engine written in java. It uses Lucene to index and reduce the number of comparison (to avoid the unacceptable Cartesian product comparison). It supports the most common algorithm (edit distance, jaro winkler, etc) and it is extremely extensible and configurable.
不能通过电子邮件和产品 ID 为表建立索引吗?然后按索引读取应该可以通过顺序读取轻松识别电子邮件或电子邮件+prodId 的重复项,并简单地匹配先前的记录。
Can you not index the table by email and product ID? Then reading by index should make duplicates of either email or email+prodId readily identified via sequential reads and simply matching the previous record.
您的问题可以通过提取、转换、加载 (ETL) 方法来解决:
您可以手动执行此操作或使用 ETL 工具。
Your problem can be solve with a Extract, transform, load (ETL) approach:
You can do this manually or use an ETL tool.