forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[samples](rust) add rust stream load sample (apache#38141)
add rust stream load sample
- Loading branch information
Showing
4 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
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,16 @@ | ||
### Rust template | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
debug/ | ||
target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
# MSVC Windows builds of rustc generate these, which store debugging information | ||
*.pdb | ||
|
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,33 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
[package] | ||
name = "doris_stream_load" | ||
version = "0.1.0" | ||
edition = "2021" | ||
description = "A Rust client for performing stream loads into Apache Doris." | ||
readme = "README.md" | ||
|
||
# Binary configuration | ||
[[bin]] | ||
name = "doris_stream_load" | ||
path = "src/doris_stream_load.rs" | ||
|
||
[dependencies] | ||
reqwest = { version = "0.11", features = ["json"] } | ||
tokio = { version = "1", features = ["full"] } | ||
base64 = "0.13" |
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,48 @@ | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one | ||
or more contributor license agreements. See the NOTICE file | ||
distributed with this work for additional information | ||
regarding copyright ownership. The ASF licenses this file | ||
to you under the Apache License, Version 2.0 (the | ||
"License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
--> | ||
|
||
# Doris Stream Load in Rust | ||
|
||
## How to use: | ||
|
||
1. **Create a table in Doris:** | ||
```sql | ||
CREATE TABLE `db0`.`t_user` ( | ||
`id` int, | ||
`name` string | ||
) | ||
DUPLICATE KEY(`id`) | ||
DISTRIBUTED BY HASH(`id`) BUCKETS 1 | ||
PROPERTIES ( | ||
"replication_num" = "1" | ||
); | ||
``` | ||
|
||
2. Build the project: | ||
```shell | ||
cargo build | ||
``` | ||
|
||
3. Run the application: | ||
```shell | ||
cargo run | ||
``` | ||
|
||
## What can this demo do: | ||
This is a rust demo for doris stream load, you can use this code load your data into doris by stream load. |
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,63 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE, AUTHORIZATION, EXPECT}; | ||
use base64::encode; | ||
use std::error::Error; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn Error>> { | ||
let doris_host = "127.0.0.1"; | ||
let doris_port = "8030"; | ||
let doris_db = "db0"; | ||
let doris_table = "t_user"; | ||
let doris_user = "root"; | ||
let doris_password = ""; | ||
|
||
let url = format!( | ||
"http://{}:{}/api/{}/{}/_stream_load", | ||
doris_host, doris_port, doris_db, doris_table | ||
); | ||
|
||
let auth = format!("{}:{}", doris_user, doris_password); | ||
let encoded_auth = encode(auth); | ||
let auth_header_value = format!("Basic {}", encoded_auth); | ||
|
||
let mut headers = HeaderMap::new(); | ||
headers.insert(CONTENT_TYPE, HeaderValue::from_static("text/plain; charset=UTF-8")); | ||
headers.insert("format", HeaderValue::from_static("csv")); | ||
headers.insert("column_separator", HeaderValue::from_static(",")); | ||
headers.insert(AUTHORIZATION, HeaderValue::from_str(&auth_header_value)?); | ||
headers.insert(EXPECT, HeaderValue::from_static("100-continue")); | ||
|
||
let client = reqwest::Client::new(); | ||
let response = client | ||
.put(url) | ||
.headers(headers) | ||
.body("1,Tom\n2,Jelly") | ||
.send() | ||
.await?; | ||
|
||
if response.status().is_success() { | ||
let resp_text = response.text().await?; | ||
println!("Response: {}", resp_text); | ||
} else { | ||
println!("Failed to load stream: {}", response.status()); | ||
} | ||
|
||
Ok(()) | ||
} |