Skip to content

Commit

Permalink
first version of r2dbc
Browse files Browse the repository at this point in the history
  • Loading branch information
rernas35 committed Sep 5, 2022
1 parent 6d79b16 commit 4bb1bea
Show file tree
Hide file tree
Showing 40 changed files with 4,774 additions and 0 deletions.
26 changes: 26 additions & 0 deletions clickhouse-r2dbc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# clickhouse-r2dbc

This module provides r2dbc support to clickhouse-jdbc driver.

r2dbc link : https://r2dbc.io/

Sample code:
```java
ConnectionFactory connectionFactory = ConnectionFactories
.get("r2dbc:clickhouse:http://{username}:{password}@{host}:{port}/{database}");

Mono.from(connectionFactory.create())
.flatMapMany(connection -> connection
.createStatement("select domain, path, toDate(cdate) as d, count(1) as count from clickdb.clicks where domain = :domain group by domain, path, d")
.bind("domain", domain)
.execute())
.flatMap(result -> result
.map((row, rowMetadata) -> String.format("%s%s[%s]:%d", row.get("domain", String.class),
row.get("path", String.class),
row.get("d", LocalDate.class),
row.get("count", Long.class)) ))
.doOnNext(System.out::println)
.subscribe();
```

for full example please check clickhouse-jdbc/examples/clickhouse-r2dbc-samples/clickhouse-r2dbc-spring-webflux-sample .
286 changes: 286 additions & 0 deletions clickhouse-r2dbc/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>clickhouse-java</artifactId>
<groupId>com.clickhouse</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>clickhouse-r2dbc</artifactId>

<properties>
<hikari-cp.version>4.0.3</hikari-cp.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<r2dbc-spi.version>1.0.0.RELEASE</r2dbc-spi.version>
<spec.title>R2DBC</spec.title>
<spec.version>1.0</spec.version>
</properties>

<dependencies>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-spi</artifactId>
<version>${r2dbc-spi.version}</version>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-spi-test</artifactId>
<version>${r2dbc-spi.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-grpc-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-http-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-client</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${hikari-cp.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration combine.self="override">
<excludedGroups>${excludedGroups}</excludedGroups>
<skipTests>${skipUTs}</skipTests>
<useModulePath>false</useModulePath>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>shade</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<createDependencyReducedPom>true</createDependencyReducedPom>
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
<shadedClassifierName>shaded</shadedClassifierName>
<relocations>
<relocation>
<pattern>com.google.gson</pattern>
<shadedPattern>${shade.base}.gson</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache</pattern>
<shadedPattern>${shade.base}.apache</shadedPattern>
</relocation>
</relocations>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Automatic-Module-Name>${project.groupId}.r2dbc</Automatic-Module-Name>
<Specification-Title>${spec.title}</Specification-Title>
<Specification-Version>${spec.version}</Specification-Version>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>${project.parent.groupId}:clickhouse-grpc-client</artifact>
<excludes>
<exclude>**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>mozilla/**</exclude>
<exclude>**/darwin/**</exclude>
<exclude>**/linux/**</exclude>
<exclude>**/win32/**</exclude>
<exclude>**/module-info.class</exclude>
<exclude>META-INF/DEPENDENCIES</exclude>
<exclude>META-INF/MANIFEST.MF</exclude>
<exclude>META-INF/maven/**</exclude>
<exclude>META-INF/native-image/**</exclude>
<exclude>META-INF/*.xml</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
<execution>
<id>shade-all</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<createDependencyReducedPom>true</createDependencyReducedPom>
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
<shadedClassifierName>all</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Automatic-Module-Name>${project.groupId}.r2dbc</Automatic-Module-Name>
<Specification-Title>${spec.title}</Specification-Title>
<Specification-Version>${spec.version}</Specification-Version>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>com/google/**</exclude>
<exclude>mozilla/**</exclude>
<exclude>org/**</exclude>
<exclude>**/module-info.class</exclude>
<exclude>META-INF/DEPENDENCIES</exclude>
<exclude>META-INF/MANIFEST.MF</exclude>
<exclude>META-INF/maven/**</exclude>
<exclude>META-INF/native-image/**</exclude>
<exclude>META-INF/*.xml</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
<execution>
<id>shade-http</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<createDependencyReducedPom>true</createDependencyReducedPom>
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
<shadedClassifierName>http</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Automatic-Module-Name>${project.groupId}.r2dbc</Automatic-Module-Name>
<Specification-Title>${spec.title}</Specification-Title>
<Specification-Version>${spec.version}</Specification-Version>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>${project.parent.groupId}:clickhouse-cli-client</artifact>
<excludes>
<exclude>**</exclude>
</excludes>
</filter>
<filter>
<artifact>${project.parent.groupId}:clickhouse-grpc-client</artifact>
<excludes>
<exclude>**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>com/google/**</exclude>
<exclude>mozilla/**</exclude>
<exclude>org/**</exclude>
<exclude>ru/**</exclude>
<exclude>**/darwin/**</exclude>
<exclude>**/linux/**</exclude>
<exclude>**/win32/**</exclude>
<exclude>**/module-info.class</exclude>
<exclude>META-INF/DEPENDENCIES</exclude>
<exclude>META-INF/MANIFEST.MF</exclude>
<exclude>META-INF/maven/**</exclude>
<exclude>META-INF/native-image/**</exclude>
<exclude>META-INF/*.xml</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<executions>
<execution>
<id>flatten</id>
<phase>package</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.clickhouse.r2dbc;

import com.clickhouse.client.ClickHouseFormat;
import com.clickhouse.client.ClickHouseRequest;
import io.r2dbc.spi.Batch;
import io.r2dbc.spi.Result;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

public class ClickHouseBatch implements Batch {

private static final ClickHouseFormat PREFERRED_FORMAT = ClickHouseFormat.TabSeparatedWithNamesAndTypes;
private ClickHouseRequest<?> request;
List<String> sqlList = new ArrayList<>();

public ClickHouseBatch(ClickHouseRequest request) {
this.request = request;
}

@Override
public Batch add(String sql) {
sqlList.add(sql);
return this;
}

@Override
public Publisher<? extends Result> execute() {
return Flux.fromStream(sqlList.stream().map(sql -> {
request.query(sql).format(PREFERRED_FORMAT);
return Mono.fromFuture(request::execute); }))
.flatMap(Mono::flux)
.map(ClickHouseResult::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.clickhouse.r2dbc;

import com.clickhouse.client.ClickHouseColumn;
import com.clickhouse.r2dbc.types.ClickHouseDataTypeWrapper;
import io.r2dbc.spi.ColumnMetadata;
import io.r2dbc.spi.Type;

public class ClickHouseColumnMetadata implements ColumnMetadata {

final Type type;
final String name;

ClickHouseColumnMetadata(ClickHouseColumn col) {
this.name = col.getColumnName(); // TODO :check alias handling.
this.type = ClickHouseDataTypeWrapper.of(col.getDataType());
}

@Override
public Type getType() {
return type;
}

@Override
public String getName() {
return name;
}
}
Loading

0 comments on commit 4bb1bea

Please sign in to comment.