#iot #iotdb #operation #apache-iotdb

iotdb-client

Apache IoTDB的Rust客户端库

2个版本

0.1.1 2024年5月20日
0.1.0 2024年5月20日

#445数据库接口

Apache-2.0

1MB
21K SLoC

Apache IoTDB

Main Mac and Linux Main Win coveralls GitHub release License IoTDB Website

Apache IoTDB(物联网数据库)是一个针对物联网的数据库,具有高性能的数据管理和分析能力,可部署在边缘和云端。由于其轻量级架构、高性能和丰富的功能集,以及与Apache Hadoop、Spark和Flink的深度集成,Apache IoTDB可以满足物联网工业领域在大量数据存储、高速数据摄入和复杂数据分析方面的需求。

Apache IoTDB Rust客户端

关于版本支持

支持IoTDB的最新版本1.3.1,也兼容1.0以下版本的操作。

未来我们将继续更新并与最新官方IoTDB库保持同步。

概述

这是Apache IoTDB的Rust客户端。

Apache IoTDB网站:https://iotdb.apache.org Apache IoTDB Github:https://github.com/apache/iotdb

先决条件

apache-iotdb 0.12.0及更高版本。
rust 1.56.0及更高版本。

如何使用客户端(快速入门)

用法

将以下内容放入您的 Cargo.toml

[dependencies]
iotdb-client="0.1.0"

示例

将以下内容放入示例的 Cargo.toml

[dependencies]
iotdb-client="0.1.0"
chrono="0.4.19"
prettytable-rs="0.8.0"
structopt = "0.3.25"
use std::{collections::BTreeMap, vec};

use chrono::Local;
use iotdb_client::client::remote::{Config, RpcSession};
use iotdb_client::client::{MeasurementSchema, Result, RowRecord, Session, Tablet, Value};
use iotdb_client::protocal::{TSCompressionType, TSDataType, TSEncoding};
use prettytable::{cell, Row, Table};
use structopt::StructOpt;

fn main() {
    demo().expect("failed to run example.");
}

fn demo() -> Result<()> {

    let config = Config::builder()
        .host("127.0.0.1")
        .port(6667)
        .username("root")
        .password("root")
        .build();

    let mut session = RpcSession::new(config)?;
    session.open()?;

    //time_zone
    let tz = session.get_time_zone()?;
    if tz != "Asia/Shanghai" {
        session.set_time_zone("Asia/Shanghai")?;
    }

    //set_storage_group
    session.set_storage_group("root.ln1")?;
    //session.delete_storage_group("root.ln1")?;
    //session.delete_storage_groups(vec!["root.ln1", "root.ln2"])?;

    //execute_statement
    {
        println!("start executing statement...");
        let dataset = session.execute_statement("show timeseries", None)?;
        let mut table = Table::new();
        table.set_titles(Row::new(
            dataset
                .get_column_names()
                .iter()
                .map(|c| cell!(c))
                .collect(),
        ));
        dataset.for_each(|r: RowRecord| {
            table.add_row(Row::new(
                r.values.iter().map(|v: &Value| cell!(v)).collect(),
            ));
        });
        table.printstd();
        println!("end executing statement...");
    }


    //execute_batch_statement
    {
        session.execute_batch_statement(vec![
            "insert into root.ln1.dev0(time,s5) values(1,true)",
            "insert into root.ln1.dev1(time,s5) values(2,true)",
            "insert into root.ln1.dev2(time,s5) values(3,true)",
        ])?;
        //session.delete_timeseries(vec!["root.ln1.dev0.s5"])?;
    }

    //execute_raw_data_query
    {
        println!("start execute_raw_data_query statement...");
        let dataset = session.execute_raw_data_query(
            vec![
                "root.ln1.dev0.s5",
                "root.ln1.dev1.s5",
                "root.ln1.dev2.s5",
            ],
            0,
            i64::MAX,
            3000
        )?;
        let mut table = Table::new();
        table.set_titles(Row::new(
            dataset
                .get_column_names()
                .iter()
                .map(|c| cell!(c))
                .collect(),
        ));
        dataset.for_each(|r: RowRecord| {
            table.add_row(Row::new(
                r.values.iter().map(|v: &Value| cell!(v)).collect(),
            ));
        });
        table.printstd();
        println!("end execute_raw_data_query statement...");
    }

    //execute_query_statement
    {
        println!("start execute_query_statement...");
        let dataset = session.execute_query_statement(" select * from root.ln1.*;", None)?;
        // Get columns, column types and values from the dataset
        // For example:
        let width = 18;
        let column_count = dataset.get_column_names().len();
        let print_line_sep =
            || println!("{:=<width$}", '=', width = (width + 1) * column_count + 1);

        print_line_sep();
        dataset
            .get_column_names()
            .iter()
            .for_each(|c| print!("|{:>width$}", c.split('.').last().unwrap(), width = width));
        println!("|");
        print_line_sep();
        dataset.get_data_types().iter().for_each(|t| {
            let type_name = format!("{:?}", t);
            print!("|{:>width$}", type_name, width = width)
        });
        println!("|");
        print_line_sep();
        dataset.for_each(|r| {
            r.values.iter().for_each(|v| match v {
                Value::Bool(v) => print!("|{:>width$}", v, width = width),
                Value::Int32(v) => print!("|{:>width$}", v, width = width),
                Value::Int64(v) => print!("|{:>width$}", v, width = width),
                Value::Float(v) => print!("|{:>width$}", v, width = width),
                Value::Double(v) => print!("|{:>width$}", v, width = width),
                Value::Text(v) => print!("|{:>width$}", v, width = width),
                Value::Null => print!("|{:>width$}", "null", width = width),
            });
            println!("|");
        });
        print_line_sep();
        println!("end execute_query_statement...");
    }

    //execute_update_statement
    {
        if let Some(dataset) =
            session.execute_update_statement("delete from root.ln1.*")?
        {
            dataset.for_each(|r| println!("timestamp: {} {:?}", r.timestamp, r.values));
        }
    }

    session.close()?;
    Ok(())
}

依赖项

~1–1.5MB
~32K SLoC