Rust语言从入门到精通系列 - InfluxDB 2.x实战教程

Rust是一种体系编程言语,它具有高性能、内存安全和并发性等特点。InfluxDB是一个开源的时序数据库,它专门用于存储和查询时间序列数据。InfluxDB 2.x是InfluxDB的新版别,它供给了更好的性能和更好的用户体验。Rust言语供给了InfluxDB 2.x的官方客户端库,能够方便地在Rust项目中运用InfluxDB 2.x。

本教程将介绍如安在Rust项目中运用InfluxDB 2.x,包括根底用法和进阶用法。咱们将供给示例代码,协助读者更好地了解和运用InfluxDB 2.x。

InfluxDB 2.x版别重写了查询体系, 引入了全新的Flux言语查询。比较于SQL,个人感觉是一种让步。具体的Flux语法参考官方文档。

根底用法

创立数据库

在运用InfluxDB 2.x之前,需求先创立一个数据库。能够运用InfluxDB 2.x的Web界面或命令行东西来创立数据库。在本教程中,咱们将运用命令行东西来创立数据库。

use influxdb2::Client;
use influxdb2::models::CreateDatabaseRequest;
fn main() {
    let client = Client::new("http://localhost:8086", "my-token");
    let db_name = "my-db";
    let request = CreateDatabaseRequest::new(db_name);
    client.create_database(request).unwrap();
}

写入数据

写入数据是InfluxDB 2.x的主要功能之一。能够运用InfluxDB 2.x的客户端库来写入数据。在写入数据之前,需求先创立一个Bucket。

use influxdb2::Client;
use influxdb2::models::{CreateBucketRequest, WritePrecision, Point, FieldValue};
fn main() {
    let client = Client::new("http://localhost:8086", "my-token");
    let bucket_name = "my-bucket";
    let org_id = "my-org";
    let request = CreateBucketRequest::new(bucket_name, org_id);
    client.create_bucket(request).unwrap();
    let point = Point::new("my-measurement")
        .add_field("my-field", FieldValue::Integer(1))
        .add_tag("my-tag", "my-value")
        .timestamp(1626464400000, WritePrecision::Ms);
    let points = vec![point];
    client.write_points(bucket_name, points).unwrap();
}

查询数据

查询数据是InfluxDB 2.x的另一个主要功能。能够运用InfluxDB 2.x的客户端库来查询数据。在查询数据之前,需求先创立一个查询句子。

use influxdb2::Client;
use influxdb2::models::{QueryRequest, Query, QueryType};
fn main() {
    let client = Client::new("http://localhost:8086", "my-token");
    let query = Query::new("SELECT * FROM my-measurement");
    let request = QueryRequest::new(query, QueryType::Flux);
    let result = client.query(request).unwrap();
    println!("{:?}", result);
}

删去数据

删去数据是InfluxDB 2.x的另一个功能。能够运用InfluxDB 2.x的客户端库来删去数据。在删去数据之前,需求先创立一个删去句子。

use influxdb2::Client;
use influxdb2::models::{DeleteRequest, Predicate};
fn main() {
    let client = Client::new("http://localhost:8086", "my-token");
    let predicate = Predicate::new("my-tag", "my-value");
    let request = DeleteRequest::new("my-measurement", predicate);
    client.delete(request).unwrap();
}

创立用户

在运用InfluxDB 2.x之前,需求先创立一个用户。能够运用InfluxDB 2.x的Web界面或命令行东西来创立用户。在本教程中,咱们将运用命令行东西来创立用户。

use influxdb2::Client;
use influxdb2::models::{CreateUserRequest, UserPermission};
fn main() {
    let client = Client::new("http://localhost:8086", "my-token");
    let username = "my-user";
    let password = "my-password";
    let request = CreateUserRequest::new(username, password);
    client.create_user(request).unwrap();
    let permission = UserPermission::new("my-bucket", "read");
    client.add_permission(username, permission).unwrap();
}

创立授权令牌

在运用InfluxDB 2.x之前,需求先创立一个授权令牌。能够运用InfluxDB 2.x的Web界面或命令行东西来创立授权令牌。在本教程中,咱们将运用命令行东西来创立授权令牌。

use influxdb2::Client;
use influxdb2::models::{CreateTokenRequest, Permission};
fn main() {
    let client = Client::new("http://localhost:8086", "my-token");
    let request = CreateTokenRequest::new(vec![Permission::new("my-bucket", "read")]);
    let result = client.create_token(request).unwrap();
    println!("{:?}", result);
}

创立使命

在InfluxDB 2.x中,使命是一种自动化的操作。能够运用InfluxDB 2.x的Web界面或命令行东西来创立使命。在本教程中,咱们将运用命令行东西来创立使命。

use influxdb2::Client;
use influxdb2::models::{CreateTaskRequest, Cron};
fn main() {
    let client = Client::new("http://localhost:8086", "my-token");
    let query = "SELECT * FROM my-measurement";
    let cron = Cron::new("0 * * * * * *");
    let request = CreateTaskRequest::new("my-task", query, cron);
    client.create_task(request).unwrap();
}

进阶用法

运用Flux查询言语

Flux是InfluxDB 2.x的查询言语,它供给了更强大的查询功能。能够运用InfluxDB 2.x的客户端库来查询Flux句子。

use influxdb2::Client;
use influxdb2::models::{QueryRequest, Query, QueryType};
fn main() {
    let client = Client::new("http://localhost:8086", "my-token");
    let query = Query::new("from(bucket:\"my-bucket\") |> range(start: -1h) |> filter(fn: (r) => r._measurement == \"my-measurement\") |> limit(n: 10)");
    let request = QueryRequest::new(query, QueryType::Flux);
    let result = client.query(request).unwrap();
    println!("{:?}", result);
}

运用Task API创立使命

能够运用Task API来创立使命,这样能够更方便地办理使命。能够运用InfluxDB 2.x的客户端库来创立使命。

use influxdb2::Client;
use influxdb2::models::{CreateTaskRequest, Cron, TaskStatus};
fn main() {
    let client = Client::new("http://localhost:8086", "my-token");
    let query = "SELECT * FROM my-measurement";
    let cron = Cron::new("0 * * * * * *");
    let request = CreateTaskRequest::new("my-task", query, cron);
    client.create_task(request).unwrap();
    let status = TaskStatus::Inactive;
    client.update_task_status("my-task", status).unwrap();
}

运用Write API批量写入数据

能够运用Write API来批量写入数据,这样能够进步写入数据的功率。能够运用InfluxDB 2.x的客户端库来批量写入数据。

use influxdb2::Client;
use influxdb2::models::{WriteRequest, WritePrecision, Point, FieldValue};
fn main() {
    let client = Client::new("http://localhost:8086", "my-token");
    let bucket_name = "my-bucket";
    let point1 = Point::new("my-measurement")
        .add_field("my-field", FieldValue::Integer(1))
        .add_tag("my-tag", "my-value")
        .timestamp(1626464400000, WritePrecision::Ms);
    let point2 = Point::new("my-measurement")
        .add_field("my-field", FieldValue::Integer(2))
        .add_tag("my-tag", "my-value")
        .timestamp(1626464401000, WritePrecision::Ms);
    let points = vec![point1, point2];
    let request = WriteRequest::new(points);
    client.write(request).unwrap();
}

运用Query API查询数据

能够运用Query API来查询数据,这样能够更方便地查询数据。能够运用InfluxDB 2.x的客户端库来查询数据。

use influxdb2::Client;
use influxdb2::models::{QueryRequest, Query, QueryType};
fn main() {
    let client = Client::new("http://localhost:8086", "my-token");
    let query = Query::new("from(bucket:\"my-bucket\") |> range(start: -1h) |> filter(fn: (r) => r._measurement == \"my-measurement\") |> limit(n: 10)");
    let request = QueryRequest::new(query, QueryType::Flux);
    let result = client.query(request).unwrap();
    println!("{:?}", result);
}

最佳实践

运用环境变量存储认证信息

在实践运用中,通常不会将认证信息硬编码到代码中。能够运用环境变量来存储认证信息,这样能够更安全地办理认证信息。

use influxdb2::Client;
use influxdb2::models::CreateDatabaseRequest;
use std::env;
fn main() {
    let url = env::var("INFLUXDB_URL").unwrap();
    let token = env::var("INFLUXDB_TOKEN").unwrap();
    let client = Client::new(&url, &token);
    let db_name = "my-db";
    let request = CreateDatabaseRequest::new(db_name);
    client.create_database(request).unwrap();
}

运用Rust的异步编程模型

在实践运用中,通常需求处理大量的数据。能够运用Rust的异步编程模型来进步数据处理的功率。

use influxdb2::Client;
use influxdb2::models::{WriteRequest, WritePrecision, Point, FieldValue};
use futures::executor::block_on;
async fn write_data(client: &Client) {
    let bucket_name = "my-bucket";
    let point1 = Point::new("my-measurement")
        .add_field("my-field", FieldValue::Integer(1))
        .add_tag("my-tag", "my-value")
        .timestamp(1626464400000, WritePrecision::Ms);
    let point2 = Point::new("my-measurement")
        .add_field("my-field", FieldValue::Integer(2))
        .add_tag("my-tag", "my-value")
        .timestamp(1626464401000, WritePrecision::Ms);
    let points = vec![point1, point2];
    let request = WriteRequest::new(points);
    client.write(request).await.unwrap();
}
fn main() {
    let url = "http://localhost:8086";
    let token = "my-token";
    let client = Client::new(url, token);
    let future = write_data(&client);
    block_on(future);
}

运用连接池进步功率

在实践运用中,通常需求处理大量的恳求。能够运用连接池来进步恳求处理的功率。

use influxdb2::Client;
use influxdb2::models::CreateDatabaseRequest;
use r2d2::Pool;
use r2d2_influxdb2::InfluxDB2ConnectionManager;
fn main() {
    let url = "http://localhost:8086";
    let token = "my-token";
    let manager = InfluxDB2ConnectionManager::new(url, token);
    let pool = Pool::builder().max_size(10).build(manager).unwrap();
    let client = Client::new(pool);
    let db_name = "my-db";
    let request = CreateDatabaseRequest::new(db_name);
    client.create_database(request).unwrap();
}

定论

本教程介绍了如安在Rust项目中运用InfluxDB 2.x,包括根底用法和进阶用法。咱们供给了示例代码,协助读者更好地了解和运用InfluxDB 2.x。最终,咱们供给了一些最佳实践,协助读者更好地运用InfluxDB 2.x。