Skip to content

Commit

Permalink
[samples](rust) add rust stream load sample (apache#38141)
Browse files Browse the repository at this point in the history
add rust stream load sample
  • Loading branch information
zy-kkk authored Jul 22, 2024
1 parent 4cc4944 commit dcc88e5
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
16 changes: 16 additions & 0 deletions samples/stream_load/rust/.gitignore
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

33 changes: 33 additions & 0 deletions samples/stream_load/rust/Cargo.toml
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"
48 changes: 48 additions & 0 deletions samples/stream_load/rust/README.md
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.
63 changes: 63 additions & 0 deletions samples/stream_load/rust/src/doris_stream_load.rs
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(())
}

0 comments on commit dcc88e5

Please sign in to comment.