CData JDBC Driver for MongoDB 2017J - Online Help

バッチ処理

CData JDBC Driver for MongoDB 2017J - Build 17.0.6669

CData JDBC Driver for MongoDB 2017J では、JDBC batch API を通じて、MongoDB の一括ロードサポートを活用することができます。batch API を使用して、関連するSQL データ操作ステートメントを同時に実行できます。

JDBC Batch API の使用

Statement、PreparedStatement、またはCallableStatement クラスを使用して一括操作を実行できます。次の例は、PreparedStatement を使用して一括操作を実行する方法を示しています。

一括挿入

To perform a bulk insert with a PreparedStatement, call addBatch for each set of parameters you want to execute as part of the bulk insert.After adding all the sets of parameters to the batch, you can execute the bulk insert by calling executeBatch.

The executeBatch method returns the update counts for each statement in an array.

Connection conn = DriverManager.getConnection("jdbc:mongodb:Server=127.0.0.1;Port=27017;Database=test;User=test;Password=test;"); 

String query = "INSERT INTO Customers (CompanyName) VALUES(?)"; 
PreparedStatement pstmt = conn.prepareStatement(query);

pstmt.setString(1, "Jon Deere"); 
pstmt.addBatch();

pstmt.setString(1, "Caterpillar"); 
pstmt.addBatch();

int[] r = pstmt.executeBatch();
for(int i: r)
  System.out.println(i);

一括更新

To perform a bulk update with a PreparedStatement, call addBatch for each set of parameters you want to execute as part of the bulk update.After adding all the sets of parameters to the batch, you can execute the bulk update by calling executeBatch.

The executeBatch method returns the update counts for each statement in an array.

Connection conn = DriverManager.getConnection("jdbc:mongodb:Server=127.0.0.1;Port=27017;Database=test;User=test;Password=test;");

String query = "UPDATE Customers SET CompanyName = ? WHERE _id = ?"; 
PreparedStatement pstmt = conn.prepareStatement(query);

pstmt.setString(1,"Jon Deere");
pstmt.setString(2,"MyId"); 
pstmt.addBatch();		

pstmt.setString(1,"Caterpillar");
pstmt.setString(2,"MyId2"); 
pstmt.addBatch();		

int[] r = pstmt.executeBatch();
for(int i: r)
  System.out.println(i);

一括削除

To perform a bulk delete with a PreparedStatement, call addBatch for each set of parameters you want to execute as part of the bulk delete.After adding all the sets of parameters to the batch, you can execute the bulk delete by calling executeBatch.

The executeBatch method returns the update counts for each statement in an array.

Connection conn = DriverManager.getConnection("jdbc:mongodb:Server=127.0.0.1;Port=27017;Database=test;User=test;Password=test;");

String query = "DELETE FROM Customers WHERE _id = ?";
PreparedStatement pstmt = conn.prepareStatement(query);

pstmt.setString(1,"MyId1"); 
pstmt.addBatch();		

pstmt.setString(1,"MyId2");
pstmt.addBatch();		

int[] r = pstmt.executeBatch();
for(int i: r)
  System.out.println(i);

バッチサイズの設定

サーバーに送信されるリクエストのサイズに制限がある場合は、バッチサイズの最大値を設定する必要があります。BatchSize プロパティを設定して、バッチ全体を指定された値のバッチに分割します。各バッチは、個別にサーバーに送信されます。

または、BatchSize を0 に設定して、バッチ全体を送信します。

 
 
Copyright (c) 2018 CData Software, Inc. - All rights reserved.
Build 17.0.6669.0