Skip to content

Commit 1c13e4d

Browse files
authored
Merge pull request #3350 from ClickHouse/v2_usecases_docs
[DRAFT] Documentation for client v2 how to read/write
2 parents 31589e2 + 6fea7c0 commit 1c13e4d

File tree

1 file changed

+122
-0
lines changed
  • docs/integrations/language-clients/java/client/_snippets

1 file changed

+122
-0
lines changed

docs/integrations/language-clients/java/client/_snippets/_v0_8.mdx

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,128 @@ try (InsertResponse response = client.insert(TABLE_NAME, events).get()) {
382382
}
383383
```
384384

385+
### insert(String tableName, DataStreamWriter writer, ClickHouseFormat format, InsertSettings settings)
386+
**Beta**
387+
388+
This API method allows to pass a writer object that will encode data directly into an output stream. Data will be compressed by the client.
389+
There is a configuration option in `InsertSettings` called `appCompressedData` that allows to turn off client compression and let application to send compressed stream.
390+
Examples shows major usecases this API was designed for.
391+
392+
`com.clickhouse.client.api.DataStreamWriter` is a functional interface with a method `onOutput` that is called by the client when output stream is ready for data to be written. This interface has
393+
another method `onRetry` with default implementation. This method is called when retry logic is triggered and mainly used to reset data source if applicable.
394+
395+
396+
**Signatures**
397+
```java
398+
CompletableFuture<InsertResponse> insert(String tableName, // name of destination table
399+
DataStreamWriter writer, // data writer instance
400+
ClickHouseFormat format, // data format in which the writer encodes data
401+
InsertSettings settings) // operation settings
402+
```
403+
404+
**Parameters**
405+
406+
`tableName` - name of the target table.
407+
408+
`writer` - data writer instance.
409+
410+
`format` - data format in which the writer encodes data.
411+
412+
`settings` - request settings.
413+
414+
**Return value**
415+
416+
Future of `InsertResponse` type - the result of the operation and additional information like server side metrics.
417+
418+
**Examples**
419+
420+
Writing a collection of JSON objects encoded as string values using `JSONEachRow` format:
421+
```java showLineNumbers
422+
423+
final int EXECUTE_CMD_TIMEOUT = 10; // seconds
424+
final String tableName = "events";
425+
final String tableCreate = "CREATE TABLE \"" + tableName + "\" " +
426+
" (name String, " +
427+
" v1 Float32, " +
428+
" v2 Float32, " +
429+
" attrs Nullable(String), " +
430+
" corrected_time DateTime('UTC') DEFAULT now()," +
431+
" special_attr Nullable(Int8) DEFAULT -1)" +
432+
" Engine = MergeTree ORDER by ()";
433+
434+
client.execute("DROP TABLE IF EXISTS " + tableName).get(EXECUTE_CMD_TIMEOUT, TimeUnit.SECONDS);
435+
client.execute(createTableSQL).get(EXECUTE_CMD_TIMEOUT, TimeUnit.SECONDS);
436+
437+
String correctedTime = Instant.now().atZone(ZoneId.of("UTC")).format(DataTypeUtils.DATETIME_FORMATTER);
438+
String[] rows = new String[] {
439+
"{ \"name\": \"foo1\", \"v1\": 0.3, \"v2\": 0.6, \"attrs\": \"a=1,b=2,c=5\", \"corrected_time\": \"" + correctedTime + "\", \"special_attr\": 10}",
440+
"{ \"name\": \"foo1\", \"v1\": 0.3, \"v2\": 0.6, \"attrs\": \"a=1,b=2,c=5\", \"corrected_time\": \"" + correctedTime + "\"}",
441+
"{ \"name\": \"foo1\", \"v1\": 0.3, \"v2\": 0.6, \"attrs\": \"a=1,b=2,c=5\" }",
442+
"{ \"name\": \"foo1\", \"v1\": 0.3, \"v2\": 0.6 }",
443+
};
444+
445+
446+
try (InsertResponse response = client.insert(tableName, out -> {
447+
// writing raw bytes
448+
for (String row : rows) {
449+
out.write(row.getBytes());
450+
}
451+
452+
}, ClickHouseFormat.JSONEachRow, new InsertSettings()).get()) {
453+
454+
System.out.println("Rows written: " + response.getWrittenRows());
455+
}
456+
457+
```
458+
459+
Writing already compressed data:
460+
```java showLineNumbers
461+
String tableName = "very_long_table_name_with_uuid_" + UUID.randomUUID().toString().replace('-', '_');
462+
String tableCreate = "CREATE TABLE \"" + tableName + "\" " +
463+
" (name String, " +
464+
" v1 Float32, " +
465+
" v2 Float32, " +
466+
" attrs Nullable(String), " +
467+
" corrected_time DateTime('UTC') DEFAULT now()," +
468+
" special_attr Nullable(Int8) DEFAULT -1)" +
469+
" Engine = MergeTree ORDER by ()";
470+
471+
client.execute("DROP TABLE IF EXISTS " + tableName).get(EXECUTE_CMD_TIMEOUT, TimeUnit.SECONDS);
472+
client.execute(createTableSQL).get(EXECUTE_CMD_TIMEOUT, TimeUnit.SECONDS);
473+
474+
String correctedTime = Instant.now().atZone(ZoneId.of("UTC")).format(DataTypeUtils.DATETIME_FORMATTER);
475+
String[] data = new String[] {
476+
"{ \"name\": \"foo1\", \"v1\": 0.3, \"v2\": 0.6, \"attrs\": \"a=1,b=2,c=5\", \"corrected_time\": \"" + correctedTime + "\", \"special_attr\": 10}",
477+
"{ \"name\": \"foo1\", \"v1\": 0.3, \"v2\": 0.6, \"attrs\": \"a=1,b=2,c=5\", \"corrected_time\": \"" + correctedTime + "\"}",
478+
"{ \"name\": \"foo1\", \"v1\": 0.3, \"v2\": 0.6, \"attrs\": \"a=1,b=2,c=5\" }",
479+
"{ \"name\": \"foo1\", \"v1\": 0.3, \"v2\": 0.6 }",
480+
};
481+
482+
483+
// This step is only for showcase. Real application would have already compressed data.
484+
byte[][] compressedData = new byte[data.length][];
485+
for (int i = 0 ; i < data.length; i++) {
486+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
487+
GZIPOutputStream gz = new GZIPOutputStream(baos);
488+
gz.write(data[i].getBytes(StandardCharsets.UTF_8));
489+
gz.finish();
490+
compressedData[i] = baos.toByteArray();
491+
}
492+
493+
InsertSettings insertSettings = new InsertSettings()
494+
.appCompressedData(true, "gzip"); // defining compression algorithm (sent via HTTP headers)
495+
496+
try (InsertResponse response = client.insert(tableName, out -> {
497+
// Writing data
498+
for (byte[] row : compressedData) {
499+
out.write(row);
500+
}
501+
}, ClickHouseFormat.JSONEachRow, insertSettings).get()) {
502+
System.out.println("Rows written: " + response.getWrittenRows());
503+
}
504+
505+
```
506+
385507
### InsertSettings {#insertsettings}
386508

387509
Configuration options for insert operations.

0 commit comments

Comments
 (0)