clickhouse4j

Light and fast JDBC driver for ClickHouse

License

License

Categories

Categories

CLI User Interface
GroupId

GroupId

cc.blynk.clickhouse
ArtifactId

ArtifactId

clickhouse4j
Last Version

Last Version

1.4.4
Release Date

Release Date

Type

Type

jar
Description

Description

clickhouse4j
Light and fast JDBC driver for ClickHouse
Project URL

Project URL

https://github.com/blynkkk/clickhouse4j.git
Source Code Management

Source Code Management

https://github.com/blynkkk/clickhouse4j.git

Download clickhouse4j

How to add to project

<!-- https://jarcasting.com/artifacts/cc.blynk.clickhouse/clickhouse4j/ -->
<dependency>
    <groupId>cc.blynk.clickhouse</groupId>
    <artifactId>clickhouse4j</artifactId>
    <version>1.4.4</version>
</dependency>
// https://jarcasting.com/artifacts/cc.blynk.clickhouse/clickhouse4j/
implementation 'cc.blynk.clickhouse:clickhouse4j:1.4.4'
// https://jarcasting.com/artifacts/cc.blynk.clickhouse/clickhouse4j/
implementation ("cc.blynk.clickhouse:clickhouse4j:1.4.4")
'cc.blynk.clickhouse:clickhouse4j:jar:1.4.4'
<dependency org="cc.blynk.clickhouse" name="clickhouse4j" rev="1.4.4">
  <artifact name="clickhouse4j" type="jar" />
</dependency>
@Grapes(
@Grab(group='cc.blynk.clickhouse', module='clickhouse4j', version='1.4.4')
)
libraryDependencies += "cc.blynk.clickhouse" % "clickhouse4j" % "1.4.4"
[cc.blynk.clickhouse/clickhouse4j "1.4.4"]

Dependencies

test (6)

Group / Artifact Type Version
org.openjdk.jmh : jmh-core jar 1.21
org.openjdk.jmh : jmh-generator-annprocess jar 1.21
javax.xml.bind : jaxb-api jar 2.3.1
org.testng : testng jar 6.8.21
org.mockito : mockito-all jar 1.10.19
com.fasterxml.jackson.core : jackson-databind jar 2.10.1

Project Modules

There are no modules declared in this project.

Clickhouse4j - lighter and faster alternative for the official ClickHouse JDBC driver

Maven Central clickhouse4j

The main differences between this and the official driver are:

  • Removed Guava, Jackson and Apache Http Client dependencies;
  • Smaller size - 850kb vs 5.6mb of the original driver (7x smaller jar size)
  • A bunch of micro optimizations were applied (for example, batch inserts are now 40% faster)
  • CopyManager added;
  • Support for JSON, JSONCompact select;
  • Compiled against Java 8 and many other things

Usage

<dependency>
    <groupId>cc.blynk.clickhouse</groupId>
    <artifactId>clickhouse4j</artifactId>
    <version>1.4.4</version>
</dependency>

CopyManager usage

CopyManager is utility class that helps to read / write the queries from / to the file/stream/reader.

Select from DB to File
String query = "SELECT * from copy_manager_test.my_table FORMAT CSVWithNames";
Path outputFile = ...;

try (CopyManager copyManager = CopyManagerFactory.create(dataSource)) {
    copyManager.copyFromDb(query, outputFile);
}
//outputFile now has all the data and headers from the copy_manager_test DB and my_table table
Select from DB to File with prepared statement
try (Connection connection = dataSource.getConnection();
     PreparedStatement ps = connection.prepareStatement(sql);
     CopyManager copyManager = CopyManagerFactory.create(connection)) {
        ps.setLong(1, id);
        copyManager.copyFromDb(ps, outputStream);
}
Insert from File to DB
String query = "INSERT INTO copy_manager_test.my_table FORMAT CSV";
Path inputFile = ...;

try (CopyManager copyManager = CopyManagerFactory.create(dataSource)) {
    copyManager.copyToDb(query, inputFile);
}

//DB copy_manager_test and my_table table now has all csv data from the inputFile
Select as JSON
ResultSet rs = connection.createStatement().executeQuery("SELECT * FROM test.my_table FORMAT JSON");
if (rs.next()) {
    return rs.getString("json");
}

//respone example:

{
	"meta":
	[
		{
			"name": "created",
			"type": "DateTime"
		},
		{
			"name": "value",
			"type": "Int32"
		}
	],

	"data":
	[
		{
			"created": "2019-11-17 11:31:22",
			"value": 1
		},
		{
			"created": "2019-11-17 11:31:22",
			"value": 2
		}
	],

	"rows": 2,

	"statistics":
	{
		"elapsed": 0.000312306,
		"rows_read": 2,
		"bytes_read": 16
	}
}

Migration from the official driver

All you need to do is replace:

ru.yandex.clickhouse.ClickHouseDriver to cc.blynk.clickhouse.ClickHouseDriver

URL syntax: jdbc:clickhouse://<host>:<port>[/<database>], e.g. jdbc:clickhouse://localhost:8123/test

JDBC Driver Class: cc.blynk.clickhouse.ClickHouseDriver

additionally, if you have a few instances, you can use BalancedClickhouseDataSource.

Build requirements

In order to build the jdbc client one needs to have jdk 1.8 or higher.

Compiling with maven

mvn package -DskipTests=true

To build a jar with dependencies use

mvn package assembly:single -DskipTests=true

Versions

Version
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.0
1.2.0
1.1.1
1.1.0
1.0.0