|
1 | 1 | # ClickHouse-Java Demo Service |
2 | 2 |
|
3 | 3 | ## About |
4 | | -This is an example of a Spring Boot application using different ClickHouse-Java clients and features. |
| 4 | +This is and example of a Spring Boot service using ClickHouse client directly and via JPA. |
| 5 | +Example is an application that requires ClickHouse DB running externally. It can be a Docker or |
| 6 | +ClickHouse Cloud instance. |
5 | 7 |
|
| 8 | +## How to Run |
6 | 9 |
|
7 | | -## Usage |
| 10 | +### Initialize DB |
| 11 | +Set up a ClickHouse instance. |
8 | 12 |
|
9 | | -This example requires an instance of ClickHouse running locally on in remote server. |
10 | | -Application uses `system.numbers` table to generate dataset of any size. It is very convenient because: |
11 | | -- data is already there |
12 | | -- server generates data as if it was a real table |
13 | | -- no need to change schema or create tables |
| 13 | +Example of running with Docker: |
| 14 | +```shell |
| 15 | +docker run -d --name demo-service-db -e CLICKHOUSE_USER=default -e CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1 -e CLICKHOUSE_PASSWORD=secret\ |
| 16 | + -p 8123:8123 clickhouse/clickhouse-server |
14 | 17 |
|
15 | | -Run clickhouse instance in docker: |
| 18 | +docker ps |
| 19 | +# output should be like: |
| 20 | +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
| 21 | +2abfefc40a84 clickhouse/clickhouse-server "/entrypoint.sh" 4 seconds ago Up 2 seconds 9000/tcp, 0.0.0.0:8123->8123/tcp, :::8123->8123/tcp, 9009/tcp demo-service-db |
16 | 22 | ``` |
17 | | -docker run --rm -e CLICKHOUSE_USER=default -e CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1 -e CLICKHOUSE_PASSWORD=secret\ |
18 | | - -p 8123:8123 clickhouse/clickhouse-server |
19 | 23 |
|
| 24 | +Example how to run client: |
| 25 | +```shell |
| 26 | +docker exec -it demo-service-db clickhouse-client |
20 | 27 | ``` |
21 | 28 |
|
22 | | -Create table: |
| 29 | +Create table (needed for JPA example): |
23 | 30 | ``` |
24 | 31 | CREATE TABLE ui_events |
25 | 32 | ( |
26 | | - `id` UUID, |
| 33 | + `id` String, |
27 | 34 | `timestamp` DateTime64, |
28 | | - `eventName` String |
| 35 | + `event_name` String, |
| 36 | + `tags` Array(String) |
29 | 37 | ) |
30 | 38 | ENGINE = MergeTree |
31 | 39 | ORDER BY timestamp |
32 | 40 | ``` |
33 | 41 |
|
34 | | -To run |
| 42 | +### Run Demo-Service |
35 | 43 |
|
36 | 44 | ```shell |
37 | 45 | ./gradlew bootRun |
38 | 46 | ``` |
39 | 47 |
|
40 | | -To test |
| 48 | +### Interact with API |
| 49 | + |
| 50 | +#### Direct Client |
41 | 51 |
|
| 52 | +To read `limit` number of rows from `system.numbers`: |
42 | 53 | ```shell |
43 | 54 | curl http://localhost:8080/direct/dataset/0?limit=100000 |
44 | 55 | ``` |
45 | 56 |
|
46 | | -JPA Insert |
| 57 | +#### JPA |
| 58 | + |
| 59 | +To insert some data: |
47 | 60 | ```shell |
48 | | - curl -v -X POST -H "Content-Type: application/json" -d '{"id": "123", "timestamp": "2025-04-07T14:30:00.000Z", "eventName": "Login", "tags": ["security", "activity"]}' http://localhost:8080/events/ui_events |
49 | | -``` |
50 | | -JPA Select |
51 | | -```shell |
52 | | - curl -v -X GET http://localhost:8080/events/ui_events |
| 61 | +curl -v -X POST -H "Content-Type: application/json" \ |
| 62 | + -d '{"id": "4NAD7B8HH1", "timestamp": "2025-04-07T14:30:00.000Z", "eventName": "Login", "tags": ["security", "activity"]}'\ |
| 63 | + http://localhost:8080/events/ui_events |
53 | 64 | ``` |
54 | 65 |
|
55 | | -## Features |
56 | | - |
57 | | -- [x] Client V2 New Implementation |
| 66 | +To fetch inserted data: |
| 67 | +```shell |
| 68 | +curl -v -X GET http://localhost:8080/events/ui_events |
| 69 | +``` |
0 commit comments