Skip to content

Commit 1b0a40e

Browse files
authored
Merge pull request #1701 from ClickHouse/add-query-id-v2
Adding query_id to V2 client
2 parents 6539f9c + 9873a5d commit 1b0a40e

File tree

6 files changed

+42
-1
lines changed

6 files changed

+42
-1
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,12 @@ public long getServerTime() {
8585
public long getResultRows() {
8686
return operationMetrics.getMetric(ServerMetrics.RESULT_ROWS).getLong();
8787
}
88+
89+
/**
90+
* Alias for {@link OperationMetrics#getQueryId()}
91+
* @return number of returned bytes
92+
*/
93+
public String getQueryId() {
94+
return operationMetrics.getQueryId();
95+
}
8896
}

client-v2/src/main/java/com/clickhouse/client/api/metrics/Metric.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public interface Metric {
77
* @return value of the metric as a long
88
*/
99
long getLong();
10+
1011
}

client-v2/src/main/java/com/clickhouse/client/api/metrics/OperationMetrics.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
public class OperationMetrics {
1717

1818
public Map<String, Metric> metrics = new HashMap<>();
19+
private String queryId;
1920

2021
private final ClientStatisticsHolder clientStatistics;
2122

@@ -31,6 +32,10 @@ public Metric getMetric(ClientMetrics metric) {
3132
return metrics.get(metric.getKey());
3233
}
3334

35+
public String getQueryId() {
36+
return queryId;
37+
}
38+
3439
public void operationComplete(ClickHouseResponseSummary serverStats) {
3540
for (Map.Entry<String, StopWatch> sw : clientStatistics.getStopWatches().entrySet()) {
3641
sw.getValue().stop();
@@ -43,11 +48,13 @@ public void operationComplete(ClickHouseResponseSummary serverStats) {
4348
metrics.put(ServerMetrics.NUM_BYTES_WRITTEN.getKey(), new Gauge(serverStats.getWrittenBytes()));
4449
metrics.put(ServerMetrics.RESULT_ROWS.getKey(), new Gauge(serverStats.getResultRows()));
4550
metrics.put(ServerMetrics.ELAPSED_TIME.getKey(), new Gauge(serverStats.getElapsedTime()));
51+
this.queryId = serverStats.getQueryId();
4652
}
4753

4854
@Override
4955
public String toString() {
5056
return "OperationStatistics{" +
57+
"\"queryId\"=\"" + queryId + "\", " +
5158
"\"metrics\"=" + metrics +
5259
'}';
5360
}

client-v2/src/main/java/com/clickhouse/client/api/query/QueryResponse.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,12 @@ public long getServerTime() {
152152
public long getResultRows() {
153153
return operationMetrics.getMetric(ServerMetrics.RESULT_ROWS).getLong();
154154
}
155+
156+
/**
157+
* Alias for {@link OperationMetrics#getQueryId()}
158+
* @return query id of the request
159+
*/
160+
public String getQueryId() {
161+
return operationMetrics.getQueryId();
162+
}
155163
}

client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,20 @@ private void createTable(String tableQuery) throws ClickHouseException {
5353
}
5454
}
5555

56+
private void dropTable(String tableName) throws ClickHouseException {
57+
try (ClickHouseClient client = ClickHouseClient.builder().config(new ClickHouseConfig())
58+
.nodeSelector(ClickHouseNodeSelector.of(ClickHouseProtocol.HTTP))
59+
.build()) {
60+
String tableQuery = "DROP TABLE IF EXISTS " + tableName;
61+
client.read(getServer(ClickHouseProtocol.HTTP)).query(tableQuery).executeAndWait().close();
62+
}
63+
}
64+
5665
@Test(groups = { "integration" }, enabled = true)
5766
public void insertSimplePOJOs() throws Exception {
5867
String tableName = "simple_pojo_table";
5968
String createSQL = SamplePOJO.generateTableCreateSQL(tableName);
69+
String uuid = UUID.randomUUID().toString();
6070
System.out.println(createSQL);
6171
createTable(createSQL);
6272
client.register(SamplePOJO.class, client.getTableSchema(tableName, "default"));
@@ -65,12 +75,16 @@ public void insertSimplePOJOs() throws Exception {
6575
for (int i = 0; i < 1000; i++) {
6676
simplePOJOs.add(new SamplePOJO());
6777
}
78+
settings.setQueryId(uuid);
6879
InsertResponse response = client.insert(tableName, simplePOJOs, settings).get(30, TimeUnit.SECONDS);
6980

7081
OperationMetrics metrics = response.getMetrics();
7182
assertEquals(simplePOJOs.size(), metrics.getMetric(ServerMetrics.NUM_ROWS_WRITTEN).getLong());
7283
assertEquals(simplePOJOs.size(), response.getWrittenRows());
7384
assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0);
7485
assertTrue(metrics.getMetric(ClientMetrics.OP_SERIALIZATION).getLong() > 0);
86+
assertEquals(metrics.getQueryId(), uuid);
87+
assertEquals(response.getQueryId(), uuid);
88+
dropTable(tableName);
7589
}
7690
}

client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,9 @@ public void testDataTypes(List<String> columns, List<Supplier<String>> valueGene
915915
public void testQueryMetrics() throws Exception {
916916
prepareDataSet(DATASET_TABLE, DATASET_COLUMNS, DATASET_VALUE_GENERATORS, 10);
917917

918+
String uuid = UUID.randomUUID().toString();
918919
QuerySettings settings = new QuerySettings()
919-
.setFormat(ClickHouseFormat.TabSeparated);
920+
.setFormat(ClickHouseFormat.TabSeparated).setQueryId(uuid);
920921

921922
QueryResponse response = client.query("SELECT * FROM " + DATASET_TABLE + " LIMIT 3", settings).get();
922923

@@ -947,6 +948,8 @@ public void testQueryMetrics() throws Exception {
947948
Assert.assertEquals(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(), rowsToInsert);
948949
Assert.assertEquals(response.getReadRows(), rowsToInsert);
949950
Assert.assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0);
951+
Assert.assertEquals(metrics.getQueryId(), uuid);
952+
Assert.assertEquals(response.getQueryId(), uuid);
950953

951954
}
952955

0 commit comments

Comments
 (0)