r2dbc
R2DBC 驱动程序
ClickHouse 的异步 Java 客户端的 R2DBC 封装。
环境需求
- OpenJDK 版本 >= 8
设置
<dependency>
<groupId>com.clickhouse</groupId>
<!-- change to clickhouse-r2dbc_0.9.1 for SPI 0.9.1.RELEASE -->
<artifactId>clickhouse-r2dbc</artifactId>
<version>0.6.5</version>
<!-- use uber jar with all dependencies included, change classifier to http or grpc for smaller jar -->
<classifier>all</classifier>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
连接到 ClickHouse
ConnectionFactory connectionFactory = ConnectionFactories
.get("r2dbc:clickhouse:http://{username}:{password}@{host}:{port}/{database}");
Mono.from(connectionFactory.create())
.flatMapMany(connection -> 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();
插入
connection
.createStatement("insert into clickdb.clicks values (:domain, :path, :cdate, :count)")
.bind("domain", click.getDomain())
.bind("path", click.getPath())
.bind("cdate", LocalDateTime.now())
.bind("count", 1)
.execute();