@@ -1425,9 +1425,23 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data,
1425
1425
* @return {@code CompletableFuture<InsertResponse>} - a promise to insert response
1426
1426
*/
1427
1427
public CompletableFuture <InsertResponse > insert (String tableName , InputStream data , ClickHouseFormat format ) {
1428
- return insert (tableName , data , format , new InsertSettings ());
1428
+ return insert (tableName , Collections . emptyList (), data , format , new InsertSettings ());
1429
1429
}
1430
1430
1431
+ /**
1432
+ * <p>Sends write request to database. Input data is read from the input stream.</p>
1433
+ *
1434
+ * @param tableName - destination table name
1435
+ * @param data - data stream to insert
1436
+ * @param format - format of the data in the stream
1437
+ * @return {@code CompletableFuture<InsertResponse>} - a promise to insert response
1438
+ */
1439
+ public CompletableFuture <InsertResponse > insert (String tableName , List <String > columnNames , InputStream data , ClickHouseFormat format ) {
1440
+ return insert (tableName , columnNames , data , format , new InsertSettings ());
1441
+ }
1442
+
1443
+
1444
+
1431
1445
/**
1432
1446
* Sends write request to database. Input data is read from the input stream.
1433
1447
*
@@ -1441,6 +1455,23 @@ public CompletableFuture<InsertResponse> insert(String tableName,
1441
1455
InputStream data ,
1442
1456
ClickHouseFormat format ,
1443
1457
InsertSettings settings ) {
1458
+ return insert (tableName , Collections .emptyList (), data , format , settings );
1459
+ }
1460
+ /**
1461
+ * Sends write request to database. Input data is read from the input stream.
1462
+ *
1463
+ * @param tableName - destination table name
1464
+ * @param columnNames - list of column names to insert data into. If null or empty, all columns will be used.
1465
+ * @param data - data stream to insert
1466
+ * @param format - format of the data in the stream
1467
+ * @param settings - insert operation settings
1468
+ * @return {@code CompletableFuture<InsertResponse>} - a promise to insert response
1469
+ */
1470
+ public CompletableFuture <InsertResponse > insert (String tableName ,
1471
+ List <String > columnNames ,
1472
+ InputStream data ,
1473
+ ClickHouseFormat format ,
1474
+ InsertSettings settings ) {
1444
1475
1445
1476
final int writeBufferSize = settings .getInputStreamCopyBufferSize () <= 0 ?
1446
1477
Integer .parseInt (configuration .getOrDefault (ClientConfigProperties .CLIENT_NETWORK_BUFFER_SIZE .getKey (),
@@ -1451,7 +1482,7 @@ public CompletableFuture<InsertResponse> insert(String tableName,
1451
1482
throw new IllegalArgumentException ("Buffer size must be greater than 0" );
1452
1483
}
1453
1484
1454
- return insert (tableName , new DataStreamWriter () {
1485
+ return insert (tableName , columnNames , new DataStreamWriter () {
1455
1486
@ Override
1456
1487
public void onOutput (OutputStream out ) throws IOException {
1457
1488
byte [] buffer = new byte [writeBufferSize ];
@@ -1480,11 +1511,29 @@ public void onRetry() throws IOException {
1480
1511
* @return {@code CompletableFuture<InsertResponse>} - a promise to insert response
1481
1512
*/
1482
1513
public CompletableFuture <InsertResponse > insert (String tableName ,
1514
+ DataStreamWriter writer ,
1515
+ ClickHouseFormat format ,
1516
+ InsertSettings settings ) {
1517
+ return insert (tableName , Collections .emptyList (), writer , format , settings );
1518
+ }
1519
+
1520
+ /**
1521
+ * Does an insert request to a server. Data is pushed when a {@link DataStreamWriter#onOutput(OutputStream)} is called.
1522
+ *
1523
+ * @param tableName - target table name
1524
+ * @param columnNames - list of column names to insert data into. If null or empty, all columns will be used.
1525
+ * @param writer - {@link DataStreamWriter} implementation
1526
+ * @param format - source format
1527
+ * @param settings - operation settings
1528
+ * @return {@code CompletableFuture<InsertResponse>} - a promise to insert response
1529
+ */
1530
+ public CompletableFuture <InsertResponse > insert (String tableName ,
1531
+ List <String > columnNames ,
1483
1532
DataStreamWriter writer ,
1484
1533
ClickHouseFormat format ,
1485
1534
InsertSettings settings ) {
1486
1535
1487
- String operationId = ( String ) settings .getOperationId ();
1536
+ String operationId = settings .getOperationId ();
1488
1537
ClientStatisticsHolder clientStats = null ;
1489
1538
if (operationId != null ) {
1490
1539
clientStats = globalClientStats .remove (operationId );
@@ -1509,8 +1558,18 @@ public CompletableFuture<InsertResponse> insert(String tableName,
1509
1558
1510
1559
settings .setOption (ClientConfigProperties .INPUT_OUTPUT_FORMAT .getKey (), format .name ());
1511
1560
final InsertSettings finalSettings = settings ;
1512
- final String sqlStmt = "INSERT INTO " + tableName + " FORMAT " + format .name ();
1513
- finalSettings .serverSetting (ClickHouseHttpProto .QPARAM_QUERY_STMT , sqlStmt );
1561
+
1562
+ StringBuilder sqlStmt = new StringBuilder ("INSERT INTO " ).append (tableName );
1563
+ if (columnNames != null && !columnNames .isEmpty ()) {
1564
+ sqlStmt .append (" (" );
1565
+ for (String columnName : columnNames ) {
1566
+ sqlStmt .append (columnName ).append (", " );
1567
+ }
1568
+ sqlStmt .deleteCharAt (sqlStmt .length () - 2 );
1569
+ sqlStmt .append (")" );
1570
+ }
1571
+ sqlStmt .append (" FORMAT " ).append (format .name ());
1572
+ finalSettings .serverSetting (ClickHouseHttpProto .QPARAM_QUERY_STMT , sqlStmt .toString ());
1514
1573
responseSupplier = () -> {
1515
1574
long startTime = System .nanoTime ();
1516
1575
// Selecting some node
0 commit comments