mongodb批量处理
<p>mongodb支持批量插入。</p><p>1.使用Java mongodb api</p>
<p>查看源码com.mongodb.MongoCollectionImpl,有两个方法</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> insertMany(<span style="color: rgba(0, 0, 255, 1)">final</span> List<? <span style="color: rgba(0, 0, 255, 1)">extends</span> TDocument><span style="color: rgba(0, 0, 0, 1)"> documents) {
insertMany(documents, </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InsertManyOptions());
}
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> insertMany(<span style="color: rgba(0, 0, 255, 1)">final</span> List<? <span style="color: rgba(0, 0, 255, 1)">extends</span> TDocument> documents, <span style="color: rgba(0, 0, 255, 1)">final</span><span style="color: rgba(0, 0, 0, 1)"> InsertManyOptions options) {
notNull(</span>"documents"<span style="color: rgba(0, 0, 0, 1)">, documents);
List</span><InsertRequest> requests = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<InsertRequest><span style="color: rgba(0, 0, 0, 1)">(documents.size());
</span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (TDocument document : documents) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (document == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> IllegalArgumentException("documents can not contain a null value"<span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (getCodec() <span style="color: rgba(0, 0, 255, 1)">instanceof</span><span style="color: rgba(0, 0, 0, 1)"> CollectibleCodec) {
document </span>= ((CollectibleCodec<TDocument><span style="color: rgba(0, 0, 0, 1)">) getCodec()).generateIdIfAbsentFromDocument(document);
}
requests.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> InsertRequest(documentToBsonDocument(document)));
}
executor.execute(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> MixedBulkWriteOperation(namespace, requests, options.isOrdered(), writeConcern)
.bypassDocumentValidation(options.getBypassDocumentValidation()));
}</span></pre>
</div>
<pre>insertMany(final List<? extends TDocument> documents) 默认使用 private boolean ordered = true;即有序插入。</pre>
<pre>insertMany(final List<? extends TDocument> documents, final InsertManyOptions options)调用者自己设置。<br><br>ordered属性有什么用?</pre>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* Gets whether the documents should be inserted in the order provided, stopping on the first failed insertion. The default is true.
* If false, the server will attempt to insert all the documents regardless of an failures.
*
* </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> whether the the documents should be inserted in order
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> isOrdered() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ordered;
}</span></pre>
</div>
<p>大概意思是:</p>
<p>true:按提供的顺序插入文档,并在首次插入失败时停止。 (按顺序插入文档,遇到失败后停止。之前已经插入成功的不返回,失败及失败之后的不插入。)</p>
<p>false: 服务器将尝试插入所有文档,而不考虑失败。(只要能插入成功的,都存储进数据库)</p>
<p> </p>
<p>2.spring-data-mongodb</p>
<p>org.springframework.data.mongodb.core.MongoTemplate</p>
<p>以下是该方法的源码和使用案例:</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.ExecutableInsertOperation#bulkOps(org.springframework.data.mongodb.core.BulkMode, java.lang.String)
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> BulkOperations bulkOps(BulkMode bulkMode, String collectionName) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> bulkOps(bulkMode, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">, collectionName);
}</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> batchInsertStudents() {</span><span style="color: rgba(0, 128, 0, 1)"><br></span></pre>
<pre><span> BulkWriteResult result = null<span>;</span></span></pre>
<pre> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
List</span><Student> documents = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<><span style="color: rgba(0, 0, 0, 1)">();</span>
String collectionName = "myTestCol"<span style="color: rgba(0, 0, 0, 1)">;</span><span style="color: rgba(0, 0, 0, 1)">
BulkOperations bulkOp </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.mongoTemplate.bulkOps(BulkMode.UNORDERED, collectionName);</span>
<span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">int</span> i = 502610; i< 2000000; i++<span style="color: rgba(0, 0, 0, 1)">) {
Student student </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Student();
student.setId(String.valueOf(i));
student.setAge(i);
student.setGender(</span>"男"<span style="color: rgba(0, 0, 0, 1)">);
student.setName(</span>"李三"+<span style="color: rgba(0, 0, 0, 1)"> i);
documents.add(student);
}
bulkOp.insert(documents);</span><span style="color: rgba(0, 0, 0, 1)">
result </span>=<span style="color: rgba(0, 0, 0, 1)"> bulkOp.execute();</span>
<span style="color: rgba(0, 0, 0, 1)"> }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (DuplicateKeyException e) {
System.out.println(</span>"**********" +<span style="color: rgba(0, 0, 0, 1)"> e.getMessage());</span><span style="color: rgba(0, 0, 0, 1)">
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> result<span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p>有两种模式:</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
* Mode for bulk operation.
*</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">enum</span><span style="color: rgba(0, 0, 0, 1)"> BulkMode {
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> Perform bulk operations in sequence. The first error will cancel processing. </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
ORDERED,
</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> Perform bulk operations in parallel. Processing will continue on errors. </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
UNORDERED
};</span></pre>
</div>
<p>与之前的order(true|false)相对应。</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/muxi0407/p/11648383.html
頁:
[1]