Skip to content

Commit 20a7dbe

Browse files
Merge pull request #2 from timeplus-io/refine_examples
Refine examples
2 parents 4cc164e + 5ffa5fa commit 20a7dbe

File tree

10 files changed

+55
-121
lines changed

10 files changed

+55
-121
lines changed

Handover.md

Lines changed: 0 additions & 78 deletions
This file was deleted.

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ This client uses https://crates.io/crates/clickhouse as a dependency.
1414

1515
## Install Proton
1616

17+
Please install Proton as a standalone server or via Docker. Make sure port 8123 is exposed for `pront-rust-client` to connect and run SQL.
18+
1719
### As a single binary
1820

1921
On Linux or Mac, you can install it via `curl https://install.timeplus.com | sh`
@@ -27,11 +29,10 @@ In a separate terminal, connect to the server via `proton client` (Note: If you
2729
### As a Docker container
2830

2931
```bash
30-
docker run -d --pull always --name proton ghcr.io/timeplus-io/proton:latest
32+
docker run -d --pull always --name proton -p 8123:8123 -p 8463:8463 ghcr.io/timeplus-io/proton:latest
3133
```
3234

33-
Proton is automatically started. Open the terminal of the container, and run `proton client`
34-
35+
Proton is automatically started with port 8123 and 8463 exposed. Open the terminal of the container, and run `proton client`
3536

3637
For detailed usage and more information, check out the documentation: https://docs.timeplus.com/proton
3738

@@ -77,21 +78,28 @@ const FN_NAME: &str = "[prepare]:";
7778

7879
#[tokio::main]
7980
async fn main() -> Result<()> {
80-
println!("{} Start", FN_NAME);
81+
println!("{}Start", FN_NAME);
8182

8283
println!("{}Build client", FN_NAME);
8384
let client = ProtonClient::new("http://localhost:8123");
8485

85-
println!("{} Create stream if not exists", FN_NAME);
86+
println!("{}Create stream if not exists", FN_NAME);
8687
create_stream(&client)
8788
.await
8889
.expect("[main]: Failed to create Stream");
8990

90-
println!("{} Stop", FN_NAME);
91+
println!("{}Stop", FN_NAME);
9192
Ok(())
92-
}
93+
}
94+
95+
pub async fn create_stream(client: &ProtonClient) -> Result<()> {
96+
client
97+
.execute_query("CREATE STREAM IF NOT EXISTS test_stream(no uint32, name string) ORDER BY no")
98+
.await
99+
}
93100
```
94101

102+
Check more examples [here](examples).
95103

96104
## What's next?
97105

examples/README_EXAMPLES.md renamed to examples/README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
## Install Proton
77

8+
Please install Proton as a standalone server or via Docker. Make sure port 8123 is exposed for `pront-rust-client` to connect and run SQL.
9+
810
### As a single binary
911

1012
On Linux or Mac, you can install it via `curl https://install.timeplus.com | sh`
@@ -18,10 +20,10 @@ In a separate terminal, connect to the server via `proton client` (Note: If you
1820
### As a Docker container
1921

2022
```bash
21-
docker run -d --pull always --name proton ghcr.io/timeplus-io/proton:latest
23+
docker run -d --pull always --name proton -p 8123:8123 -p 8463:8463 ghcr.io/timeplus-io/proton:latest
2224
```
2325

24-
Proton is automatically started. Open the terminal of the container, and run `proton client`
26+
Proton is automatically started with port 8123 and 8463 exposed. Open the terminal of the container, and run `proton client`
2527

2628

2729
For detailed usage and more information, check out the documentation: https://docs.timeplus.com/proton
@@ -58,7 +60,7 @@ const FN_NAME: &str = "[prepare]:";
5860
async fn main() -> Result<()> {
5961
println!("{} Start", FN_NAME);
6062

61-
println!("{}Build client", FN_NAME);
63+
println!("{} Build client", FN_NAME);
6264
let client = ProtonClient::new("http://localhost:8123");
6365

6466
println!("{} Create stream if not exists", FN_NAME);
@@ -74,28 +76,30 @@ async fn main() -> Result<()> {
7476

7577
## Run the client code example
7678

79+
In the root folder of `protno-rust-client`
80+
7781
1) Create a stream and insert some data
7882

7983
```
80-
cargo run --bin prepare
84+
cargo run --example prepare
8185
```
8286

8387
Expected output
8488

8589
```
86-
[prepare]: Start
90+
[prepare]:Start
8791
[prepare]:Build client
88-
[prepare]: Create stream if not exists
92+
[prepare]:Create stream if not exists
8993
[prepare]:Insert data
9094
[prepare]:Count inserted data
9195
[prepare]:Inserted data: 1000
92-
[prepare]: Stop
96+
[prepare]:Stop
9397
```
9498

9599
2) Stream some data (fetch) and load all data at once (fetch_all)
96100

97101
```
98-
cargo run --bin main
102+
cargo run --example query
99103
```
100104

101105
Expected output
@@ -110,21 +114,21 @@ MyRow { no: 503, name: "foo" }
110114
MyRow { no: 504, name: "foo" }
111115
[main]:Fetch all data
112116
[MyRowOwned { no: 500, name: "foo" }, MyRowOwned { no: 501, name: "foo" }, MyRowOwned { no: 502, name: "foo" }, MyRowOwned { no: 503, name: "foo" }, MyRowOwned { no: 504, name: "foo" }]
113-
[main]: Stop
117+
[main]:Stop
114118
```
115119

116120
3) Cleanup and delete stream
117121

118122

119123
```
120-
cargo run --bin cleanup
124+
cargo run --example remove
121125
```
122126

123127
Expected output
124128

125129
```
126-
[prepare]: Start
130+
[prepare]:Start
127131
[prepare]:Build client
128-
[prepare]: Delete Stream
129-
[prepare]: Stop
132+
[prepare]:Delete Stream
133+
[prepare]:Stop
130134
```

examples/prepare/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ impl<'a> MyRow<'a> {
1717

1818
#[tokio::main]
1919
async fn main() -> Result<()> {
20-
println!("{} Start", FN_NAME);
20+
println!("{}Start", FN_NAME);
2121

2222
println!("{}Build client", FN_NAME);
2323
let client = ProtonClient::new("http://localhost:8123");
2424

25-
println!("{} Create stream if not exists", FN_NAME);
25+
println!("{}Create stream if not exists", FN_NAME);
2626
create_stream(&client)
2727
.await
2828
.expect("[main]: Failed to create Stream");
@@ -39,19 +39,19 @@ async fn main() -> Result<()> {
3939

4040
println!("{}Inserted data: {}", FN_NAME, count);
4141

42-
println!("{} Stop", FN_NAME);
42+
println!("{}Stop", FN_NAME);
4343
Ok(())
4444
}
4545

4646
pub async fn create_stream(client: &ProtonClient) -> Result<()> {
4747
client
48-
.execute_query("CREATE STREAM IF NOT EXISTS some(no uint32, name string) ORDER BY no")
48+
.execute_query("CREATE STREAM IF NOT EXISTS test_stream(no uint32, name string) ORDER BY no")
4949
.await
5050
}
5151

5252
pub async fn insert(client: &ProtonClient) -> Result<()> {
5353
let mut insert = client
54-
.insert("some")
54+
.insert("test_stream")
5555
.await
5656
.expect("[main/insert]: Failed to build inserter for table some");
5757

@@ -70,7 +70,7 @@ pub async fn insert(client: &ProtonClient) -> Result<()> {
7070
pub async fn select_count(client: &ProtonClient) -> Result<u64> {
7171
let count = client
7272
.clone()
73-
.fetch_one("select count() from table(some)")
73+
.fetch_one("select count() from table(test_stream)")
7474
.await
7575
.expect("[main/select_count]: Failed to fetch count()");
7676

examples/query/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ async fn main() -> Result<()> {
5050
.await
5151
.expect("[main/fetch_all]: Failed to fetch data");
5252

53-
println!("{} Stop", FN_NAME);
53+
println!("{}Stop", FN_NAME);
5454
Ok(())
5555
}
5656

5757
pub async fn fetch(client: &ProtonClient) -> clickhouse::error::Result<()> {
5858
let mut cursor = client
59-
.fetch::<MyRow<'_>>("SELECT ?fields from table(some) WHERE no BETWEEN 500 AND 504")
59+
.fetch::<MyRow<'_>>("SELECT ?fields from test_stream WHERE no BETWEEN 500 AND 504")
6060
.await
6161
.expect("[main/fetch]: Failed to fetch data");
6262

@@ -69,7 +69,7 @@ pub async fn fetch(client: &ProtonClient) -> clickhouse::error::Result<()> {
6969

7070
pub async fn fetch_all(client: &ProtonClient) -> clickhouse::error::Result<()> {
7171
let vec = client
72-
.fetch_all::<MyRowOwned>("SELECT ?fields from table(some) WHERE no BETWEEN 500 AND 504")
72+
.fetch_all::<MyRowOwned>("SELECT ?fields from test_stream WHERE no BETWEEN 500 AND 504")
7373
.await
7474
.expect("[main/fetch_all]: Failed to fetch all");
7575

examples/remove/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ const FN_NAME: &str = "[prepare]:";
44

55
#[tokio::main]
66
async fn main() -> Result<()> {
7-
println!("{} Start", FN_NAME);
7+
println!("{}Start", FN_NAME);
88

99
println!("{}Build client", FN_NAME);
1010
let client = ProtonClient::new("http://localhost:8123");
1111

12-
println!("{} Delete Stream", FN_NAME);
12+
println!("{}Delete Stream", FN_NAME);
1313
delete_stream(&client)
1414
.await
1515
.expect("[main]: Failed to delete Stream");
1616

17-
println!("{} Stop", FN_NAME);
17+
println!("{}Stop", FN_NAME);
1818

1919
Ok(())
2020
}
2121

2222
pub async fn delete_stream(client: &ProtonClient) -> Result<()> {
2323
// Drop a stream
2424
// https://docs.timeplus.com/proton-drop-stream
25-
client.execute_query("DROP STREAM some").await
25+
client.execute_query("DROP STREAM IF EXISTS test_stream").await
2626
}

src/lib/fetch.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl ProtonClient {
2323
/// let client = ProtonClient::new("http://localhost:8123");
2424
///
2525
/// let mut cursor = client
26-
/// .fetch::<MyRow<'_>>("SELECT ?fields from table(some) WHERE no BETWEEN 500 AND 504")
26+
/// .fetch::<MyRow<'_>>("SELECT ?fields from table(test_stream) WHERE no BETWEEN 500 AND 504")
2727
/// .await
2828
/// .expect("[main/fetch]: Failed to fetch data");
2929
///
@@ -64,7 +64,7 @@ impl ProtonClient {
6464
///
6565
/// let client = ProtonClient::new("http://localhost:8123");
6666
///
67-
/// let query = "SELECT ?fields FROM some WHERE no BETWEEN 0 AND 1";
67+
/// let query = "SELECT ?fields FROM test_stream WHERE no BETWEEN 0 AND 1";
6868
/// let data = client.fetch_all::<MyRow>(query).await.unwrap();
6969
///
7070
/// println!("Received {} records", data.len());
@@ -100,7 +100,7 @@ impl ProtonClient {
100100
/// async fn example() -> Result<()> {
101101
///
102102
/// let client = ProtonClient::new("http://localhost:8123");
103-
/// let query = "select count() from table(table_name)";
103+
/// let query = "select count() from table(test_stream)";
104104
/// let item = client.fetch_one::<u64>(query).await.unwrap();
105105
///
106106
/// println!("Single result: {:#?}", item);
@@ -143,7 +143,7 @@ impl ProtonClient {
143143
///
144144
/// let client = ProtonClient::new("http://localhost:8123");
145145
/// let item_id = 42;
146-
/// let query = "SELECT ?fields FROM some WHERE no = 42";
146+
/// let query = "SELECT ?fields FROM test_stream WHERE no = 42";
147147
/// let item = client.fetch_optional::<MyRow>(query).await.unwrap();
148148
///
149149
/// match item {

src/lib/getters.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl ProtonClient {
1414
///
1515
/// async fn example() -> Result<()> {
1616
///
17-
/// let client = ProtonClient::new("https://api.proton.com");
17+
/// let client = ProtonClient::new("http://localhost:8123");
1818
///
1919
/// let url = client.client().await;
2020
///
@@ -37,11 +37,11 @@ impl ProtonClient {
3737
///
3838
/// async fn example() -> Result<()> {
3939
///
40-
/// let client = ProtonClient::new("https://api.proton.com");
40+
/// let client = ProtonClient::new("http://localhost:8123");
4141
///
4242
/// let url = client.url().await;
4343
///
44-
/// assert_eq!(url, "https://api.proton.com");
44+
/// assert_eq!(url, "http://localhost:8123");
4545
///
4646
/// Ok(())
4747
/// }

0 commit comments

Comments
 (0)