Package org.elasticsearch.action.bulk
Class BulkProcessor
java.lang.Object
org.elasticsearch.action.bulk.BulkProcessor
- All Implemented Interfaces:
Closeable
,AutoCloseable
A bulk processor is a thread safe bulk processing class, allowing to easily set when to "flush" a new bulk request
(either based on number of actions, based on the size, or time), and to easily control the number of concurrent bulk
requests allowed to be executed in parallel.
In order to create a new bulk processor, use the BulkProcessor.Builder
.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
A builder used to create a build an instance of a bulk processor.static interface
A listener for the execution. -
Method Summary
Modifier and TypeMethodDescriptionadd(DeleteRequest request)
Adds anDeleteRequest
to the list of actions to execute.add(DocWriteRequest<?> request)
Adds either a delete or an index request.add(IndexRequest request)
Adds anIndexRequest
to the list of actions to execute.add(BytesReference data, String defaultIndex, String defaultType, String defaultPipeline, org.elasticsearch.common.xcontent.XContentType xContentType)
Adds the data from the bytes to be processed by the bulk processoradd(BytesReference data, String defaultIndex, String defaultType, org.elasticsearch.common.xcontent.XContentType xContentType)
Adds the data from the bytes to be processed by the bulk processorboolean
awaitClose(long timeout, TimeUnit unit)
Closes the processor.static BulkProcessor.Builder
builder(BiConsumer<BulkRequest,ActionListener<BulkResponse>> consumer, BulkProcessor.Listener listener)
Deprecated.static BulkProcessor.Builder
builder(BiConsumer<BulkRequest,ActionListener<BulkResponse>> consumer, BulkProcessor.Listener listener, String name)
static BulkProcessor.Builder
builder(Client client, BulkProcessor.Listener listener)
Deprecated.Usebuilder(BiConsumer, Listener, String)
with client::bulk as the first argument, orbuilder(org.elasticsearch.client.Client, org.elasticsearch.action.bulk.BulkProcessor.Listener, org.elasticsearch.threadpool.Scheduler, org.elasticsearch.threadpool.Scheduler, java.lang.Runnable)
and manage the flush and retry schedulers explicitlystatic BulkProcessor.Builder
builder(Client client, BulkProcessor.Listener listener, Scheduler flushScheduler, Scheduler retryScheduler, Runnable onClose)
void
close()
Closes the processor.protected void
void
flush()
Flush pending delete or index requests.
-
Method Details
-
builder
public static BulkProcessor.Builder builder(Client client, BulkProcessor.Listener listener, Scheduler flushScheduler, Scheduler retryScheduler, Runnable onClose)- Parameters:
client
- The client that executes the bulk operationslistener
- The BulkProcessor listener that gets called on bulk eventsflushScheduler
- The scheduler that is used to flushretryScheduler
- The scheduler that is used for retriesonClose
- The runnable instance that is executed on close. Consumers are required to clean up the schedulers.- Returns:
- the builder for BulkProcessor
-
builder
@Deprecated public static BulkProcessor.Builder builder(Client client, BulkProcessor.Listener listener)Deprecated.Usebuilder(BiConsumer, Listener, String)
with client::bulk as the first argument, orbuilder(org.elasticsearch.client.Client, org.elasticsearch.action.bulk.BulkProcessor.Listener, org.elasticsearch.threadpool.Scheduler, org.elasticsearch.threadpool.Scheduler, java.lang.Runnable)
and manage the flush and retry schedulers explicitly- Parameters:
client
- The client that executes the bulk operationslistener
- The BulkProcessor listener that gets called on bulk events- Returns:
- the builder for BulkProcessor
-
builder
@Deprecated public static BulkProcessor.Builder builder(BiConsumer<BulkRequest,ActionListener<BulkResponse>> consumer, BulkProcessor.Listener listener)Deprecated.usebuilder(BiConsumer, Listener, String)
instead- Parameters:
consumer
- The consumer that is called to fulfil bulk operationslistener
- The BulkProcessor listener that gets called on bulk events- Returns:
- the builder for BulkProcessor
-
builder
public static BulkProcessor.Builder builder(BiConsumer<BulkRequest,ActionListener<BulkResponse>> consumer, BulkProcessor.Listener listener, String name)- Parameters:
consumer
- The consumer that is called to fulfil bulk operationslistener
- The BulkProcessor listener that gets called on bulk eventsname
- The name of this processor, e.g. to identify the scheduler threads- Returns:
- the builder for BulkProcessor
-
close
public void close()Closes the processor. If flushing by time is enabled, then it's shutdown. Any remaining bulk actions are flushed.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
awaitClose
Closes the processor. If flushing by time is enabled, then it's shutdown. Any remaining bulk actions are flushed.If concurrent requests are not enabled, returns
true
immediately. If concurrent requests are enabled, waits for up to the specified timeout for all bulk requests to complete then returnstrue
If the specified waiting time elapses before all bulk requests complete,false
is returned.- Parameters:
timeout
- The maximum time to wait for the bulk requests to completeunit
- The time unit of thetimeout
argument- Returns:
true
if all bulk requests completed andfalse
if the waiting time elapsed before all the bulk requests completed- Throws:
InterruptedException
- If the current thread is interrupted
-
add
Adds anIndexRequest
to the list of actions to execute. Follows the same behavior ofIndexRequest
(for example, if no id is provided, one will be generated, or usage of the create flag). -
add
Adds anDeleteRequest
to the list of actions to execute. -
add
Adds either a delete or an index request. -
ensureOpen
protected void ensureOpen() -
add
public BulkProcessor add(BytesReference data, @Nullable String defaultIndex, @Nullable String defaultType, org.elasticsearch.common.xcontent.XContentType xContentType) throws ExceptionAdds the data from the bytes to be processed by the bulk processor- Throws:
Exception
-
add
public BulkProcessor add(BytesReference data, @Nullable String defaultIndex, @Nullable String defaultType, @Nullable String defaultPipeline, org.elasticsearch.common.xcontent.XContentType xContentType) throws ExceptionAdds the data from the bytes to be processed by the bulk processor- Throws:
Exception
-
flush
public void flush()Flush pending delete or index requests.
-
builder(BiConsumer, Listener, String)
instead