|
7 | 7 | import com.clickhouse.client.api.command.CommandResponse; |
8 | 8 | import com.clickhouse.client.api.command.CommandSettings; |
9 | 9 | import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader; |
| 10 | +import com.clickhouse.client.api.data_formats.NativeFormatReader; |
| 11 | +import com.clickhouse.client.api.data_formats.RowBinaryFormatReader; |
10 | 12 | import com.clickhouse.client.api.data_formats.RowBinaryWithNamesAndTypesFormatReader; |
| 13 | +import com.clickhouse.client.api.data_formats.RowBinaryWithNamesFormatReader; |
11 | 14 | import com.clickhouse.client.api.data_formats.internal.MapBackedRecord; |
12 | 15 | import com.clickhouse.client.api.data_formats.internal.ProcessParser; |
13 | 16 | import com.clickhouse.client.api.enums.Protocol; |
|
36 | 39 | import com.clickhouse.client.config.ClickHouseClientOption; |
37 | 40 | import com.clickhouse.client.config.ClickHouseDefaults; |
38 | 41 | import com.clickhouse.client.http.ClickHouseHttpProto; |
39 | | -import com.clickhouse.client.http.config.ClickHouseHttpOption; |
40 | 42 | import com.clickhouse.data.ClickHouseColumn; |
41 | 43 | import com.clickhouse.data.ClickHouseDataStreamFactory; |
42 | 44 | import com.clickhouse.data.ClickHouseFormat; |
43 | 45 | import com.clickhouse.data.ClickHousePipedOutputStream; |
44 | 46 | import com.clickhouse.data.format.BinaryStreamUtils; |
45 | | -import org.apache.commons.compress.compressors.lz4.BlockLZ4CompressorOutputStream; |
46 | | -import org.apache.commons.compress.compressors.lz4.FramedLZ4CompressorOutputStream; |
47 | 47 | import org.apache.hc.core5.concurrent.DefaultThreadFactory; |
48 | 48 | import org.apache.hc.core5.http.ClassicHttpResponse; |
49 | 49 | import org.apache.hc.core5.http.HttpStatus; |
|
78 | 78 | import java.util.concurrent.Executors; |
79 | 79 | import java.util.concurrent.TimeUnit; |
80 | 80 | import java.util.concurrent.TimeoutException; |
| 81 | +import java.util.function.Supplier; |
81 | 82 |
|
82 | 83 | import static java.time.temporal.ChronoUnit.SECONDS; |
83 | 84 |
|
@@ -680,6 +681,10 @@ private Map<String, String> setDefaults(Map<String, String> userConfig) { |
680 | 681 | userConfig.put(ClickHouseClientOption.SERVER_TIME_ZONE.getKey(), "UTC"); |
681 | 682 | } |
682 | 683 |
|
| 684 | + if (!userConfig.containsKey(ClickHouseClientOption.ASYNC.getKey())) { |
| 685 | + userConfig.put(ClickHouseClientOption.ASYNC.getKey(), "false"); |
| 686 | + } |
| 687 | + |
683 | 688 | return userConfig; |
684 | 689 | } |
685 | 690 | } |
@@ -850,7 +855,7 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data, |
850 | 855 |
|
851 | 856 | settings.setOption(ClickHouseClientOption.FORMAT.getKey(), format.name()); |
852 | 857 | final InsertSettings finalSettings = settings; |
853 | | - CompletableFuture<InsertResponse> future = CompletableFuture.supplyAsync(() -> { |
| 858 | + Supplier<InsertResponse> supplier = () -> { |
854 | 859 | // Selecting some node |
855 | 860 | ClickHouseNode selectedNode = getNextAliveNode(); |
856 | 861 |
|
@@ -902,8 +907,9 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data, |
902 | 907 | } |
903 | 908 | } |
904 | 909 | throw new ClientException("Failed to get table schema: too many retries"); |
905 | | - }, sharedOperationExecutor); |
906 | | - return future; |
| 910 | + }; |
| 911 | + boolean isAsync = MapUtils.getFlag(configuration, settings.getAllSettings(), ClickHouseClientOption.ASYNC.getKey()); |
| 912 | + return isAsync ? CompletableFuture.supplyAsync(supplier, sharedOperationExecutor) : CompletableFuture.completedFuture(supplier.get()); |
907 | 913 | } else { |
908 | 914 | //Create an output stream to write the data to |
909 | 915 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
@@ -972,7 +978,7 @@ public CompletableFuture<InsertResponse> insert(String tableName, |
972 | 978 |
|
973 | 979 | settings.setOption(ClickHouseClientOption.FORMAT.getKey(), format.name()); |
974 | 980 | final InsertSettings finalSettings = settings; |
975 | | - CompletableFuture<InsertResponse> future = CompletableFuture.supplyAsync(() -> { |
| 981 | + Supplier<InsertResponse> supplier = () -> { |
976 | 982 | // Selecting some node |
977 | 983 | ClickHouseNode selectedNode = getNextAliveNode(); |
978 | 984 |
|
@@ -1029,8 +1035,9 @@ public CompletableFuture<InsertResponse> insert(String tableName, |
1029 | 1035 | } |
1030 | 1036 | } |
1031 | 1037 | throw new ClientException("Failed to insert data: too many retries"); |
1032 | | - }, sharedOperationExecutor); |
1033 | | - return future; |
| 1038 | + }; |
| 1039 | + boolean isAsync = MapUtils.getFlag(configuration, settings.getAllSettings(), ClickHouseClientOption.ASYNC.getKey()); |
| 1040 | + return isAsync ? CompletableFuture.supplyAsync(supplier, sharedOperationExecutor) : CompletableFuture.completedFuture(supplier.get()); |
1034 | 1041 | } else { |
1035 | 1042 | CompletableFuture<InsertResponse> responseFuture = new CompletableFuture<>(); |
1036 | 1043 |
|
@@ -1145,7 +1152,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec |
1145 | 1152 | settings.setOption("statement_params", queryParams); |
1146 | 1153 | } |
1147 | 1154 | final QuerySettings finalSettings = settings; |
1148 | | - CompletableFuture<QueryResponse> future = CompletableFuture.supplyAsync(() -> { |
| 1155 | + Supplier<QueryResponse> supplier = () -> { |
1149 | 1156 | // Selecting some node |
1150 | 1157 | ClickHouseNode selectedNode = getNextAliveNode(); |
1151 | 1158 | for (int i = 0; i <= maxRetries; i++) { |
@@ -1180,8 +1187,9 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec |
1180 | 1187 | } |
1181 | 1188 | } |
1182 | 1189 | throw new ClientException("Failed to get table schema: too many retries"); |
1183 | | - }, sharedOperationExecutor); |
1184 | | - return future; |
| 1190 | + }; |
| 1191 | + boolean isAsync = MapUtils.getFlag(configuration, settings.getAllSettings(), ClickHouseClientOption.ASYNC.getKey()); |
| 1192 | + return isAsync ? CompletableFuture.supplyAsync(supplier, sharedOperationExecutor) : CompletableFuture.completedFuture(supplier.get()); |
1185 | 1193 | } else { |
1186 | 1194 | ClickHouseRequest<?> request = oldClient.read(getServerNode()); |
1187 | 1195 | request.options(SettingsConverter.toRequestOptions(settings.getAllSettings())); |
@@ -1361,6 +1369,39 @@ public CompletableFuture<CommandResponse> execute(String sql) { |
1361 | 1369 | }, sharedOperationExecutor); |
1362 | 1370 | } |
1363 | 1371 |
|
| 1372 | + /** |
| 1373 | + * <p>Create an instance of {@link ClickHouseBinaryFormatReader} based on response. Table schema is option and only |
| 1374 | + * required for {@link ClickHouseFormat#RowBinaryWithNames}, {@link ClickHouseFormat#RowBinary}. |
| 1375 | + * Format {@link ClickHouseFormat#RowBinaryWithDefaults} is not supported for output (read operations).</p> |
| 1376 | + * @param response |
| 1377 | + * @param schema |
| 1378 | + * @return |
| 1379 | + */ |
| 1380 | + public static ClickHouseBinaryFormatReader newBinaryFormatReader(QueryResponse response, TableSchema schema) { |
| 1381 | + ClickHouseBinaryFormatReader reader = null; |
| 1382 | + switch (response.getFormat()) { |
| 1383 | + case Native: |
| 1384 | + reader = new NativeFormatReader(response.getInputStream(), response.getSettings()); |
| 1385 | + break; |
| 1386 | + case RowBinaryWithNamesAndTypes: |
| 1387 | + reader = new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream(), response.getSettings()); |
| 1388 | + break; |
| 1389 | + case RowBinaryWithNames: |
| 1390 | + reader = new RowBinaryWithNamesFormatReader(response.getInputStream(), response.getSettings(), schema); |
| 1391 | + break; |
| 1392 | + case RowBinary: |
| 1393 | + reader = new RowBinaryFormatReader(response.getInputStream(), response.getSettings(), schema); |
| 1394 | + break; |
| 1395 | + default: |
| 1396 | + throw new IllegalArgumentException("Unsupported format: " + response.getFormat()); |
| 1397 | + } |
| 1398 | + return reader; |
| 1399 | + } |
| 1400 | + |
| 1401 | + public static ClickHouseBinaryFormatReader newBinaryFormatReader(QueryResponse response) { |
| 1402 | + return newBinaryFormatReader(response, null); |
| 1403 | + } |
| 1404 | + |
1364 | 1405 | private String startOperation() { |
1365 | 1406 | String operationId = UUID.randomUUID().toString(); |
1366 | 1407 | globalClientStats.put(operationId, new ClientStatisticsHolder()); |
|
0 commit comments