Connect with Rust
Create Project
cargo new --bin cloud-example
Add Dependency
Add dependency to Cargo.toml
.
[package]
name = "cloud-example"
version = "0.1.0"
edition = "2021"
[dependencies]
taos = { version = "*", default-features = false, features = ["ws"] }
tokio = { version = "1", features = ["full"]}
anyhow = "1.0.0"
Config
Run this command in your terminal to save TDengine cloud DSN as variables:
- Bash
- CMD
- Powershell
export TDENGINE_CLOUD_DSN="<DSN>"
set TDENGINE_CLOUD_DSN=<DSN>
$env:TDENGINE_CLOUD_DSN='<DSN>'
Replace <DSN> with value of the TDengine Cloud instance you wish to access, which should be in the format wss://<cloud_endpoint>?token=<token>
.
To obtain the real dsn value, please log in TDengine Cloud and click "Programming" on the left menu, then select "Rust".
Connect
Copy following code to main.rs
.Then you can execute cargo run
to test the connection.
use anyhow::Result;
use taos::*;
#[tokio::main]
async fn main() -> Result<()> {
let dsn = std::env::var("TDENGINE_CLOUD_DSN")?;
println!("dsn: {}", dsn);
let taos = TaosBuilder::from_dsn(dsn)?.build().await?;
let mut res = taos.query("show databases").await?;
res.rows().try_for_each(|row| async {
println!("{}", row.into_value_iter().join(","));
Ok(())
}).await?;
Ok(())
}
For how to write data and query data, please refer to Insert and Query.
For more details about how to write or query data via REST API, please check REST API.