Skip to content

Commit

Permalink
sample is added.
Browse files Browse the repository at this point in the history
  • Loading branch information
rernas35 committed Jun 25, 2022
1 parent 22b1656 commit 281c39f
Show file tree
Hide file tree
Showing 20 changed files with 1,771 additions and 1 deletion.
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>
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);
}
}
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);
}
}
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));
}
}
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());
}

}
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;
}
}
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;
}
}
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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
clickhouse:
host: localhost
port: 9100
database: clickdb
debug: true
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);
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
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>
Loading

0 comments on commit 281c39f

Please sign in to comment.