From a8e3388a0433b02009efc74b721f08bd772ef587 Mon Sep 17 00:00:00 2001 From: marlondu Date: Sun, 15 Aug 2021 15:51:16 +0800 Subject: [PATCH] support config client name --- .../housepower/jdbc/ClickHouseConnection.java | 15 +++++----- .../housepower/settings/ClickHouseConfig.java | 28 +++++++++++++++++-- .../housepower/settings/SettingKey.java | 6 ++++ .../jdbc/ClickHouseDatabaseMetadataITest.java | 2 +- .../settings/ClickHouseConfigTest.java | 5 ++-- .../src/main/java/examples/SimpleQuery.java | 2 +- 6 files changed, 44 insertions(+), 14 deletions(-) diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/ClickHouseConnection.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/ClickHouseConnection.java index 1e6fafd1..1d5665ae 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/ClickHouseConnection.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/jdbc/ClickHouseConnection.java @@ -14,22 +14,22 @@ package com.github.housepower.jdbc; -import com.github.housepower.client.SessionState; import com.github.housepower.client.NativeClient; import com.github.housepower.client.NativeContext; +import com.github.housepower.client.SessionState; import com.github.housepower.data.Block; import com.github.housepower.data.DataTypeFactory; -import com.github.housepower.misc.Validate; -import com.github.housepower.protocol.HelloResponse; -import com.github.housepower.stream.QueryResult; -import com.github.housepower.settings.ClickHouseConfig; -import com.github.housepower.settings.ClickHouseDefines; import com.github.housepower.jdbc.statement.ClickHousePreparedInsertStatement; import com.github.housepower.jdbc.statement.ClickHousePreparedQueryStatement; import com.github.housepower.jdbc.statement.ClickHouseStatement; import com.github.housepower.jdbc.wrapper.SQLConnection; import com.github.housepower.log.Logger; import com.github.housepower.log.LoggerFactory; +import com.github.housepower.misc.Validate; +import com.github.housepower.protocol.HelloResponse; +import com.github.housepower.settings.ClickHouseConfig; +import com.github.housepower.settings.ClickHouseDefines; +import com.github.housepower.stream.QueryResult; import javax.annotation.Nullable; import java.net.InetSocketAddress; @@ -37,7 +37,6 @@ import java.time.Duration; import java.time.ZoneId; import java.util.HashMap; -import java.util.Locale; import java.util.Map; import java.util.Properties; import java.util.concurrent.Executor; @@ -313,7 +312,7 @@ private static NativeContext createNativeContext(ClickHouseConfig configure) thr private static NativeContext.ClientContext clientContext(NativeClient nativeClient, ClickHouseConfig configure) throws SQLException { Validate.isTrue(nativeClient.address() instanceof InetSocketAddress); InetSocketAddress address = (InetSocketAddress) nativeClient.address(); - String clientName = String.format(Locale.ROOT, "%s %s", ClickHouseDefines.NAME, "client"); + String clientName = configure.clientName(); String initialAddress = "[::ffff:127.0.0.1]:0"; return new NativeContext.ClientContext(initialAddress, address.getHostName(), clientName); } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/settings/ClickHouseConfig.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/settings/ClickHouseConfig.java index 4b34ec1c..e04156d1 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/settings/ClickHouseConfig.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/settings/ClickHouseConfig.java @@ -23,6 +23,7 @@ import java.nio.charset.Charset; import java.time.Duration; import java.util.HashMap; +import java.util.Locale; import java.util.Map; import java.util.Properties; @@ -39,10 +40,11 @@ public class ClickHouseConfig implements Serializable { private final String charset; // use String because Charset is not serializable private final Map settings; private final boolean tcpKeepAlive; + private final String clientName; private ClickHouseConfig(String host, int port, String database, String user, String password, Duration queryTimeout, Duration connectTimeout, boolean tcpKeepAlive, - String charset, Map settings) { + String charset, String clientName, Map settings) { this.host = host; this.port = port; this.database = database; @@ -52,6 +54,7 @@ private ClickHouseConfig(String host, int port, String database, String user, St this.connectTimeout = connectTimeout; this.tcpKeepAlive = tcpKeepAlive; this.charset = charset; + this.clientName = clientName; this.settings = settings; } @@ -87,12 +90,17 @@ public Charset charset() { return Charset.forName(charset); } + public String clientName() { + return this.clientName; + } + public String jdbcUrl() { StringBuilder builder = new StringBuilder(ClickhouseJdbcUrlParser.JDBC_CLICKHOUSE_PREFIX) .append("//").append(host).append(":").append(port).append("/").append(database) .append("?").append(SettingKey.query_timeout.name()).append("=").append(queryTimeout.getSeconds()) .append("&").append(SettingKey.connect_timeout.name()).append("=").append(connectTimeout.getSeconds()) .append("&").append(SettingKey.charset.name()).append("=").append(charset) + .append("&").append(SettingKey.client_name.name()).append("=").append(clientName) .append("&").append(SettingKey.tcp_keep_alive.name()).append("=").append(tcpKeepAlive); for (Map.Entry entry : settings.entrySet()) { @@ -143,6 +151,12 @@ public ClickHouseConfig withCharset(Charset charset) { .build(); } + public ClickHouseConfig withClientName(String clientName) { + return Builder.builder(this) + .clientName(clientName) + .build(); + } + public ClickHouseConfig withSettings(Map settings) { return Builder.builder(this) .withSettings(settings) @@ -182,6 +196,7 @@ public static final class Builder { private Duration queryTimeout; private boolean tcpKeepAlive; private Charset charset; + private String clientName; private Map settings = new HashMap<>(); private Builder() { @@ -202,6 +217,7 @@ public static Builder builder(ClickHouseConfig cfg) { .queryTimeout(cfg.queryTimeout()) .charset(cfg.charset()) .tcpKeepAlive(cfg.tcpKeepAlive()) + .clientName(cfg.clientName()) .withSettings(cfg.settings()); } @@ -265,6 +281,11 @@ public Builder charset(Charset charset) { return this; } + public Builder clientName(String clientName) { + this.withSetting(SettingKey.client_name, clientName); + return this; + } + public Builder settings(Map settings) { this.settings = settings; return this; @@ -293,12 +314,14 @@ public ClickHouseConfig build() { this.queryTimeout = (Duration) this.settings.getOrDefault(SettingKey.query_timeout, Duration.ZERO); this.tcpKeepAlive = (boolean) this.settings.getOrDefault(SettingKey.tcp_keep_alive, false); this.charset = Charset.forName((String) this.settings.getOrDefault(SettingKey.charset, "UTF-8")); + this.clientName = (String) this.settings.getOrDefault(SettingKey.client_name, + String.format(Locale.ROOT, "%s %s", ClickHouseDefines.NAME, "client")); revisit(); purgeSettings(); return new ClickHouseConfig( - host, port, database, user, password, queryTimeout, connectTimeout, tcpKeepAlive, charset.name(), settings); + host, port, database, user, password, queryTimeout, connectTimeout, tcpKeepAlive, charset.name(), clientName, settings); } private void revisit() { @@ -321,6 +344,7 @@ private void purgeSettings() { this.settings.remove(SettingKey.connect_timeout); this.settings.remove(SettingKey.tcp_keep_alive); this.settings.remove(SettingKey.charset); + this.settings.remove(SettingKey.client_name); } } } diff --git a/clickhouse-native-jdbc/src/main/java/com/github/housepower/settings/SettingKey.java b/clickhouse-native-jdbc/src/main/java/com/github/housepower/settings/SettingKey.java index 40ef619e..3fd60684 100644 --- a/clickhouse-native-jdbc/src/main/java/com/github/housepower/settings/SettingKey.java +++ b/clickhouse-native-jdbc/src/main/java/com/github/housepower/settings/SettingKey.java @@ -797,6 +797,12 @@ public class SettingKey implements Serializable { .withDescription("Allow Int128, Int256, UInt256 and Decimal256 types") .build(); + public static SettingKey client_name = SettingKey.builder() + .withName("client_name") + .withType(SettingType.UTF8) + .withDescription("identify who you are, it will record in system.query_log") + .build(); + public static SettingKey port = SettingKey.builder() .withName("port") diff --git a/clickhouse-native-jdbc/src/test/java/com/github/housepower/jdbc/ClickHouseDatabaseMetadataITest.java b/clickhouse-native-jdbc/src/test/java/com/github/housepower/jdbc/ClickHouseDatabaseMetadataITest.java index 069e4063..5306439e 100644 --- a/clickhouse-native-jdbc/src/test/java/com/github/housepower/jdbc/ClickHouseDatabaseMetadataITest.java +++ b/clickhouse-native-jdbc/src/test/java/com/github/housepower/jdbc/ClickHouseDatabaseMetadataITest.java @@ -47,7 +47,7 @@ void allTablesAreSelectable() throws Exception { void getURL() throws Exception { withNewConnection(connection -> { DatabaseMetaData dm = connection.getMetaData(); - assertEquals(String.format(Locale.ROOT, "jdbc:clickhouse://%s:%s/default?query_timeout=0&connect_timeout=0&charset=UTF-8&tcp_keep_alive=false", CK_HOST, CK_PORT), + assertEquals(String.format(Locale.ROOT, "jdbc:clickhouse://%s:%s/default?query_timeout=0&connect_timeout=0&charset=UTF-8&client_name=ClickHouse client&tcp_keep_alive=false", CK_HOST, CK_PORT), dm.getURL()); }); } diff --git a/clickhouse-native-jdbc/src/test/java/com/github/housepower/settings/ClickHouseConfigTest.java b/clickhouse-native-jdbc/src/test/java/com/github/housepower/settings/ClickHouseConfigTest.java index cd1b2a04..1d6fad17 100644 --- a/clickhouse-native-jdbc/src/test/java/com/github/housepower/settings/ClickHouseConfigTest.java +++ b/clickhouse-native-jdbc/src/test/java/com/github/housepower/settings/ClickHouseConfigTest.java @@ -38,7 +38,7 @@ public void testDefaultClickHouseConfig() { assertEquals(StandardCharsets.UTF_8, cfg.charset()); assertFalse(cfg.tcpKeepAlive()); assertEquals("default", cfg.database()); - assertEquals("jdbc:clickhouse://127.0.0.1:9000/default?query_timeout=0&connect_timeout=0&charset=UTF-8&tcp_keep_alive=false", + assertEquals("jdbc:clickhouse://127.0.0.1:9000/default?query_timeout=0&connect_timeout=0&charset=UTF-8&client_name=ClickHouse client&tcp_keep_alive=false", cfg.jdbcUrl()); } @@ -47,6 +47,7 @@ public void testClickHouseConfig() { ClickHouseConfig cfg = ClickHouseConfig.Builder.builder() .withJdbcUrl("jdbc:clickhouse://1.2.3.4:8123/db2") .charset("GBK") + .clientName("ck-test") .withSetting(SettingKey.allow_distributed_ddl, true) .build() .withCredentials("user", "passWorD"); @@ -59,7 +60,7 @@ public void testClickHouseConfig() { assertEquals(Charset.forName("GBK"), cfg.charset()); assertFalse(cfg.tcpKeepAlive()); assertEquals("db2", cfg.database()); - assertEquals("jdbc:clickhouse://1.2.3.4:8123/db2?query_timeout=0&connect_timeout=0&charset=GBK&tcp_keep_alive=false&allow_distributed_ddl=true", + assertEquals("jdbc:clickhouse://1.2.3.4:8123/db2?query_timeout=0&connect_timeout=0&charset=GBK&client_name=ck-test&tcp_keep_alive=false&allow_distributed_ddl=true", cfg.jdbcUrl()); } diff --git a/examples/src/main/java/examples/SimpleQuery.java b/examples/src/main/java/examples/SimpleQuery.java index 1f6c7e0f..2c7e952e 100644 --- a/examples/src/main/java/examples/SimpleQuery.java +++ b/examples/src/main/java/examples/SimpleQuery.java @@ -25,7 +25,7 @@ public class SimpleQuery { public static void main(String[] args) throws Exception { - try (Connection connection = DriverManager.getConnection("jdbc:clickhouse://127.0.0.1:9000")) { + try (Connection connection = DriverManager.getConnection("jdbc:clickhouse://127.0.0.1:9000?client_name=ck-example")) { try (Statement stmt = connection.createStatement()) { try (ResultSet rs = stmt.executeQuery( "SELECT (number % 3 + 1) as n, sum(number) FROM numbers(10000000) GROUP BY n")) {