-
Notifications
You must be signed in to change notification settings - Fork 557
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
20 changed files
with
1,771 additions
and
1 deletion.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
clickhouse-r2dbc-samples/clickhouse-r2dbc-spring-webflux-sample/pom.xml
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,56 @@ | ||
<?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-r2dbc-samples</artifactId> | ||
<groupId>com.clickhouse</groupId> | ||
<version>0.3.3-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>clickhouse-r2dbc-spring-webflux-sample</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<spring-boot-starter.version>2.7.1</spring-boot-starter.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-webflux</artifactId> | ||
<version>${spring-boot-starter.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>2.0.0-alpha0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
<version>2.0.0-alpha0</version> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<!-- <dependency>--> | ||
<!-- <groupId>org.springframework.boot</groupId>--> | ||
<!-- <artifactId>spring-boot-starter-data-r2dbc</artifactId>--> | ||
<!-- <version>${spring-boot-starter.version}</version>--> | ||
<!-- <exclusions>--> | ||
<!-- <exclusion>--> | ||
<!-- <groupId>io.r2dbc</groupId>--> | ||
<!-- <artifactId>r2dbc-spi</artifactId>--> | ||
<!-- </exclusion>--> | ||
<!-- </exclusions>--> | ||
<!-- </dependency>--> | ||
<dependency> | ||
<groupId>com.clickhouse</groupId> | ||
<artifactId>clickhouse-r2dbc</artifactId> | ||
<version>${version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
17 changes: 17 additions & 0 deletions
17
...-webflux-sample/src/main/java/com/clickhouse/r2dbc/spring/webflux/sample/Application.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,17 @@ | ||
package com.clickhouse.r2dbc.spring.webflux.sample; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration; | ||
import org.springframework.web.reactive.config.EnableWebFlux; | ||
|
||
@SpringBootApplication(exclude = { R2dbcAutoConfiguration.class, | ||
R2dbcDataAutoConfiguration.class}) | ||
@EnableWebFlux | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...spring-webflux-sample/src/main/java/com/clickhouse/r2dbc/spring/webflux/sample/Runny.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,26 @@ | ||
package com.clickhouse.r2dbc.spring.webflux.sample; | ||
|
||
import com.clickhouse.r2dbc.spring.webflux.sample.model.ClickStats; | ||
import io.r2dbc.spi.ConnectionFactory; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import static io.r2dbc.spi.ConnectionFactories.get; | ||
import static java.lang.String.format; | ||
|
||
public class Runny { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
ConnectionFactory connectionFactory = get(format("r2dbc:clickhouse:grpc://@%s:%d/%s", "localhost", 9100, "clickdb")); | ||
Flux<ClickStats> clickStatsFlux = Mono.from(connectionFactory.create()).flatMapMany(conn -> conn.createStatement("select domain, path, count(1) from clickdb.clicks where domain = :domain group by domain, path") | ||
.bind("domain", "falan") | ||
.execute()) | ||
.flatMap(result -> result.map((row, rowMetadata) -> new ClickStats(row | ||
.get("domain", String.class), row.get("path", String.class), row.get("cdate", LocalDateTime.class), row.get("count", Long.class)))); | ||
|
||
clickStatsFlux.subscribe(clickStats -> System.out.println(clickStatsFlux)); | ||
Thread.sleep(1000L); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...x-sample/src/main/java/com/clickhouse/r2dbc/spring/webflux/sample/config/R2DBCConfig.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,27 @@ | ||
package com.clickhouse.r2dbc.spring.webflux.sample.config; | ||
|
||
import io.r2dbc.spi.ConnectionFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import static io.r2dbc.spi.ConnectionFactories.get; | ||
import static java.lang.String.format; | ||
|
||
@Configuration | ||
public class R2DBCConfig { | ||
|
||
@Value("${clickhouse.hosdot:localhost}") | ||
private String host; | ||
|
||
@Value("${clickhouse.port:9100}") | ||
private String port; | ||
|
||
@Value("${clickhouse.database:clickdb}") | ||
private String database; | ||
|
||
@Bean | ||
public ConnectionFactory connectionFactory() { | ||
return get(format("r2dbc:clickhouse:grpc://@%s:%d/%s", host, Integer.parseInt(port), database)); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
.../src/main/java/com/clickhouse/r2dbc/spring/webflux/sample/controller/ClickController.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,39 @@ | ||
package com.clickhouse.r2dbc.spring.webflux.sample.controller; | ||
|
||
import com.clickhouse.r2dbc.spring.webflux.sample.model.Click; | ||
import com.clickhouse.r2dbc.spring.webflux.sample.model.ClickStats; | ||
import com.clickhouse.r2dbc.spring.webflux.sample.repository.ClickRepository; | ||
import org.reactivestreams.Publisher; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@RestController | ||
@RequestMapping("/clicks") | ||
public class ClickController { | ||
|
||
@Autowired | ||
ClickRepository clickRepository; | ||
|
||
@GetMapping("/{domain}") | ||
public Publisher<ResponseEntity<List<ClickStats>>> getEmployeeById(@PathVariable("domain") String domain) { | ||
return clickRepository.getStatsByDomain(domain).collect(Collectors.toList()).map(list -> ResponseEntity.ok(list)); | ||
} | ||
|
||
@PostMapping | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public Publisher<ResponseEntity> add(Click click){ | ||
return clickRepository.add(new ClickStats(click.getDomain(), click.getPath(), LocalDateTime.now(), 1)).map(v -> ResponseEntity.ok().build()); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...-webflux-sample/src/main/java/com/clickhouse/r2dbc/spring/webflux/sample/model/Click.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,24 @@ | ||
package com.clickhouse.r2dbc.spring.webflux.sample.model; | ||
|
||
|
||
public class Click { | ||
|
||
String domain; | ||
String path; | ||
|
||
public String getDomain() { | ||
return domain; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public void setDomain(String domain) { | ||
this.domain = domain; | ||
} | ||
|
||
public void setPath(String path) { | ||
this.path = path; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...lux-sample/src/main/java/com/clickhouse/r2dbc/spring/webflux/sample/model/ClickStats.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,50 @@ | ||
package com.clickhouse.r2dbc.spring.webflux.sample.model; | ||
|
||
|
||
import java.time.LocalDateTime; | ||
|
||
public class ClickStats { | ||
private String domain; | ||
private String path; | ||
private LocalDateTime cdate; | ||
private long count; | ||
|
||
public ClickStats(String domain, String path, LocalDateTime date, long count) { | ||
this.domain = domain; | ||
this.path = path; | ||
this.cdate = date; | ||
this.count = count; | ||
} | ||
|
||
public String getDomain() { | ||
return domain; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
public void setDomain(String domain) { | ||
this.domain = domain; | ||
} | ||
|
||
public void setPath(String path) { | ||
this.path = path; | ||
} | ||
|
||
public long getCount() { | ||
return count; | ||
} | ||
|
||
public void setCount(long count) { | ||
this.count = count; | ||
} | ||
|
||
public LocalDateTime getCdate() { | ||
return cdate; | ||
} | ||
|
||
public void setCdate(LocalDateTime cdate) { | ||
this.cdate = cdate; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
.../src/main/java/com/clickhouse/r2dbc/spring/webflux/sample/repository/ClickRepository.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,37 @@ | ||
package com.clickhouse.r2dbc.spring.webflux.sample.repository; | ||
|
||
import com.clickhouse.r2dbc.spring.webflux.sample.model.ClickStats; | ||
import io.r2dbc.spi.ConnectionFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Repository; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
|
||
|
||
@Repository | ||
public class ClickRepository { | ||
|
||
@Autowired | ||
ConnectionFactory connectionFactory; | ||
|
||
public Flux<ClickStats> getStatsByDomain(String domain){ | ||
return Mono.from(connectionFactory.create()) | ||
.flatMapMany(conn -> conn.createStatement("select domain, path, count(1) from clickdb.clicks where domain = :domain group by domain, path") | ||
.bind("domain", domain) | ||
.execute()) | ||
.flatMap(result -> result.map((row, rowMetadata) -> new ClickStats(row | ||
.get("domain", String.class), row.get("path", String.class), row.get("cdate", LocalDateTime.class), row.get("count", Long.class)))); | ||
} | ||
|
||
public Mono<Void> add(ClickStats click){ | ||
return Mono.from(connectionFactory.create()).map(conn -> conn.createStatement("insert into clickdb.clicks values (:domain, :path, :cdate)") | ||
.bind("domain", click.getDomain()) | ||
.bind("path", click.getPath()) | ||
.bind("cdate", click.getCdate()).execute()) | ||
.then(); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...-r2dbc-samples/clickhouse-r2dbc-spring-webflux-sample/src/main/resources/application.yaml
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,5 @@ | ||
clickhouse: | ||
host: localhost | ||
port: 9100 | ||
database: clickdb | ||
debug: true |
9 changes: 9 additions & 0 deletions
9
clickhouse-r2dbc-samples/clickhouse-r2dbc-spring-webflux-sample/src/main/resources/init.sql
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,9 @@ | ||
create table if not exists clickdb.clicks | ||
( | ||
domain String, | ||
path String, | ||
cdate DateTime, | ||
count UInt64 | ||
) | ||
engine = SummingMergeTree(count) | ||
order by (domain, path, cdate); |
4 changes: 4 additions & 0 deletions
4
...-r2dbc-samples/clickhouse-r2dbc-spring-webflux-sample/src/main/resources/log4j.properties
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,4 @@ | ||
log4j.rootCategory=DEBUG, stdout | ||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender | ||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout | ||
log4j.appender.stdout.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss:SSS} %5p %t %c{2}:%L - %m%n |
12 changes: 12 additions & 0 deletions
12
...ouse-r2dbc-samples/misc/docker/clickhouse-docker-mount/config.d/docker_related_config.xml
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,12 @@ | ||
<clickhouse> | ||
<!-- Listen wildcard address to allow accepting connections from other containers and host network. --> | ||
<listen_host>::</listen_host> | ||
<listen_host>0.0.0.0</listen_host> | ||
<listen_try>1</listen_try> | ||
|
||
<!-- | ||
<logger> | ||
<console>1</console> | ||
</logger> | ||
--> | ||
</clickhouse> |
Oops, something went wrong.