Skip to content

ctrlaltf24/influxdb-rust

 
 

Repository files navigation


rust-influxdb

Unofficial InfluxDB Driver for Rust

Build Status Coverage Report Documentation Status Build with Rust Minimum Rust Version

This library is a work in progress. This means a feature you might need is not implemented yet or could be handled better.

Pull requests are always welcome. See Contributing and Code of Conduct. For a list of past changes, see CHANGELOG.md.

Currently Supported Features

  • Reading and Writing to InfluxDB
  • Optional Serde Support for Deserialization
  • Running multiple queries in one request (e.g. SELECT * FROM weather_berlin; SELECT * FROM weather_london)
  • Authenticated and Unauthenticated Connections
  • async/await support
  • #[derive(InfluxDbWriteable)] Derive Macro for Writing / Reading into Structs
  • GROUP BY support
  • Tokio and async-std support (see example below) or available backends
  • Swappable HTTP backends (see below)

Quickstart

Add the following to your Cargo.toml

influxdb = { version = "0.4.0", features = ["derive"] }

For an example with using Serde deserialization, please refer to serde_integration

use influxdb::{Client, Query, Timestamp};
use influxdb::InfluxDbWriteable;
use chrono::{DateTime, Utc};

#[tokio::main]
// or #[async_std::main] if you prefer
async fn main() {
    // Connect to db `test` on `http://localhost:8086`
    let client = Client::new("http://localhost:8086", "test");

    #[derive(InfluxDbWriteable)]
    struct WeatherReading {
        time: DateTime<Utc>,
        humidity: i32,
        #[influxdb(tag)] wind_direction: String,
    }

    // Let's write some data into a measurement called `weather`
    let weather_reading = WeatherReading {
        time: Timestamp::Hours(1).into(),
        humidity: 30,
        wind_direction: String::from("north"),
    };

    let write_result = client
        .query(&weather_reading.into_query("weather"))
        .await;
    assert!(write_result.is_ok(), "Write result was not okay");

    // Let's see if the data we wrote is there
    let read_query = Query::raw_read_query("SELECT * FROM weather");

    let read_result = client.query(&read_query).await;
    assert!(read_result.is_ok(), "Read result was not ok");
    println!("{}", read_result.unwrap());
}

For further examples, check out the Integration Tests in tests/integration_tests.rs in the repository.

Choice of HTTP backend

To communicate with InfluxDB, you can choose the HTTP backend to be used configuring the appropriate feature. We recommend sticking with the default reqwest-based client, unless you really need async-std compatibility.

  • hyper (through reqwest, used by default), with rustls

    influxdb = { version = "0.4.0", features = ["derive"] }
  • hyper (through reqwest), with native TLS (OpenSSL)

    influxdb = { version = "0.4.0", default-features = false, features = ["derive", "use-serde", "reqwest-client"] }
  • hyper (through surf), use this if you need tokio 0.2 compatibility

    influxdb = { version = "0.4.0", default-features = false, features = ["derive", "use-serde", "curl-client"] }
  • curl, using libcurl

    influxdb = { version = "0.4.0", default-features = false, features = ["derive", "use-serde", "curl-client"] }
  • async-h1 with native TLS (OpenSSL)

    influxdb = { version = "0.4.0", default-features = false, features = ["derive", "use-serde", "h1-client"] }
  • async-h1 with rustls

    influxdb = { version = "0.4.0", default-features = false, features = ["derive", "use-serde", "h1-client-rustls"] }
  • WebAssembly's window.fetch, via web-sys and wasm-bindgen

    influxdb = { version = "0.4.0", default-features = false, features = ["derive", "use-serde", "wasm-client"] }

License

License: MIT

@ 2020 Gero Gerke and contributors.

About

Rust Client for the InfluxDB Time Series Database

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 99.0%
  • Other 1.0%