From 33995c2b4e508aa99499c9a8d5de16cb0c9ec629 Mon Sep 17 00:00:00 2001 From: Zhichun Wu Date: Wed, 1 Feb 2023 21:24:42 +0800 Subject: [PATCH] create source jar and update examples --- clickhouse-benchmark/pom.xml | 4 +- clickhouse-cli-client/pom.xml | 1 + clickhouse-grpc-client/pom.xml | 3 ++ clickhouse-http-client/pom.xml | 1 + clickhouse-jdbc/pom.xml | 10 ++-- clickhouse-r2dbc/pom.xml | 2 + examples/grpc/pom.xml | 4 +- .../com/clickhouse/examples/jdbc/Main.java | 12 ++--- examples/jdbc/pom.xml | 10 +++- .../com/clickhouse/examples/jdbc/Basic.java | 46 +++++++++++++++---- pom.xml | 6 +-- 11 files changed, 75 insertions(+), 24 deletions(-) diff --git a/clickhouse-benchmark/pom.xml b/clickhouse-benchmark/pom.xml index 6038d4dfc..c4e030cb9 100644 --- a/clickhouse-benchmark/pom.xml +++ b/clickhouse-benchmark/pom.xml @@ -122,8 +122,8 @@ jctools-core - mysql - mysql-connector-java + com.mysql + mysql-connector-j org.mariadb.jdbc diff --git a/clickhouse-cli-client/pom.xml b/clickhouse-cli-client/pom.xml index 3f02dacd1..5f7190247 100644 --- a/clickhouse-cli-client/pom.xml +++ b/clickhouse-cli-client/pom.xml @@ -85,6 +85,7 @@ true shaded true + true true diff --git a/clickhouse-grpc-client/pom.xml b/clickhouse-grpc-client/pom.xml index d68650377..278f7232c 100644 --- a/clickhouse-grpc-client/pom.xml +++ b/clickhouse-grpc-client/pom.xml @@ -148,6 +148,7 @@ true shaded true + true true @@ -250,6 +251,7 @@ true true + true true netty @@ -341,6 +343,7 @@ true true + true true okhttp diff --git a/clickhouse-http-client/pom.xml b/clickhouse-http-client/pom.xml index bb09567b3..4adb29901 100644 --- a/clickhouse-http-client/pom.xml +++ b/clickhouse-http-client/pom.xml @@ -115,6 +115,7 @@ true shaded true + true true diff --git a/clickhouse-jdbc/pom.xml b/clickhouse-jdbc/pom.xml index 1c297693b..534407746 100644 --- a/clickhouse-jdbc/pom.xml +++ b/clickhouse-jdbc/pom.xml @@ -17,7 +17,6 @@ https://github.com/ClickHouse/clickhouse-java/tree/main/clickhouse-jdbc - 4.5.13 4.1.4 JDBC 4.2 @@ -89,8 +88,8 @@ test - mysql - mysql-connector-java + com.mysql + mysql-connector-j test @@ -170,6 +169,7 @@ true true + true true shaded @@ -248,6 +248,7 @@ true true + true true all @@ -324,6 +325,7 @@ true true + true true grpc @@ -416,6 +418,7 @@ true true + true true http @@ -503,6 +506,7 @@ true true + true true cli diff --git a/clickhouse-r2dbc/pom.xml b/clickhouse-r2dbc/pom.xml index 3fe72dd81..a0fb2bdf8 100644 --- a/clickhouse-r2dbc/pom.xml +++ b/clickhouse-r2dbc/pom.xml @@ -219,6 +219,7 @@ true true + true true all @@ -313,6 +314,7 @@ true true + true true http diff --git a/examples/grpc/pom.xml b/examples/grpc/pom.xml index c2b729396..2ac897c4d 100644 --- a/examples/grpc/pom.xml +++ b/examples/grpc/pom.xml @@ -1,5 +1,7 @@ - + 4.0.0 com.clickhouse diff --git a/examples/grpc/src/main/java/com/clickhouse/examples/jdbc/Main.java b/examples/grpc/src/main/java/com/clickhouse/examples/jdbc/Main.java index b97c3ca15..611a221c8 100644 --- a/examples/grpc/src/main/java/com/clickhouse/examples/jdbc/Main.java +++ b/examples/grpc/src/main/java/com/clickhouse/examples/jdbc/Main.java @@ -44,12 +44,12 @@ static long insert(ClickHouseNode server, String table) throws ClickHouseExcepti CompletableFuture future; // back-pressuring is not supported, you can adjust the first two arguments try (ClickHousePipedOutputStream stream = ClickHouseDataStreamFactory.getInstance() - .createPipedOutputStream(config, null)) { + .createPipedOutputStream(config, (Runnable) null)) { // in async mode, which is default, execution happens in a worker thread future = request.data(stream.getInputStream()).execute(); // writing happens in main thread - for (int i = 0; i < 1000000; i++) { + for (int i = 0; i < 10_000; i++) { BinaryStreamUtils.writeString(stream, String.valueOf(i % 16)); BinaryStreamUtils.writeNonNull(stream); BinaryStreamUtils.writeString(stream, UUID.randomUUID().toString()); @@ -74,7 +74,7 @@ static int query(ClickHouseNode server, String table) throws ClickHouseException ClickHouseResponse response = client.connect(server).query("select * from " + table).execute().get()) { int count = 0; // or use stream API via response.stream() - for (ClickHouseRecord rec : response.records()) { + for (ClickHouseRecord r : response.records()) { count++; } return count; @@ -88,7 +88,7 @@ static int query(ClickHouseNode server, String table) throws ClickHouseException public static void main(String[] args) { ClickHouseNode server = ClickHouseNode.builder() - .host(System.getProperty("chHost", "192.168.3.16")) + .host(System.getProperty("chHost", "127.0.0.1")) .port(ClickHouseProtocol.GRPC, Integer.parseInt(System.getProperty("chPort", "9100"))) .database("system").credentials(ClickHouseCredentials.fromUserAndPassword( System.getProperty("chUser", "default"), System.getProperty("chPassword", ""))) @@ -99,9 +99,9 @@ public static void main(String[] args) { try { dropAndCreateTable(server, table); - insert(server, table); + System.out.println("Insert: " + insert(server, table)); - query(server, table); + System.out.println("Query: " + query(server, table)); } catch (ClickHouseException e) { e.printStackTrace(); } diff --git a/examples/jdbc/pom.xml b/examples/jdbc/pom.xml index 85444fbb0..7cf6dd91a 100644 --- a/examples/jdbc/pom.xml +++ b/examples/jdbc/pom.xml @@ -1,5 +1,7 @@ - + 4.0.0 com.clickhouse @@ -65,6 +67,7 @@ UTF-8 0.4.0 + 4.0.3 3.8.1 @@ -84,6 +87,11 @@ + + com.zaxxer + HikariCP + ${hikaricp.version} + diff --git a/examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Basic.java b/examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Basic.java index 5ace5b16e..22382998a 100644 --- a/examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Basic.java +++ b/examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Basic.java @@ -13,6 +13,9 @@ import com.clickhouse.data.ClickHouseOutputStream; import com.clickhouse.data.ClickHouseWriter; +import com.clickhouse.jdbc.ClickHouseDataSource; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; public class Basic { static final String TABLE_NAME = "jdbc_example_basic"; @@ -125,6 +128,7 @@ public void write(ClickHouseOutputStream output) throws IOException { // this will be executed in a separate thread for (int i = 0; i < 1_000_000; i++) { output.writeUnicodeString("a-" + i); + output.writeBoolean(false); // non-null output.writeUnicodeString("b-" + i); } } @@ -137,11 +141,11 @@ public void write(ClickHouseOutputStream output) throws IOException { static int connectWithCustomSettings(String url) throws SQLException { // comma separated settings - String customSettings = "session_check=0,max_query_size=100"; + String customSettings = "session_check=0,max_query_size=1000"; Properties properties = new Properties(); - // properties.setProperty(ClickHouseHttpClientOption.CUSTOM_PARAMS.getKey(), + // properties.setProperty(ClickHouseClientOption.CUSTOM_SETTINGS.getKey(), // customSettings); - properties.setProperty("custom_http_params", customSettings); // limited to http protocol + properties.setProperty("custom_settings", customSettings); try (Connection conn = getConnection(url, properties); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select 5")) { @@ -194,7 +198,7 @@ static void insertByteArray(Connection conn) throws SQLException { + "ORDER BY (resource_container, event_type, event_subtype) " + "SETTINGS index_granularity = 8192"); try (PreparedStatement stmt = conn.prepareStatement( - "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")) { + "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', ?)")) { stmt.setObject(1, Collections.singletonMap("key1", "value1")); stmt.execute(); @@ -206,18 +210,44 @@ static void insertByteArray(Connection conn) throws SQLException { } } + static void usedPooledConnection(String url) throws SQLException { + // connection pooling won't help much in terms of performance, + // because the underlying implementation has its own pool. + // for example: HttpURLConnection has a pool for sockets + HikariConfig poolConfig = new HikariConfig(); + poolConfig.setConnectionTimeout(5000L); + poolConfig.setMaximumPoolSize(20); + poolConfig.setMaxLifetime(300_000L); + poolConfig.setDataSource(new ClickHouseDataSource(url)); + + HikariDataSource ds = new HikariDataSource(poolConfig); + + try (Connection conn = ds.getConnection(); + Statement s = conn.createStatement(); + ResultSet rs = s.executeQuery("select 123")) { + System.out.println(rs.next()); + System.out.println(rs.getInt(1)); + } + } + public static void main(String[] args) { // jdbc:ch:https://explorer@play.clickhouse.com:443 // jdbc:ch:https://demo:demo@github.demo.trial.altinity.cloud String url = System.getProperty("chUrl", "jdbc:ch://localhost"); + try { + usedPooledConnection(url); + } catch (SQLException e) { + e.printStackTrace(); + } + try (Connection conn = getConnection(url)) { - // connectWithCustomSettings(url); + connectWithCustomSettings(url); insertByteArray(conn); - // System.out.println("Update Count: " + dropAndCreateTable(conn)); - // System.out.println("Inserted Rows: " + batchInsert(conn)); - // System.out.println("Result Rows: " + query(conn)); + System.out.println("Update Count: " + dropAndCreateTable(conn)); + System.out.println("Inserted Rows: " + batchInsert(conn)); + System.out.println("Result Rows: " + query(conn)); } catch (SQLException e) { e.printStackTrace(); } diff --git a/pom.xml b/pom.xml index f9997b8d8..cb834bb50 100644 --- a/pom.xml +++ b/pom.xml @@ -116,7 +116,7 @@ 7.5 3.1.1 - 8.0.31 + 8.0.32 42.5.1 1.7.0 @@ -348,8 +348,8 @@ - mysql - mysql-connector-java + com.mysql + mysql-connector-j ${mysql-driver.version}