> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8a08bda2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Драйвер ClickHouse R2DBC

# Драйвер R2DBC

<div id="r2dbc-driver">
  ## Драйвер R2DBC
</div>

[R2DBC](https://r2dbc.io/) — обёртка над асинхронным Java-клиентом для ClickHouse.

<div id="environment-requirements">
  ### Требования к окружению
</div>

* [OpenJDK](https://openjdk.java.net) версии 8 или выше

<div id="setup">
  ### Настройка
</div>

```xml theme={null}
<dependency>
    <groupId>com.clickhouse</groupId>
    <!-- замените на clickhouse-r2dbc_0.9.1 для SPI 0.9.1.RELEASE -->
    <artifactId>clickhouse-r2dbc</artifactId>
    <version>0.7.1</version>
    <!-- используйте uber jar со всеми включёнными зависимостями; замените classifier на http или grpc для jar меньшего размера -->
    <classifier>all</classifier>
    <exclusions>
        <exclusion>
            <groupId>*</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>
```

<div id="connect-to-clickhouse">
  ### Подключение к ClickHouse
</div>

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

    Mono.from(connectionFactory.create())
        .flatMapMany(connection -> connection
```

<div id="query">
  ### Запрос
</div>

```java showLineNumbers theme={null}
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();
```

<div id="insert">
  ### Вставка
</div>

```java showLineNumbers theme={null}
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();
```
