简体   繁体   English

MongoDb Java APi-忽略批量导入中的错误

[英]MongoDb Java APi - Ignore errors in bulk import

Hi i'm importing Documents into mongodb with this function 嗨,我正在使用此功能将文档导入mongodb

WriteResult com.mongodb.DBCollection.insert(List<DBObject> list)

some inserts fail because the data violates an index. 一些插入失败,因为数据违反索引。 Is it possible to ignore these errors and continue with the other documents? 是否可以忽略这些错误并继续其他文档?

Exception in thread "main" com.mongodb.WriteConcernException: { "serverUsed" : "localhost:27017" , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 16755 Can't extract geo keys from object, malformed geometry?: XXXXX , "code" : 16755}
    at com.mongodb.CommandResult.getWriteException(CommandResult.java:90)
    at com.mongodb.CommandResult.getException(CommandResult.java:79)
    at com.mongodb.DBCollectionImpl.translateBulkWriteException(DBCollectionImpl.java:314)
    at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:189)

对我有用的是:

wayCollection.insert(ways, new WriteConcern(0, 0, false, false, true));

If you're using MongoDB 2.6+ you can do an unordered bulk operation . 如果您使用的是MongoDB 2.6+,则可以执行无序批量操作 If the error occurs when doing write operations, MongoDB will continue to process the remaining operations: 如果在执行写操作时发生错误,MongoDB将继续处理其余操作:

DBCollection coll = db.getCollection("test");
BulkWriteOperation bulk = coll.initializeUnorderedBulkOperation();
bulk.insert(new BasicDBObject("foo", 1));
bulk.insert(new BasicDBObject("bar", 2));
bulk.execute();

The downside to this approach is that you can't use it if your inserts needs to be executed in an order, but the upside is that the bulk inserts will be executed faster than by doing multiple inserts. 这种方法的缺点是,如果需要按顺序执行插入操作,则无法使用它,但不利的是,批量插入操作的执行速度要比多次插入操作快。

The additional benefit is that you can get the number of inserted documents from the BulkWriteResult object (returned from the execute method). 另一个好处是,您可以从BulkWriteResult对象(从execute方法返回)中获得插入文档的数量。

You can take a look at the Java documentation for bulk inserts here . 您可以在此处查看有关批量插入的Java文档。

Edit: Just to be clear, I don't recommend that you ignore the errors, you should fix your data/inserts. 编辑:为了清楚起见,我不建议您忽略这些错误,您应该修复数据/插入。

Edit 2 You can also execute a bulk operation with and set a write concern for the operation: 编辑2您还可以使用执行批量操作并为该操作设置写关注点:

bulk.execute(new WriteConcern(0, 0, false, false, true));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM