How to enable batch mode in Java for InfluxDB 2.0?

The 1.8 version allows me to enable batching of single Point writes as easy as follows:

influxDB.enableBatch(100000, 10000, TimeUnit.MILLISECONDS);

Could you tell me what is the equivalent of 2.0 version to enable batch mode? It would be better if you could give me a Java code snippet.

Hi @mfrice,

thanks for using our client. The default configuration for WriteApi is asynchronous batching mode. For more info see - influxdb-client-java/client at master · influxdata/influxdb-client-java · GitHub

Regards

Hi @bednar ,

Thank you for your reply! The documentation didn’t show how to modify the batch options. I tried to write my own code, which is as follows:

WriteApi writeApi = client.makeWriteApi(
		WriteOptions.builder()
		            .batchSize(300000)
		            .flushInterval(30000)
		            .build())

Then I call its writePoint(Point point) method as usual.

writeApi.writePoint(point);

Is it correct? Is it the same as influxDB.enableBatch(300000, 30000, TimeUnit.MILLISECONDS); then call influxDB.write(point) as in the older version?

1 Like

Here is an example: influxdb-client-java/examples/src/main/java/example/PlatformExample.java at 050db6ae578951a097c4525bee6c36d83c61f953 · influxdata/influxdb-client-java · GitHub

Yes, but be sure that you use WriteApi as a singleton and also call .close() after finish your work.

Yes

Hi @bednar ,
I am really confused that I keep getting the bufferLimit warning which I’ve never gotten in the older version.

Aug 24, 2021 4:02:05 AM com.influxdb.client.write.events.BackpressureEvent logEvent
WARNING: Backpressure applied, try increase WriteOptions.bufferLimit

I tried several configurations including default but still get it. Do I need to manually clear the buffer before restart my program?

On the other hand, does the backpressure strategy means I would loss data? Can I choose to NOT loss data when the buffer is full?

update: I found there is a WriteApiBlocking class, does it also support batching on default?

No. Increase the bufferLimit because the default value is 10_000. You can use something like:

WriteOptions.builder()
            .batchSize(300_000)
            .bufferLimit(6_000_000)
            .flushInterval(30_000)
            .build()

Before restart your program, you should call: writeApi.close(); to flush your data into InfluxDB.

You are able to choose from following strategies:

  • DROP_OLDEST - Drop the oldest data points from the backlog
  • DROP_LATEST - Drop the latest data points from the backlog
  • ERROR - Signal a exception

For more info see - influxdb-client-java/client/README.md at master · influxdata/influxdb-client-java · GitHub

No.