8 releases

0.3.5 Dec 17, 2022
0.3.4 Dec 17, 2022
0.3.3 Oct 3, 2022
0.3.2 Sep 7, 2022
0.1.0 Aug 19, 2021

#458 in Parser implementations

Download history 195/week @ 2022-10-11 189/week @ 2022-10-18 160/week @ 2022-10-25 282/week @ 2022-11-01 145/week @ 2022-11-08 131/week @ 2022-11-15 159/week @ 2022-11-22 122/week @ 2022-11-29 170/week @ 2022-12-06 226/week @ 2022-12-13 183/week @ 2022-12-20 117/week @ 2022-12-27 216/week @ 2023-01-03 186/week @ 2023-01-10 185/week @ 2023-01-17 151/week @ 2023-01-24

755 downloads per month
Used in 2 crates

MIT license

150KB
3.5K SLoC

influxdb2

This is a Rust client to InfluxDB using the 2.0 API.

Setup

Add this to cargo.toml:

influxdb2 = "0.3"
influxdb2-structmap = "0.2"
num-traits = "0.2"

Usage

Querying

use chrono::{DateTime, FixedOffset};
use influxdb2::{Client, FromDataPoint};
use influxdb2::models::Query;

#[derive(Debug, FromDataPoint)]
pub struct StockPrice {
    ticker: String,
    value: f64,
    time: DateTime<FixedOffset>,
}

impl Default for StockPrice {
    fn default() -> Self {
        Self {
            ticker: "".to_string(),
            value: 0_f64,
            time: chrono::MIN_DATETIME.with_timezone(&chrono::FixedOffset::east(7 * 3600)),
        }
    }
}

async fn example() -> Result<(), Box<dyn std::error::Error>> {
    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 client = Client::new(host, org, token);

    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(())
}

Writing

async fn example() -> Result<(), Box<dyn std::error::Error>> {
    use futures::prelude::*;
    use influxdb2::models::DataPoint;
    use influxdb2::Client;

    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(())
}

Supported Data Types

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:

  • bool
  • f64
  • i64
  • u64 - DEPRECATED, will be removed in version 0.4
  • String
  • Vec
  • chrono::Duration
  • DateTime

Features

Implemented API

  • Query API
  • Write API
  • Delete API
  • Bucket API (partial: only list, create, delete)
  • Organization API (partial: only list)
  • Task API (partial: only list, create, delete)

TLS Implementations

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.

# Usage for native-tls (the default).
influxdb2 = "0.3"

# Usage for rustls.
influxdb2 = { version = "0.3", features = ["rustls"], default-features = false }

Development Status

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.

Dependencies

~15–24MB
~449K SLoC