This is a Rust client to InfluxDB using the 2.0 API.
Add this to cargo.toml
:
toml
influxdb2 = "0.3"
influxdb2-structmap = "0.2"
num-traits = "0.2"
```rust use chrono::{DateTime, FixedOffset}; use influxdb2::{Client, FromDataPoint}; use influxdb2::models::Query;
pub struct StockPrice {
ticker: String,
value: f64,
time: DateTime
impl Default for StockPrice { fn default() -> Self { Self { ticker: "".tostring(), value: 0f64, time: chrono::MINDATETIME.withtimezone(&chrono::FixedOffset::east(7 * 3600)), } } }
async fn example() -> Result<(), Box
let qs = format!("from(bucket: \"stock-prices\")
|> range(start: -1w)
|> filter(fn: (r) => r.ticker == \"{}\")
|> last()
", "AAPL");
let query = Query::new(qs.to_string());
let res: Vec<StockPrice> = client.query::<StockPrice>(Some(query))
.await?;
println!("{:?}", res);
Ok(())
} ```
```rust
async fn example() -> Result<(), Box
let host = std::env::var("INFLUXDB_HOST").unwrap();
let org = std::env::var("INFLUXDB_ORG").unwrap();
let token = std::env::var("INFLUXDB_TOKEN").unwrap();
let bucket = "bucket";
let client = Client::new(host, org, token);
let points = vec![
DataPoint::builder("cpu")
.tag("host", "server01")
.field("usage", 0.5)
.build()?,
DataPoint::builder("cpu")
.tag("host", "server01")
.tag("region", "us-west")
.field("usage", 0.87)
.build()?,
];
client.write(bucket, stream::iter(points)).await?;
Ok(())
} ```
InfluxDB data point doesn't support every data types supported by Rust. So, the derive macro only allows for a subset of data types which is also supported in InfluxDB.
Supported struct field types:
Implemented API
This crate uses reqwest under the hood.
You can choose between native-tls
and rustls
with the features provided with this crate.
native-tls
is chosen as the default, like reqwest does.
```toml
influxdb2 = "0.3"
influxdb2 = { version = "0.3", features = ["rustls"], default-features = false } ```
This project is still at alpha status and all the bugs haven't been ironed yet. With that said, use it at your own risk and feel free to create an issue or pull request.