usman
usman

Reputation: 1391

how can we Batch insert using java clickhouse-client?

Performance of clickhouse-client is far better than clickhouse-jdbc where we do have preparedStatment for batchInsert. But how can we do Batch insert using clickhouse-client.

try (ClickHouseClient client = ClickHouseClient.newInstance(preferredProtocol);
    ClickHouseResponse response = client.connect(server)
        .format(preferredFormat)
        .query("insert query")
        .params("param for insert query").execute().get()) {

    }
}

As http request is stateless and single but is there any other class or way to have batch insert OR we need to dynamically build insert query with multiple VALUES as parameters for query statement?

Upvotes: 2

Views: 5071

Answers (1)

Denny Crane
Denny Crane

Reputation: 13275

For example https://github.com/ClickHouse/clickhouse-jdbc

try (ClickHouseClient client = ClickHouseClient.newInstance(server.getProtocol())) {
    ClickHouseRequest<?> request = client.connect(server).format(ClickHouseFormat.RowBinaryWithNamesAndTypes);
    // load data into a table and wait until it's completed
    request.write().query("insert into my_table select c2, c3 from input('c1 UInt8, c2 String, c3 Int32')")
        .data(myInputStream).execute().thenAccept(response -> {
            response.close();
        });

You just need to implement myInputStream.

I have a question, what is the source of your data? Is it a file? Or stream of data in some format?

Upvotes: 1

Related Questions