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 real TDengine cloud DSN. To obtain the real value, please log in TDengine Cloud and click "Programming" on the left menu, then select "Rust".
Connect
Copy following code to main.rs
.
use anyhow::Result;
use taos::*;
#[tokio::main]
async fn main() -> Result<()> {
let dsn = std::env::var("TDENGINE_CLOUD_DSN")?;
let taos = TaosBuilder::from_dsn(dsn)?.build()?;
let _ = taos.query("show databases").await?;
println!("Connected");
Ok(())
}
Then you can execute cargo run
to test the connection. For how to write data and query data, please refer to Data In and Tools.
For more details about how to write or query data via REST API, please check REST API.