diff --git a/client-v2/src/main/java/com/clickhouse/client/api/Client.java b/client-v2/src/main/java/com/clickhouse/client/api/Client.java index 9f67559fa..31228fc37 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/Client.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/Client.java @@ -19,6 +19,7 @@ import com.clickhouse.client.api.insert.POJOSerializer; import com.clickhouse.client.api.insert.SerializerNotFoundException; import com.clickhouse.client.api.internal.ClientStatisticsHolder; +import com.clickhouse.client.api.internal.ClientV1AdaptorHelper; import com.clickhouse.client.api.internal.SerializerUtils; import com.clickhouse.client.api.internal.SettingsConverter; import com.clickhouse.client.api.internal.TableSchemaParser; @@ -30,6 +31,7 @@ import com.clickhouse.client.api.query.QuerySettings; import com.clickhouse.client.api.query.Records; import com.clickhouse.client.config.ClickHouseClientOption; +import com.clickhouse.config.ClickHouseOption; import com.clickhouse.data.ClickHouseColumn; import com.clickhouse.data.ClickHouseDataStreamFactory; import com.clickhouse.data.ClickHouseFormat; @@ -42,6 +44,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; @@ -430,10 +433,11 @@ public boolean ping() { public boolean ping(long timeout) { ValidationUtils.checkRange(timeout, TimeUnit.SECONDS.toMillis(1), TimeUnit.MINUTES.toMillis(10), "timeout"); - ClickHouseClient clientPing = ClickHouseClient.newInstance(ClickHouseProtocol.HTTP); - return clientPing.ping(getServerNode(), Math.toIntExact(timeout)); - } + try (ClickHouseClient client = ClientV1AdaptorHelper.createClient(configuration)) { + return client.ping(getServerNode(), Math.toIntExact(timeout)); + } + } /** *

Registers a POJO class and maps its fields to a table schema

@@ -615,8 +619,9 @@ public CompletableFuture insert(String tableName, CompletableFuture responseFuture = new CompletableFuture<>(); - try (ClickHouseClient client = createClient()) { - ClickHouseRequest.Mutation request = createMutationRequest(client.write(getServerNode()), tableName, settings).format(format); + try (ClickHouseClient client = ClientV1AdaptorHelper.createClient(configuration)) { + ClickHouseRequest.Mutation request = ClientV1AdaptorHelper + .createMutationRequest(client.write(getServerNode()), tableName, settings, configuration).format(format); CompletableFuture future = null; try(ClickHousePipedOutputStream stream = ClickHouseDataStreamFactory.getInstance().createPipedOutputStream(request.getConfig())) { future = request.data(stream.getInputStream()).execute(); @@ -708,10 +713,8 @@ public CompletableFuture query(String sqlQuery, Map request = client.read(getServerNode()); - - request.options(SettingsConverter.toRequestOptions(settings.getAllSettings())); request.settings(SettingsConverter.toRequestSettings(settings.getAllSettings(), queryParams)); request.query(sqlQuery, settings.getQueryId()); @@ -762,10 +765,8 @@ public CompletableFuture queryRecords(String sqlQuery, QuerySettings se settings.setFormat(ClickHouseFormat.RowBinaryWithNamesAndTypes); ClientStatisticsHolder clientStats = new ClientStatisticsHolder(); clientStats.start("query"); - ClickHouseClient client = createClient(); + ClickHouseClient client = ClientV1AdaptorHelper.createClient(configuration); ClickHouseRequest request = client.read(getServerNode()); - - request.options(SettingsConverter.toRequestOptions(settings.getAllSettings())); request.settings(SettingsConverter.toRequestSettings(settings.getAllSettings(), null)); request.query(sqlQuery, settings.getQueryId()); @@ -836,8 +837,8 @@ public TableSchema getTableSchema(String table) { * @return {@code TableSchema} - Schema of the table */ public TableSchema getTableSchema(String table, String database) { - try (ClickHouseClient clientQuery = createClient()) { - ClickHouseRequest request = clientQuery.read(getServerNode()); + try (ClickHouseClient clientQuery = ClientV1AdaptorHelper.createClient(configuration)) { + ClickHouseRequest request = clientQuery.read(getServerNode()); // XML - because java has a built-in XML parser. Will consider CSV later. request.query("DESCRIBE TABLE " + table + " FORMAT " + ClickHouseFormat.TSKV.name()); try { @@ -848,31 +849,6 @@ public TableSchema getTableSchema(String table, String database) { } } - private ClickHouseClient createClient() { - ClickHouseConfig clientConfig = new ClickHouseConfig(); - ClickHouseClientBuilder clientV1 = ClickHouseClient.builder() - .config(clientConfig) - .nodeSelector(ClickHouseNodeSelector.of(ClickHouseProtocol.HTTP)); - return clientV1.build(); - } - - private ClickHouseRequest.Mutation createMutationRequest(ClickHouseRequest.Mutation request, String tableName, InsertSettings settings) { - if (settings == null) return request.table(tableName); - - if (settings.getQueryId() != null) {//This has to be handled separately - request.table(tableName, settings.getQueryId()); - } else { - request.table(tableName); - } - - //For each setting, set the value in the request - for (Map.Entry entry : settings.getAllSettings().entrySet()) { - request.set(entry.getKey(), String.valueOf(entry.getValue())); - } - - return request; - } - private String startOperation() { String operationId = UUID.randomUUID().toString(); globalClientStats.put(operationId, new ClientStatisticsHolder()); diff --git a/client-v2/src/main/java/com/clickhouse/client/api/internal/ClientV1AdaptorHelper.java b/client-v2/src/main/java/com/clickhouse/client/api/internal/ClientV1AdaptorHelper.java new file mode 100644 index 000000000..b7f351eb5 --- /dev/null +++ b/client-v2/src/main/java/com/clickhouse/client/api/internal/ClientV1AdaptorHelper.java @@ -0,0 +1,69 @@ +package com.clickhouse.client.api.internal; + +import com.clickhouse.client.ClickHouseClient; +import com.clickhouse.client.ClickHouseClientBuilder; +import com.clickhouse.client.ClickHouseConfig; +import com.clickhouse.client.ClickHouseNodeSelector; +import com.clickhouse.client.ClickHouseProtocol; +import com.clickhouse.client.ClickHouseRequest; +import com.clickhouse.client.api.insert.InsertSettings; +import com.clickhouse.client.config.ClickHouseClientOption; +import com.clickhouse.client.config.ClickHouseProxyType; +import com.clickhouse.config.ClickHouseOption; + +import java.io.Serializable; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class ClientV1AdaptorHelper { + + private static void copyProxySettings(Map target, Map config) { + ClickHouseClientOption opt = ClickHouseClientOption.PROXY_HOST; + String value = config.get(opt.getKey()); + if (value != null) { + target.put(opt, value); + } + opt = ClickHouseClientOption.PROXY_PORT; + value = config.get(opt.getKey()); + if (value != null) { + target.put(opt, Integer.parseInt(value)); + } + opt = ClickHouseClientOption.PROXY_TYPE; + value = config.get(opt.getKey()); + if (value != null) { + target.put(opt, ClickHouseProxyType.valueOf(value)); + } + } + + public static ClickHouseClient createClient(Map configuration) { + Map config = new HashMap<>(); + copyProxySettings(config, configuration); + + ClickHouseConfig clientConfig = new ClickHouseConfig(config); + + ClickHouseClientBuilder clientV1 = ClickHouseClient.builder() + .config(clientConfig) + .nodeSelector(ClickHouseNodeSelector.of(ClickHouseProtocol.HTTP)); + return clientV1.build(); + } + + public static ClickHouseRequest.Mutation createMutationRequest(ClickHouseRequest.Mutation request, + String tableName, + InsertSettings settings, + Map configuration) { + if (settings.getQueryId() != null) {//This has to be handled separately + request.table(tableName, settings.getQueryId()); + } else { + request.table(tableName); + } + + Map opSettings = settings == null ? Collections.emptyMap() : settings.getAllSettings(); + //For each setting, set the value in the request + for (Map.Entry entry : opSettings.entrySet()) { + request.set(entry.getKey(), String.valueOf(entry.getValue())); + } + + return request; + } +}