Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support config client name #362

Merged
merged 1 commit into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,29 @@

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;
import java.sql.*;
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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,10 +40,11 @@ public class ClickHouseConfig implements Serializable {
private final String charset; // use String because Charset is not serializable
private final Map<SettingKey, Serializable> 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<SettingKey, Serializable> settings) {
String charset, String clientName, Map<SettingKey, Serializable> settings) {
this.host = host;
this.port = port;
this.database = database;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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<SettingKey, Serializable> entry : settings.entrySet()) {
Expand Down Expand Up @@ -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<SettingKey, Serializable> settings) {
return Builder.builder(this)
.withSettings(settings)
Expand Down Expand Up @@ -182,6 +196,7 @@ public static final class Builder {
private Duration queryTimeout;
private boolean tcpKeepAlive;
private Charset charset;
private String clientName;
private Map<SettingKey, Serializable> settings = new HashMap<>();

private Builder() {
Expand All @@ -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());
}

Expand Down Expand Up @@ -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<SettingKey, Serializable> settings) {
this.settings = settings;
return this;
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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");
Expand All @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion examples/src/main/java/examples/SimpleQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down