@@ -235,7 +235,7 @@ public void insertRawDataSimple(int numberOfRecords) throws Exception {
235235 public void testInsertMetricsOperationId () throws Exception {
236236 final String tableName = "insert_metrics_test" ;
237237 final String createSQL = "CREATE TABLE " + tableName +
238- " (Id UInt32, event_ts Timestamp, name String, p1 Int64, p2 String) ENGINE = MergeTree() ORDER BY ()" ;
238+ " (Id UInt32, event_ts Timestamp, name String, p1 Int64, p2 String) ENGINE = MergeTree() ORDER BY ()" ;
239239 dropTable (tableName );
240240 createTable (createSQL );
241241
@@ -248,13 +248,50 @@ public void testInsertMetricsOperationId() throws Exception {
248248 writer .flush ();
249249
250250 InsertSettings settings = new InsertSettings ()
251- .setQueryId (String .valueOf (UUID .randomUUID ()))
252- .setOperationId (UUID .randomUUID ().toString ());
251+ .setQueryId (String .valueOf (UUID .randomUUID ()))
252+ .setOperationId (UUID .randomUUID ().toString ());
253253 InsertResponse response = client .insert (tableName , new ByteArrayInputStream (data .toByteArray ()),
254- ClickHouseFormat .TSV , settings ).get (30 , TimeUnit .SECONDS );
254+ ClickHouseFormat .TSV , settings ).get (30 , TimeUnit .SECONDS );
255255 OperationMetrics metrics = response .getMetrics ();
256256 assertEquals ((int )response .getWrittenRows (), numberOfRecords );
257257 assertEquals (metrics .getQueryId (), settings .getQueryId ());
258258 assertTrue (metrics .getMetric (ClientMetrics .OP_DURATION ).getLong () > 0 );
259259 }
260+
261+ @ Test (groups = { "integration" })
262+ public void testInsertSettingsAddDatabase () throws Exception {
263+ final String tableName = "insert_settings_database_test" ;
264+ final String new_database = "new_database" ;
265+ final String createDatabaseSQL = "CREATE DATABASE " + new_database ;
266+ final String createTableSQL = "CREATE TABLE " + new_database + "." + tableName +
267+ " (Id UInt32, event_ts Timestamp, name String, p1 Int64, p2 String) ENGINE = MergeTree() ORDER BY ()" ;
268+ final String dropDatabaseSQL = "DROP DATABASE IF EXISTS " + new_database ;
269+
270+ try (ClickHouseClient client = ClickHouseClient .builder ().config (new ClickHouseConfig ())
271+ .nodeSelector (ClickHouseNodeSelector .of (ClickHouseProtocol .HTTP ))
272+ .build ()) {
273+ client .read (getServer (ClickHouseProtocol .HTTP )).query (dropDatabaseSQL ).executeAndWait ().close ();
274+ client .read (getServer (ClickHouseProtocol .HTTP )).query (createDatabaseSQL ).executeAndWait ().close ();
275+ client .read (getServer (ClickHouseProtocol .HTTP )).query (createTableSQL ).executeAndWait ().close ();
276+ }
277+
278+
279+ InsertSettings insertSettings = settings .setInputStreamCopyBufferSize (8198 * 2 )
280+ .setDeduplicationToken (RandomStringUtils .randomAlphabetic (36 ))
281+ .setQueryId (String .valueOf (UUID .randomUUID ()));
282+ insertSettings .setDatabase (new_database );
283+
284+ ByteArrayOutputStream data = new ByteArrayOutputStream ();
285+ PrintWriter writer = new PrintWriter (data );
286+ for (int i = 0 ; i < 1000 ; i ++) {
287+ writer .printf ("%d\t %s\t %s\t %d\t %s\n " , i , "2021-01-01 00:00:00" , "name" + i , i , "p2" );
288+ }
289+ writer .flush ();
290+ InsertResponse response = client .insert (tableName , new ByteArrayInputStream (data .toByteArray ()),
291+ ClickHouseFormat .TSV , insertSettings ).get (30 , TimeUnit .SECONDS );
292+ assertEquals ((int )response .getWrittenRows (), 1000 );
293+
294+ List <GenericRecord > records = client .queryAll ("SELECT * FROM " + new_database + "." + tableName );
295+ assertEquals (records .size (), 1000 );
296+ }
260297}
0 commit comments