Skip to content

Commit 0e646c5

Browse files
author
Paultagoras
committed
Adding stopwatch and tweaking InsertSettings
1 parent 227bb62 commit 0e646c5

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.clickhouse.client.api.query.QueryResponse;
3333
import com.clickhouse.client.api.query.QuerySettings;
3434
import com.clickhouse.data.format.BinaryStreamUtils;
35+
import org.apache.commons.lang3.time.StopWatch;
3536
import org.slf4j.Logger;
3637
import org.slf4j.LoggerFactory;
3738
import org.slf4j.MDC;
@@ -247,7 +248,7 @@ public InsertResponse insert(String tableName,
247248
if (data == null || data.isEmpty()) {
248249
throw new IllegalArgumentException("Data cannot be empty");
249250
}
250-
long s1 = System.currentTimeMillis();
251+
StopWatch watch = StopWatch.createStarted();
251252

252253
//Add format to the settings
253254
if (settings == null) {
@@ -271,7 +272,6 @@ public InsertResponse insert(String tableName,
271272
throw new IllegalArgumentException("No serializer found for the given class. Please register() before calling this method.");
272273
}
273274

274-
long s2 = System.currentTimeMillis();
275275
//Call the static .serialize method on the POJOSerializer for each object in the list
276276
for (Object obj : data) {
277277
for (POJOSerializer serializer : serializers) {
@@ -282,9 +282,9 @@ public InsertResponse insert(String tableName,
282282
}
283283
}
284284
}
285-
long s3 = System.currentTimeMillis();
286285

287-
LOG.debug("Total serialization time: {}", s3 - s1);
286+
watch.stop();
287+
LOG.debug("Total serialization time: {}", watch.getTime());
288288
return insert(tableName, new ByteArrayInputStream(stream.toByteArray()), settings);
289289
}
290290

@@ -294,18 +294,18 @@ public InsertResponse insert(String tableName,
294294
public InsertResponse insert(String tableName,
295295
InputStream data,
296296
InsertSettings settings) throws IOException, ClientException {
297-
long s1 = System.currentTimeMillis();
297+
StopWatch watch = StopWatch.createStarted();
298298
InsertResponse response;
299299
try (ClickHouseClient client = createClient()) {
300300
ClickHouseRequest.Mutation request = createMutationRequest(client.write(getServerNode()), tableName, settings)
301-
.format((ClickHouseFormat) settings.getSetting(ClickHouseClientOption.FORMAT.getKey()));
301+
.format(settings.getFormat());
302302

303303
Future<ClickHouseResponse> future;
304304
try(ClickHousePipedOutputStream stream = ClickHouseDataStreamFactory.getInstance().createPipedOutputStream(request.getConfig())) {
305305
future = request.data(stream.getInputStream()).execute();
306306

307307
//Copy the data from the input stream to the output stream
308-
byte[] buffer = new byte[8196];
308+
byte[] buffer = new byte[settings.getInputStreamBatchSize()];
309309
int bytesRead;
310310
while ((bytesRead = data.read(buffer)) != -1) {
311311
stream.write(buffer, 0, bytesRead);
@@ -318,8 +318,8 @@ public InsertResponse insert(String tableName,
318318
}
319319
}
320320

321-
long s2 = System.currentTimeMillis();
322-
LOG.debug("Total insert (InputStream) time: {}", s2 - s1);
321+
watch.stop();
322+
LOG.debug("Total insert (InputStream) time: {}", watch.getTime());
323323
return response;
324324
}
325325

client-v2/src/main/java/com/clickhouse/client/api/insert/InsertSettings.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@ public class InsertSettings {
1111

1212
public InsertSettings() {
1313
rawSettings = new HashMap<>();
14+
setDefaults();
1415
}
1516

1617
public InsertSettings(Map<String, Object> settings) {
1718
rawSettings = new HashMap<>();
19+
setDefaults();
1820
rawSettings.putAll(settings);
1921
}
2022

23+
private void setDefaults() {// Default settings, for now a very small list
24+
this.setInputStreamBatchSize(8196);
25+
}
26+
2127
public Object getSetting(String option) {
2228
return rawSettings.get(option);
2329
}
@@ -27,18 +33,38 @@ public void setSetting(String option, Object value) {
2733
}
2834

2935

36+
public ClickHouseFormat getFormat() {
37+
return (ClickHouseFormat) rawSettings.get(ClickHouseClientOption.FORMAT.getKey());
38+
}
3039
public InsertSettings setFormat(ClickHouseFormat format) {
3140
rawSettings.put(ClickHouseClientOption.FORMAT.getKey(), format);
3241
return this;
3342
}
3443

44+
45+
public String getDeduplicationToken() {
46+
return (String) rawSettings.get("insert_deduplication_token");
47+
}
3548
public InsertSettings setDeduplicationToken(String deduplicationToken) {
3649
rawSettings.put("insert_deduplication_token", deduplicationToken);
3750
return this;
3851
}
3952

53+
54+
public String getQueryId() {
55+
return (String) rawSettings.get("query_id");
56+
}
4057
public InsertSettings setQueryId(String queryId) {
4158
rawSettings.put("query_id", queryId);
4259
return this;
4360
}
61+
62+
63+
public int getInputStreamBatchSize() {
64+
return (int) rawSettings.get("input_stream_batch_size");
65+
}
66+
public InsertSettings setInputStreamBatchSize(int inputStreamBatchSize) {
67+
rawSettings.put("input_stream_batch_size", inputStreamBatchSize);
68+
return this;
69+
}
4470
}

0 commit comments

Comments
 (0)