diff --git a/client-v2/src/main/java/com/clickhouse/client/api/insert/InsertResponse.java b/client-v2/src/main/java/com/clickhouse/client/api/insert/InsertResponse.java index 72582bb1e..70d479ef9 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/insert/InsertResponse.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/insert/InsertResponse.java @@ -85,4 +85,12 @@ public long getServerTime() { public long getResultRows() { return operationMetrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(); } + + /** + * Alias for {@link OperationMetrics#getQueryId()} + * @return number of returned bytes + */ + public String getQueryId() { + return operationMetrics.getQueryId(); + } } diff --git a/client-v2/src/main/java/com/clickhouse/client/api/metrics/Metric.java b/client-v2/src/main/java/com/clickhouse/client/api/metrics/Metric.java index e9628e13c..b0b17b93d 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/metrics/Metric.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/metrics/Metric.java @@ -7,4 +7,5 @@ public interface Metric { * @return value of the metric as a long */ long getLong(); + } diff --git a/client-v2/src/main/java/com/clickhouse/client/api/metrics/OperationMetrics.java b/client-v2/src/main/java/com/clickhouse/client/api/metrics/OperationMetrics.java index 723a8a186..52c6b1313 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/metrics/OperationMetrics.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/metrics/OperationMetrics.java @@ -16,6 +16,7 @@ public class OperationMetrics { public Map metrics = new HashMap<>(); + private String queryId; private final ClientStatisticsHolder clientStatistics; @@ -31,6 +32,10 @@ public Metric getMetric(ClientMetrics metric) { return metrics.get(metric.getKey()); } + public String getQueryId() { + return queryId; + } + public void operationComplete(ClickHouseResponseSummary serverStats) { for (Map.Entry sw : clientStatistics.getStopWatches().entrySet()) { sw.getValue().stop(); @@ -43,11 +48,13 @@ public void operationComplete(ClickHouseResponseSummary serverStats) { metrics.put(ServerMetrics.NUM_BYTES_WRITTEN.getKey(), new Gauge(serverStats.getWrittenBytes())); metrics.put(ServerMetrics.RESULT_ROWS.getKey(), new Gauge(serverStats.getResultRows())); metrics.put(ServerMetrics.ELAPSED_TIME.getKey(), new Gauge(serverStats.getElapsedTime())); + this.queryId = serverStats.getQueryId(); } @Override public String toString() { return "OperationStatistics{" + + "\"queryId\"=\"" + queryId + "\", " + "\"metrics\"=" + metrics + '}'; } diff --git a/client-v2/src/main/java/com/clickhouse/client/api/query/QueryResponse.java b/client-v2/src/main/java/com/clickhouse/client/api/query/QueryResponse.java index 78dd026e6..15b19cbdc 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/query/QueryResponse.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/query/QueryResponse.java @@ -152,4 +152,12 @@ public long getServerTime() { public long getResultRows() { return operationMetrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(); } + + /** + * Alias for {@link OperationMetrics#getQueryId()} + * @return query id of the request + */ + public String getQueryId() { + return operationMetrics.getQueryId(); + } } diff --git a/client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java b/client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java index c51feaf65..9fe2286b2 100644 --- a/client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java +++ b/client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java @@ -53,10 +53,20 @@ private void createTable(String tableQuery) throws ClickHouseException { } } + private void dropTable(String tableName) throws ClickHouseException { + try (ClickHouseClient client = ClickHouseClient.builder().config(new ClickHouseConfig()) + .nodeSelector(ClickHouseNodeSelector.of(ClickHouseProtocol.HTTP)) + .build()) { + String tableQuery = "DROP TABLE IF EXISTS " + tableName; + client.read(getServer(ClickHouseProtocol.HTTP)).query(tableQuery).executeAndWait().close(); + } + } + @Test(groups = { "integration" }, enabled = true) public void insertSimplePOJOs() throws Exception { String tableName = "simple_pojo_table"; String createSQL = SamplePOJO.generateTableCreateSQL(tableName); + String uuid = UUID.randomUUID().toString(); System.out.println(createSQL); createTable(createSQL); client.register(SamplePOJO.class, client.getTableSchema(tableName, "default")); @@ -65,6 +75,7 @@ public void insertSimplePOJOs() throws Exception { for (int i = 0; i < 1000; i++) { simplePOJOs.add(new SamplePOJO()); } + settings.setQueryId(uuid); InsertResponse response = client.insert(tableName, simplePOJOs, settings).get(30, TimeUnit.SECONDS); OperationMetrics metrics = response.getMetrics(); @@ -72,5 +83,8 @@ public void insertSimplePOJOs() throws Exception { assertEquals(simplePOJOs.size(), response.getWrittenRows()); assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0); assertTrue(metrics.getMetric(ClientMetrics.OP_SERIALIZATION).getLong() > 0); + assertEquals(metrics.getQueryId(), uuid); + assertEquals(response.getQueryId(), uuid); + dropTable(tableName); } } diff --git a/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java b/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java index 4245be969..0eb028134 100644 --- a/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java +++ b/client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java @@ -915,8 +915,9 @@ public void testDataTypes(List columns, List> valueGene public void testQueryMetrics() throws Exception { prepareDataSet(DATASET_TABLE, DATASET_COLUMNS, DATASET_VALUE_GENERATORS, 10); + String uuid = UUID.randomUUID().toString(); QuerySettings settings = new QuerySettings() - .setFormat(ClickHouseFormat.TabSeparated); + .setFormat(ClickHouseFormat.TabSeparated).setQueryId(uuid); QueryResponse response = client.query("SELECT * FROM " + DATASET_TABLE + " LIMIT 3", settings).get(); @@ -947,6 +948,8 @@ public void testQueryMetrics() throws Exception { Assert.assertEquals(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(), rowsToInsert); Assert.assertEquals(response.getReadRows(), rowsToInsert); Assert.assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0); + Assert.assertEquals(metrics.getQueryId(), uuid); + Assert.assertEquals(response.getQueryId(), uuid); }