1313
1414import com .clickhouse .data .ClickHouseOutputStream ;
1515import com .clickhouse .data .ClickHouseWriter ;
16+ import com .clickhouse .jdbc .ClickHouseDataSource ;
17+ import com .zaxxer .hikari .HikariConfig ;
18+ import com .zaxxer .hikari .HikariDataSource ;
1619
1720public class Basic {
1821 static final String TABLE_NAME = "jdbc_example_basic" ;
@@ -125,6 +128,7 @@ public void write(ClickHouseOutputStream output) throws IOException {
125128 // this will be executed in a separate thread
126129 for (int i = 0 ; i < 1_000_000 ; i ++) {
127130 output .writeUnicodeString ("a-" + i );
131+ output .writeBoolean (false ); // non-null
128132 output .writeUnicodeString ("b-" + i );
129133 }
130134 }
@@ -137,11 +141,11 @@ public void write(ClickHouseOutputStream output) throws IOException {
137141
138142 static int connectWithCustomSettings (String url ) throws SQLException {
139143 // comma separated settings
140- String customSettings = "session_check=0,max_query_size=100 " ;
144+ String customSettings = "session_check=0,max_query_size=1000 " ;
141145 Properties properties = new Properties ();
142- // properties.setProperty(ClickHouseHttpClientOption.CUSTOM_PARAMS .getKey(),
146+ // properties.setProperty(ClickHouseClientOption.CUSTOM_SETTINGS .getKey(),
143147 // customSettings);
144- properties .setProperty ("custom_http_params " , customSettings ); // limited to http protocol
148+ properties .setProperty ("custom_settings " , customSettings );
145149 try (Connection conn = getConnection (url , properties );
146150 Statement stmt = conn .createStatement ();
147151 ResultSet rs = stmt .executeQuery ("select 5" )) {
@@ -194,7 +198,7 @@ static void insertByteArray(Connection conn) throws SQLException {
194198 + "ORDER BY (resource_container, event_type, event_subtype) "
195199 + "SETTINGS index_granularity = 8192" );
196200 try (PreparedStatement stmt = conn .prepareStatement (
197- "INSERT INTO t_map VALUES (8481365034795008,1673349039830,'operation-9','a','service', 'bc3e47b8-2b34-4c1a-9004-123656fa0000','b', 'c', 'service-56','d', 'object','e', 'my-value-62', 'mypath', 'some.hostname.address.com', 'app-9', 'instance-6','x', ?) SETTINGS async_insert=1,wait_for_async_insert=0 " )) {
201+ "INSERT INTO t_map SETTINGS async_insert=1,wait_for_async_insert=1 VALUES (8481365034795008,1673349039830,'operation-9','a','service', 'bc3e47b8-2b34-4c1a-9004-123656fa0000','b', 'c', 'service-56','d', 'object','e', 'my-value-62', 'mypath', 'some.hostname.address.com', 'app-9', 'instance-6','x', ?)" )) {
198202 stmt .setObject (1 , Collections .singletonMap ("key1" , "value1" ));
199203 stmt .execute ();
200204
@@ -206,18 +210,44 @@ static void insertByteArray(Connection conn) throws SQLException {
206210 }
207211 }
208212
213+ static void usedPooledConnection (String url ) throws SQLException {
214+ // connection pooling won't help much in terms of performance,
215+ // because the underlying implementation has its own pool.
216+ // for example: HttpURLConnection has a pool for sockets
217+ HikariConfig poolConfig = new HikariConfig ();
218+ poolConfig .setConnectionTimeout (5000L );
219+ poolConfig .setMaximumPoolSize (20 );
220+ poolConfig .setMaxLifetime (300_000L );
221+ poolConfig .setDataSource (new ClickHouseDataSource (url ));
222+
223+ HikariDataSource ds = new HikariDataSource (poolConfig );
224+
225+ try (Connection conn = ds .getConnection ();
226+ Statement s = conn .createStatement ();
227+ ResultSet rs = s .executeQuery ("select 123" )) {
228+ System .out .println (rs .next ());
229+ System .out .println (rs .getInt (1 ));
230+ }
231+ }
232+
209233 public static void main (String [] args ) {
210234 // jdbc:ch:https://explorer@play.clickhouse.com:443
211235 // jdbc:ch:https://demo:demo@github.demo.trial.altinity.cloud
212236 String url = System .getProperty ("chUrl" , "jdbc:ch://localhost" );
213237
238+ try {
239+ usedPooledConnection (url );
240+ } catch (SQLException e ) {
241+ e .printStackTrace ();
242+ }
243+
214244 try (Connection conn = getConnection (url )) {
215- // connectWithCustomSettings(url);
245+ connectWithCustomSettings (url );
216246 insertByteArray (conn );
217247
218- // System.out.println("Update Count: " + dropAndCreateTable(conn));
219- // System.out.println("Inserted Rows: " + batchInsert(conn));
220- // System.out.println("Result Rows: " + query(conn));
248+ System .out .println ("Update Count: " + dropAndCreateTable (conn ));
249+ System .out .println ("Inserted Rows: " + batchInsert (conn ));
250+ System .out .println ("Result Rows: " + query (conn ));
221251 } catch (SQLException e ) {
222252 e .printStackTrace ();
223253 }
0 commit comments