-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
475 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.clickhouse</groupId> | ||
<artifactId>grpc-examples</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>grpc-examples</name> | ||
<description>gRPC Examples</description> | ||
<url>https://github.com/ClickHouse/clickhouse-jdbc</url> | ||
<inceptionYear>2022</inceptionYear> | ||
|
||
<organization> | ||
<name>ClickHouse, Inc.</name> | ||
<url>https://clickhouse.com/</url> | ||
</organization> | ||
|
||
<licenses> | ||
<license> | ||
<name>The Apache Software License, Version 2.0</name> | ||
<url>http://www.apache.org/licenses/LICENSE-2.0</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
|
||
<developers> | ||
<developer> | ||
<id>zhicwu</id> | ||
<name>Zhichun Wu</name> | ||
<email>[email protected]</email> | ||
<timezone>+8</timezone> | ||
</developer> | ||
</developers> | ||
|
||
<scm> | ||
<url>https://github.com/ClickHouse/clickhouse-jdbc</url> | ||
<connection>scm:[email protected]:ClickHouse/clickhouse-jdbc.git</connection> | ||
<developerConnection>scm:[email protected]:ClickHouse/clickhouse-jdbc.git</developerConnection> | ||
<tag>HEAD</tag> | ||
</scm> | ||
|
||
<issueManagement> | ||
<system>Github</system> | ||
<url>https://github.com/ClickHouse/clickhouse-jdbc/issues</url> | ||
</issueManagement> | ||
|
||
<ciManagement> | ||
<system>Github</system> | ||
<url>https://github.com/ClickHouse/clickhouse-jdbc/actions</url> | ||
</ciManagement> | ||
|
||
<properties> | ||
<project.current.year>2022</project.current.year> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
|
||
<clickhouse-grpc.version>0.3.2</clickhouse-grpc.version> | ||
|
||
<compiler-plugin.version>3.8.1</compiler-plugin.version> | ||
|
||
<minJdk>1.8</minJdk> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.clickhouse</groupId> | ||
<artifactId>clickhouse-grpc-client</artifactId> | ||
<version>${clickhouse-grpc.version}</version> | ||
<classifier>shaded</classifier> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>*</groupId> | ||
<artifactId>*</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>${compiler-plugin.version}</version> | ||
<configuration> | ||
<source>${minJdk}</source> | ||
<target>${minJdk}</target> | ||
<showWarnings>true</showWarnings> | ||
<compilerArgs> | ||
<arg>-Xlint:all</arg> | ||
<!-- arg>-Werror</arg --> | ||
</compilerArgs> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
108 changes: 108 additions & 0 deletions
108
examples/grpc/src/main/java/com/clickhouse/examples/jdbc/Main.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.clickhouse.examples.jdbc; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import com.clickhouse.client.ClickHouseClient; | ||
import com.clickhouse.client.ClickHouseConfig; | ||
import com.clickhouse.client.ClickHouseCredentials; | ||
import com.clickhouse.client.ClickHouseException; | ||
import com.clickhouse.client.ClickHouseFormat; | ||
import com.clickhouse.client.ClickHouseNode; | ||
import com.clickhouse.client.ClickHouseProtocol; | ||
import com.clickhouse.client.ClickHouseRecord; | ||
import com.clickhouse.client.ClickHouseRequest; | ||
import com.clickhouse.client.ClickHouseResponse; | ||
import com.clickhouse.client.ClickHouseResponseSummary; | ||
import com.clickhouse.client.data.BinaryStreamUtils; | ||
import com.clickhouse.client.data.ClickHousePipedStream; | ||
|
||
public class Main { | ||
static void dropAndCreateTable(ClickHouseNode server, String table) throws ClickHouseException { | ||
try (ClickHouseClient client = ClickHouseClient.newInstance(server.getProtocol())) { | ||
ClickHouseRequest<?> request = client.connect(server); | ||
// or use future chaining | ||
request.query("drop table if exists " + table).execute().get(); | ||
request.query("create table " + table + "(a String, b Nullable(String)) engine=MergeTree() order by a") | ||
.execute().get(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw ClickHouseException.forCancellation(e, server); | ||
} catch (ExecutionException e) { | ||
throw ClickHouseException.of(e, server); | ||
} | ||
} | ||
|
||
static long insert(ClickHouseNode server, String table) throws ClickHouseException { | ||
try (ClickHouseClient client = ClickHouseClient.newInstance(server.getProtocol())) { | ||
ClickHouseRequest.Mutation request = client.connect(server).write().table(table) | ||
.format(ClickHouseFormat.RowBinary); | ||
ClickHouseConfig config = request.getConfig(); | ||
CompletableFuture<ClickHouseResponse> future; | ||
// back-pressuring is not supported, you can adjust the first two arguments | ||
try (ClickHousePipedStream stream = new ClickHousePipedStream(config.getMaxBufferSize(), | ||
config.getMaxQueuedBuffers(), config.getSocketTimeout())) { | ||
// in async mode, which is default, execution happens in a worker thread | ||
future = request.data(stream.getInput()).execute(); | ||
|
||
// writing happens in main thread | ||
for (int i = 0; i < 1000000; i++) { | ||
BinaryStreamUtils.writeString(stream, String.valueOf(i % 16)); | ||
BinaryStreamUtils.writeNonNull(stream); | ||
BinaryStreamUtils.writeString(stream, UUID.randomUUID().toString()); | ||
} | ||
} | ||
|
||
// response should be always closed | ||
try (ClickHouseResponse response = future.get()) { | ||
ClickHouseResponseSummary summary = response.getSummary(); | ||
return summary.getWrittenRows(); | ||
} | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw ClickHouseException.forCancellation(e, server); | ||
} catch (ExecutionException | IOException e) { | ||
throw ClickHouseException.of(e, server); | ||
} | ||
} | ||
|
||
static int query(ClickHouseNode server, String table) throws ClickHouseException { | ||
try (ClickHouseClient client = ClickHouseClient.newInstance(server.getProtocol()); | ||
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()) { | ||
count++; | ||
} | ||
return count; | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw ClickHouseException.forCancellation(e, server); | ||
} catch (ExecutionException e) { | ||
throw ClickHouseException.of(e, server); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
ClickHouseNode server = ClickHouseNode.builder() | ||
.host(System.getProperty("chHost", "192.168.3.16")) | ||
.port(ClickHouseProtocol.GRPC, Integer.parseInt(System.getProperty("chPort", "9100"))) | ||
.database("system").credentials(ClickHouseCredentials.fromUserAndPassword( | ||
System.getProperty("chUser", "default"), System.getProperty("chPassword", ""))) | ||
.build(); | ||
|
||
String table = "grpc_example_table"; | ||
|
||
try { | ||
dropAndCreateTable(server, table); | ||
|
||
insert(server, table); | ||
|
||
query(server, table); | ||
} catch (ClickHouseException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.clickhouse</groupId> | ||
<artifactId>jdbc-examples</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>jdbc-examples</name> | ||
<description>JDBC Examples</description> | ||
<url>https://github.com/ClickHouse/clickhouse-jdbc</url> | ||
<inceptionYear>2022</inceptionYear> | ||
|
||
<organization> | ||
<name>ClickHouse, Inc.</name> | ||
<url>https://clickhouse.com/</url> | ||
</organization> | ||
|
||
<licenses> | ||
<license> | ||
<name>The Apache Software License, Version 2.0</name> | ||
<url>http://www.apache.org/licenses/LICENSE-2.0</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
|
||
<developers> | ||
<developer> | ||
<id>zhicwu</id> | ||
<name>Zhichun Wu</name> | ||
<email>[email protected]</email> | ||
<timezone>+8</timezone> | ||
</developer> | ||
</developers> | ||
|
||
<scm> | ||
<url>https://github.com/ClickHouse/clickhouse-jdbc</url> | ||
<connection>scm:[email protected]:ClickHouse/clickhouse-jdbc.git</connection> | ||
<developerConnection>scm:[email protected]:ClickHouse/clickhouse-jdbc.git</developerConnection> | ||
<tag>HEAD</tag> | ||
</scm> | ||
|
||
<issueManagement> | ||
<system>Github</system> | ||
<url>https://github.com/ClickHouse/clickhouse-jdbc/issues</url> | ||
</issueManagement> | ||
|
||
<ciManagement> | ||
<system>Github</system> | ||
<url>https://github.com/ClickHouse/clickhouse-jdbc/actions</url> | ||
</ciManagement> | ||
|
||
<properties> | ||
<project.current.year>2022</project.current.year> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
|
||
<clickhouse-jdbc.version>0.3.2</clickhouse-jdbc.version> | ||
|
||
<compiler-plugin.version>3.8.1</compiler-plugin.version> | ||
|
||
<minJdk>1.8</minJdk> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.clickhouse</groupId> | ||
<artifactId>clickhouse-jdbc</artifactId> | ||
<version>${clickhouse-jdbc.version}</version> | ||
<classifier>http</classifier> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>*</groupId> | ||
<artifactId>*</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>${compiler-plugin.version}</version> | ||
<configuration> | ||
<source>${minJdk}</source> | ||
<target>${minJdk}</target> | ||
<showWarnings>true</showWarnings> | ||
<compilerArgs> | ||
<arg>-Xlint:all</arg> | ||
<!-- arg>-Werror</arg --> | ||
</compilerArgs> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
79 changes: 79 additions & 0 deletions
79
examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Advance.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.clickhouse.examples.jdbc; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.time.LocalDateTime; | ||
import java.util.Properties; | ||
|
||
import com.clickhouse.client.ClickHouseFormat; | ||
import com.clickhouse.client.data.ClickHouseExternalTable; | ||
|
||
public class Advance { | ||
static String exteralTables(String url, String user, String password) throws SQLException { | ||
String sql = "select a.name as n1, b.name as n2 from {tt 'table1'} a inner join {tt 'table2'} b on a.id=b.id"; | ||
try (Connection conn = DriverManager.getConnection(url, user, password); | ||
PreparedStatement ps = conn.prepareStatement(sql)) { | ||
ps.setObject(1, | ||
ClickHouseExternalTable.builder().name("table1").columns("id Int32, name Nullable(String)") | ||
.format(ClickHouseFormat.CSV) | ||
.content(new ByteArrayInputStream("1,a\n2,b".getBytes(StandardCharsets.US_ASCII))).build()); | ||
ps.setObject(2, | ||
ClickHouseExternalTable.builder().name("table2").columns("id Int32, name String") | ||
.format(ClickHouseFormat.JSONEachRow) | ||
.content(new ByteArrayInputStream("{\"id\":3,\"name\":\"c\"}\n{\"id\":1,\"name\":\"d\"}" | ||
.getBytes(StandardCharsets.US_ASCII))) | ||
.build()); | ||
try (ResultSet rs = ps.executeQuery()) { | ||
if (!rs.next()) { | ||
throw new IllegalStateException("Should have at least one record"); | ||
} | ||
|
||
// n1=a, n2=d | ||
return String.format("n1=%s, n2=%s", rs.getString(1), rs.getString(2)); | ||
} | ||
} | ||
} | ||
|
||
static String namedParameter(String url, String user, String password) throws SQLException { | ||
Properties props = new Properties(); | ||
props.setProperty("user", user); | ||
props.setProperty("password", password); | ||
props.setProperty("namedParameter", "true"); | ||
// two parameters: | ||
// * a - String | ||
// * b - DateTime64(3) | ||
String sql = "select :a as a1, :a(String) as a2, :b(DateTime64(3)) as b"; | ||
try (Connection conn = DriverManager.getConnection(url, props); | ||
PreparedStatement ps = conn.prepareStatement(sql)) { | ||
ps.setString(1, "a"); | ||
ps.setObject(2, LocalDateTime.of(2022, 1, 7, 22, 48, 17, 123000000)); | ||
|
||
try (ResultSet rs = ps.executeQuery()) { | ||
if (!rs.next()) { | ||
throw new IllegalStateException("Should have at least one record"); | ||
} | ||
// a1=a, a2=a, b=2022-01-07 22:48:17.123 | ||
return String.format("a1=%s, a2=%s, b=%s", rs.getString(1), rs.getString(2), rs.getString("B")); | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
String url = String.format("jdbc:ch://%s:%d/system", System.getProperty("chHost", "localhost"), | ||
Integer.parseInt(System.getProperty("chPort", "8123"))); | ||
String user = System.getProperty("chUser", "default"); | ||
String password = System.getProperty("chPassword", ""); | ||
|
||
try { | ||
exteralTables(url, user, password); | ||
namedParameter(url, user, password); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Oops, something went wrong.