Bulk Insert: How to work with bulk insert operation?
One of the features that is particularly useful when inserting large amount of data is bulk inserting
. This is an optimized time-saving approach with few drawbacks that will be described later.
Syntax
public BulkInsertOperation bulkInsert();
public BulkInsertOperation bulkInsert(String database);
public BulkInsertOperation bulkInsert(String database, BulkInsertOptions options);
Parameters | ||
---|---|---|
database | String | Name of database for which bulk operation should be performed. If null then DefaultDatabase from DocumentStore will be used. |
options | BulkInsertOptions | Bulk operations options that should be used. |
Return Value | |
---|---|
BulkInsertOperation | Instance of BulkInsertOperation used for interaction. |
Limitations
There are several limitations to the API:
- Entity Id must be provided at the client side. The client by default will use the HiLo generator in order to generate the Id.
- Transactions are per batch, not per operation and DTC transactions are not supported.
- Documents inserted using bulk-insert will not raise notifications. More about
Changes API
can be found here. - Document Updates and Reference Checking must be explicitly turned on (see
BulkInsertOptions
). AfterCommit
method inPut Triggers
will be not executed in contrast toAllowPut
,AfterPut
andOnPut
.- The default configuration of the bulk insert is not thread safe, ChunkedBulkInsertOptions inside BulkInsertOptions should be set to null in order to support multi threaded usage. Refer to BulkInsertOptions for more details
Example
try (BulkInsertOperation bulkInsert = store.bulkInsert()) {
for (int i = 0; i < 1000 * 1000; i++){
Employee employee = new Employee();
employee.setFirstName("FirstName #" + i);
employee.setLastName("LastName #" + i);
bulkInsert.store(employee);
}
}