1919import com .clickhouse .client .api .insert .POJOSerializer ;
2020import com .clickhouse .client .api .insert .SerializerNotFoundException ;
2121import com .clickhouse .client .api .internal .ClientStatisticsHolder ;
22+ import com .clickhouse .client .api .internal .ClientV1AdaptorHelper ;
2223import com .clickhouse .client .api .internal .SerializerUtils ;
2324import com .clickhouse .client .api .internal .SettingsConverter ;
2425import com .clickhouse .client .api .internal .TableSchemaParser ;
3031import com .clickhouse .client .api .query .QuerySettings ;
3132import com .clickhouse .client .api .query .Records ;
3233import com .clickhouse .client .config .ClickHouseClientOption ;
34+ import com .clickhouse .config .ClickHouseOption ;
3335import com .clickhouse .data .ClickHouseColumn ;
3436import com .clickhouse .data .ClickHouseDataStreamFactory ;
3537import com .clickhouse .data .ClickHouseFormat ;
4244import java .io .ByteArrayOutputStream ;
4345import java .io .IOException ;
4446import java .io .InputStream ;
47+ import java .io .Serializable ;
4548import java .lang .reflect .InvocationTargetException ;
4649import java .lang .reflect .Method ;
4750import java .net .URL ;
@@ -430,10 +433,11 @@ public boolean ping() {
430433 public boolean ping (long timeout ) {
431434 ValidationUtils .checkRange (timeout , TimeUnit .SECONDS .toMillis (1 ), TimeUnit .MINUTES .toMillis (10 ),
432435 "timeout" );
433- ClickHouseClient clientPing = ClickHouseClient .newInstance (ClickHouseProtocol .HTTP );
434- return clientPing .ping (getServerNode (), Math .toIntExact (timeout ));
435- }
436436
437+ try (ClickHouseClient client = ClientV1AdaptorHelper .createClient (configuration )) {
438+ return client .ping (getServerNode (), Math .toIntExact (timeout ));
439+ }
440+ }
437441
438442 /**
439443 * <p>Registers a POJO class and maps its fields to a table schema</p>
@@ -615,8 +619,9 @@ public CompletableFuture<InsertResponse> insert(String tableName,
615619
616620 CompletableFuture <InsertResponse > responseFuture = new CompletableFuture <>();
617621
618- try (ClickHouseClient client = createClient ()) {
619- ClickHouseRequest .Mutation request = createMutationRequest (client .write (getServerNode ()), tableName , settings ).format (format );
622+ try (ClickHouseClient client = ClientV1AdaptorHelper .createClient (configuration )) {
623+ ClickHouseRequest .Mutation request = ClientV1AdaptorHelper
624+ .createMutationRequest (client .write (getServerNode ()), tableName , settings , configuration ).format (format );
620625 CompletableFuture <ClickHouseResponse > future = null ;
621626 try (ClickHousePipedOutputStream stream = ClickHouseDataStreamFactory .getInstance ().createPipedOutputStream (request .getConfig ())) {
622627 future = request .data (stream .getInputStream ()).execute ();
@@ -708,10 +713,8 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
708713 }
709714 ClientStatisticsHolder clientStats = new ClientStatisticsHolder ();
710715 clientStats .start (ClientMetrics .OP_DURATION );
711- ClickHouseClient client = createClient ();
716+ ClickHouseClient client = ClientV1AdaptorHelper . createClient (configuration );
712717 ClickHouseRequest <?> request = client .read (getServerNode ());
713-
714-
715718 request .options (SettingsConverter .toRequestOptions (settings .getAllSettings ()));
716719 request .settings (SettingsConverter .toRequestSettings (settings .getAllSettings (), queryParams ));
717720 request .query (sqlQuery , settings .getQueryId ());
@@ -762,10 +765,8 @@ public CompletableFuture<Records> queryRecords(String sqlQuery, QuerySettings se
762765 settings .setFormat (ClickHouseFormat .RowBinaryWithNamesAndTypes );
763766 ClientStatisticsHolder clientStats = new ClientStatisticsHolder ();
764767 clientStats .start ("query" );
765- ClickHouseClient client = createClient ();
768+ ClickHouseClient client = ClientV1AdaptorHelper . createClient (configuration );
766769 ClickHouseRequest <?> request = client .read (getServerNode ());
767-
768-
769770 request .options (SettingsConverter .toRequestOptions (settings .getAllSettings ()));
770771 request .settings (SettingsConverter .toRequestSettings (settings .getAllSettings (), null ));
771772 request .query (sqlQuery , settings .getQueryId ());
@@ -836,8 +837,8 @@ public TableSchema getTableSchema(String table) {
836837 * @return {@code TableSchema} - Schema of the table
837838 */
838839 public TableSchema getTableSchema (String table , String database ) {
839- try (ClickHouseClient clientQuery = createClient ()) {
840- ClickHouseRequest request = clientQuery .read (getServerNode ());
840+ try (ClickHouseClient clientQuery = ClientV1AdaptorHelper . createClient (configuration )) {
841+ ClickHouseRequest <?> request = clientQuery .read (getServerNode ());
841842 // XML - because java has a built-in XML parser. Will consider CSV later.
842843 request .query ("DESCRIBE TABLE " + table + " FORMAT " + ClickHouseFormat .TSKV .name ());
843844 try {
@@ -848,31 +849,6 @@ public TableSchema getTableSchema(String table, String database) {
848849 }
849850 }
850851
851- private ClickHouseClient createClient () {
852- ClickHouseConfig clientConfig = new ClickHouseConfig ();
853- ClickHouseClientBuilder clientV1 = ClickHouseClient .builder ()
854- .config (clientConfig )
855- .nodeSelector (ClickHouseNodeSelector .of (ClickHouseProtocol .HTTP ));
856- return clientV1 .build ();
857- }
858-
859- private ClickHouseRequest .Mutation createMutationRequest (ClickHouseRequest .Mutation request , String tableName , InsertSettings settings ) {
860- if (settings == null ) return request .table (tableName );
861-
862- if (settings .getQueryId () != null ) {//This has to be handled separately
863- request .table (tableName , settings .getQueryId ());
864- } else {
865- request .table (tableName );
866- }
867-
868- //For each setting, set the value in the request
869- for (Map .Entry <String , Object > entry : settings .getAllSettings ().entrySet ()) {
870- request .set (entry .getKey (), String .valueOf (entry .getValue ()));
871- }
872-
873- return request ;
874- }
875-
876852 private String startOperation () {
877853 String operationId = UUID .randomUUID ().toString ();
878854 globalClientStats .put (operationId , new ClientStatisticsHolder ());
0 commit comments