clickhouse-java icon indicating copy to clipboard operation
clickhouse-java copied to clipboard

How do I update the asynchronous write code after I upgrade the dependent version

Open WellJay opened this issue 1 year ago • 1 comments

How do I update the asynchronous write code after I upgrade the dependent version

version: 0.3.1-patch --> 0.6.0-patch3

ClickHouseDataSource clickHouseDataSource = this.clickHouseDataSource;
try (ClickHouseConnection connection = clickHouseDataSource.getConnection();
     ClickHouseStatement statement = connection.createStatement()) {
    StringBuilder stringBuilder = new StringBuilder();
    for (String segment : segments) {
        stringBuilder.append(segment).append("\n");
    }
    String database = clickhouseWriteProperties.getProperty("write.database");
    String table = clickhouseWriteProperties.getProperty("write.table");

  //version 0.3.1-patch code
  //=============================
    statement.write()
            .sql("insert into " + database + "." + table)
            .data(new ByteArrayInputStream(stringBuilder.toString().getBytes(StandardCharsets.UTF_8)),
                    ClickHouseFormat.CSV)
            .send();
  //=============================

    stringBuilder.setLength(0);
} catch (Exception e) {
    e.printStackTrace();
}

WellJay avatar Apr 23 '24 03:04 WellJay

Good day, @WellJay !

There are a few changes in Mutation class that is returned by .write():

  • no raw sql. There is one methods instead: table(). Client will generate required SQL itself
  • format is passed by separate method: format()
  • method send() replaced with two execute() (returns Future<ClickHouseResponse>) and executeAndWait() (waits for completion and returns response.

Here is an adopted code example:

stmt.write()
                    .table("table")
                    .format(ClickHouseFormat.CSV)
                    .data(new ByteArrayInputStream(stringBuilder.toString().getBytes(StandardCharsets.UTF_8)))
                    .execute();

chernser avatar Apr 24 '24 05:04 chernser