How to batch insert in YCQL API in Java?

The docs at Batch operations in YCQL | YugabyteDB Docs say:

BatchStatement batch = new BatchStatement();

But this does not work, as the BatchStatement class is abstract. There are no imports mentioned in the docs (that should be fixed), so I can only assume that the correct import is:

import com.datastax.oss.driver.api.core.cql.BatchStatement;

And this is coming from the (AFAIK) newest library:


There is a class DefaultBatchStatement and a BatchStatementBuilder, which all require additional parameters and appear to implement a different API than is used in the Yugabyte docs.

So am I doing something wrong or are the docs out of date?

Hi, here are some example with the import:

another one
There’s also this test:

Ok thanks. So it seems these are examples for an older driver (3.8.0-yb-x). The docs refer to driver 4.6.0-yb-x, which apparently has a newer API. I’ll look into whether I can figure out the new API and get it to work.

1 Like

Thanks, notified the doc team to look at it

The new API works like this:

        BatchStatementBuilder builder = new BatchStatementBuilder(BatchType.UNLOGGED);
        PreparedStatement stmt = session.prepare("...some cql...");
        Object[] values1 = ...;
        Object[] values2 = ...;

I am not sure what those BatchTypes really mean in the context of YugabyteDB, but I hope UNLOGGED is ok for mass data (like sensor data).

1 Like

Here is a sample code snippet with all the imports. There are no changes in how the client API behaves from 3.x to 4.x driver versions. However, the client APIs have moved under different packages -

package com.yugabyte.sample.apps;

import java.util.List;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.DefaultConsistencyLevel;
import com.datastax.oss.driver.api.core.cql.BatchStatement;
import com.datastax.oss.driver.api.core.cql.BoundStatement;
import com.datastax.oss.driver.api.core.cql.DefaultBatchType;
import com.datastax.oss.driver.api.core.cql.PreparedStatement;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.cql.SimpleStatement;

public class YcqlDemoApp {
	public static void main(String[] args) {

		try {
			CqlSession session = CqlSession.builder()
					.addContactPoint(new InetSocketAddress("", 9042))
			// Create keyspace 'ybdemo' if it does not exist.
			String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS ybdemo;";
			System.out.println("Created keyspace ybdemo");

			// Create table 'employee', if it does not exist.
			String createTable = "CREATE TABLE IF NOT EXISTS ybdemo.employee (id int PRIMARY KEY, " + "name varchar, "
					+ "age int, " + "language varchar);";
			SimpleStatement insertStmt1 = SimpleStatement
					.builder("INSERT INTO ybdemo.employee (id, name, age, language) VALUES (1, 'John', 35, 'Java');")
			SimpleStatement insertStmt2 = SimpleStatement
					.builder("INSERT INTO ybdemo.employee (id, name, age, language) VALUES (3453453, 'John', 35, 'Java');")
			PreparedStatement preparedInsrt1 = session.prepare(insertStmt1);
			PreparedStatement preparedInsrt2 = session.prepare(insertStmt2);
			BoundStatement boundSelectStmt1 = preparedInsrt1.bind();
			BoundStatement boundSelectStmt2 = preparedInsrt2.bind();
			BatchStatement batch = BatchStatement.newInstance(DefaultBatchType.UNLOGGED,
					boundSelectStmt1, boundSelectStmt2);
			SimpleStatement simpleSelect = SimpleStatement
					.builder("SELECT name, age, language FROM ybdemo.employee WHERE id = ?")

			PreparedStatement preparedSelect = session.prepare(simpleSelect);
			BoundStatement boundSelectStatement = preparedSelect.bind().setInt(0, 1);
			ResultSet selectResult = session.execute(boundSelectStatement);
			List<Row> rows = selectResult.all();
			String name = rows.get(0).getString(0);
			int age = rows.get(0).getInt(1);
			String language = rows.get(0).getString(2);
			System.out.println("Query returned " + rows.size() + " row: " + "name=" + name + ", age=" + age
					+ ", language: " + language);

		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());