-
Notifications
You must be signed in to change notification settings - Fork 621
Simple jdbc example #2083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Simple jdbc example #2083
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8c8fc2c
Remove advanced and update basic example
6a89570
Update Basic.java
6f8f311
Update Basic.java
0ce7eee
Update Basic.java
93a80ed
Update Basic.java
6e7d4ac
Update Basic.java
b07d20b
Updating for accuracy and helpfulness
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
170 changes: 0 additions & 170 deletions
170
examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Advanced.java
This file was deleted.
Oops, something went wrong.
195 changes: 29 additions & 166 deletions
195
examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Basic.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,190 +1,53 @@ | ||
| package com.clickhouse.examples.jdbc; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.Date; | ||
| import java.sql.DriverManager; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| import java.time.LocalDateTime; | ||
| import java.util.Collections; | ||
| import java.util.Properties; | ||
|
|
||
| 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"; | ||
|
|
||
| private static Connection getConnection(String url) throws SQLException { | ||
| return getConnection(url, new Properties()); | ||
| } | ||
|
|
||
| private static Connection getConnection(String url, Properties properties) throws SQLException { | ||
| final Connection conn; | ||
| // Driver driver = new ClickHouseDriver(); | ||
| // conn = driver.connect(url, properties); | ||
|
|
||
| // ClickHouseDataSource dataSource = new ClickHouseDataSource(url, properties); | ||
| // conn = dataSource.getConnection(); | ||
|
|
||
| conn = DriverManager.getConnection(url, properties); | ||
| System.out.println("Connected to: " + conn.getMetaData().getURL()); | ||
| return conn; | ||
| } | ||
|
|
||
| static int dropAndCreateTable(Connection conn) throws SQLException { | ||
| try (Statement stmt = conn.createStatement()) { | ||
| stmt.execute(String.format("DROP TABLE IF EXISTS %1$s",TABLE_NAME)); | ||
| stmt.execute(String.format("CREATE TABLE %1$s(a String, b Nullable(String)) ENGINE=MergeTree ORDER BY ()", TABLE_NAME)); | ||
| return stmt.getUpdateCount(); | ||
| } | ||
| } | ||
|
|
||
| static int batchInsert(Connection conn) throws SQLException { | ||
| // 1. NOT recommended when inserting lots of rows, because it's based on a large | ||
| // statement | ||
| String sql = String.format("INSERT INTO %s VALUES(? || ' - 1', ?)", TABLE_NAME); | ||
| int count = 0; | ||
| try (PreparedStatement ps = conn.prepareStatement(sql)) { | ||
| ps.setString(1, "a"); | ||
| ps.setString(2, "b"); | ||
| ps.addBatch(); | ||
| ps.setString(1, "c"); | ||
| ps.setString(2, null); | ||
| ps.addBatch(); | ||
|
|
||
| // same as below query: | ||
| // insert into <table> values ('a' || ' - 1', 'b'), ('c' || ' - 1', null) | ||
| for (int i : ps.executeBatch()) { | ||
| if (i > 0) { | ||
| count += i; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
| public static void main(String[] args) { | ||
| String url = System.getProperty("chUrl", "jdbc:ch://localhost:18123"); | ||
|
|
||
| static int connectWithCustomSettings(String url) throws SQLException { | ||
| // comma separated settings | ||
| String customSettings = "session_check=0,max_query_size=3000"; | ||
| // Set user and password if needed | ||
| Properties properties = new Properties(); | ||
| // properties.setProperty(ClickHouseClientOption.CUSTOM_SETTINGS.getKey(), | ||
| // customSettings); | ||
| properties.setProperty("custom_settings", customSettings); | ||
| try (Connection conn = getConnection(url, properties); | ||
| Statement stmt = conn.createStatement(); | ||
| ResultSet rs = stmt.executeQuery("select 5")) { | ||
| return rs.next() ? rs.getInt(1) : -1; | ||
| } | ||
| } | ||
|
|
||
| static int query(Connection conn) throws SQLException { | ||
| String sql = "select * from " + TABLE_NAME; | ||
| try (Statement stmt = conn.createStatement()) { | ||
| // set max_result_rows = 3, result_overflow_mode = 'break' | ||
| // or simply discard rows after the first 3 in read-only mode | ||
| stmt.setMaxRows(3); | ||
| int count = 0; | ||
| try (ResultSet rs = stmt.executeQuery(sql)) { | ||
| while (rs.next()) { | ||
| count++; | ||
| properties.setProperty("user", System.getProperty("chUser", "default")); | ||
| properties.setProperty("password", System.getProperty("chPassword", "")); | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(url, properties)) {//Grab a connection using the jdbc DriverManager | ||
| try (Statement stmt = conn.createStatement()) {//Create a statement | ||
| stmt.execute("DROP TABLE IF EXISTS " + TABLE_NAME);//Execute a query to drop the table if it exists | ||
| stmt.execute("CREATE TABLE " + TABLE_NAME + " (date Date, id UInt32, name String) ENGINE = Memory"); | ||
Paultagoras marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + TABLE_NAME + " VALUES(?, ?, ?)")) {//Create a prepared statement | ||
| pstmt.setDate(1, new Date(System.currentTimeMillis()));//Set the first parameter to the current date (using java.sql.Date) | ||
Paultagoras marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pstmt.setInt(2, 1);//Set the second parameter to 1 | ||
| pstmt.setString(3, "Alice");//Set the third parameter to "Alice" | ||
| pstmt.addBatch();//Add the current parameters to the batch | ||
|
|
||
| pstmt.setDate(1, new Date(System.currentTimeMillis()));//Set the first parameter to the current date (using java.sql.Date) | ||
Paultagoras marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pstmt.setInt(2, 2);//Set the second parameter to 2 | ||
| pstmt.setString(3, "Bob");//Set the third parameter to "Bob" | ||
| pstmt.addBatch();//Add the current parameters to the batch | ||
|
|
||
| pstmt.executeBatch();//Execute the batch | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
| } | ||
|
|
||
| static void insertByteArray(Connection conn) throws SQLException { | ||
Paultagoras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try (Statement s = conn.createStatement()) { | ||
| s.execute("DROP TABLE IF EXISTS t_map"); | ||
| s.execute("CREATE TABLE t_map" | ||
| + "(" | ||
| + " `audit_seq` Int64 CODEC(Delta(8), LZ4)," | ||
| + "`timestamp` Int64 CODEC(Delta(8), LZ4)," | ||
| + "`event_type` LowCardinality(String)," | ||
| + "`event_subtype` LowCardinality(String)," | ||
| + "`actor_type` LowCardinality(String)," | ||
| + "`actor_id` String," | ||
| + "`actor_tenant_id` LowCardinality(String)," | ||
| + "`actor_tenant_name` String," | ||
| + "`actor_firstname` String," | ||
| + "`actor_lastname` String," | ||
| + "`resource_type` LowCardinality(String)," | ||
| + "`resource_id` String," | ||
| + "`resource_container` LowCardinality(String)," | ||
| + "`resource_path` String," | ||
| + "`origin_ip` String," | ||
| + "`origin_app_name` LowCardinality(String)," | ||
| + "`origin_app_instance` String," | ||
| + "`description` String," | ||
| + "`attributes` Map(String, String)" | ||
| + ")" | ||
| + "ENGINE = MergeTree " | ||
| + "ORDER BY (resource_container, event_type, event_subtype) " | ||
| + "SETTINGS index_granularity = 8192"); | ||
| try (PreparedStatement stmt = conn.prepareStatement( | ||
| "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(); | ||
|
|
||
| try (ResultSet rs = s.executeQuery("select attributes from t_map")) { | ||
| System.out.println(rs.next()); | ||
| System.out.println(rs.getObject(1)); | ||
| try (ResultSet rs = stmt.executeQuery("SELECT * FROM " + TABLE_NAME)) { | ||
| while (rs.next()) { | ||
| System.out.println(rs.getDate(1) + ", " + rs.getInt(2) + ", " + rs.getString(3)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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); | ||
Paultagoras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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)); | ||
| } | ||
Paultagoras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| 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(); | ||
| } | ||
|
|
||
| System.out.println(url); | ||
| try (Connection conn = getConnection(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)); | ||
|
|
||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| System.out.println("Done!"); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.