#database-interface #real-time #variables #connect-database #advanced #interact #setting

arikedb

欢迎使用ArikeDB Rust库!这个库提供了与Arike数据库(一种高级实时数据库解决方案)交互的接口。本文档将指导您如何有效设置和使用此库。

4个版本

0.0.4 2024年8月4日
0.0.3 2024年7月19日
0.0.2 2024年7月19日
0.0.1 2024年7月14日

#597数据库接口

Download history 348/week @ 2024-07-14 33/week @ 2024-07-21 93/week @ 2024-07-28 103/week @ 2024-08-04 7/week @ 2024-08-11

304 每月下载量

MIT 许可证

23KB
420 代码行

ArikeDB Rust库

欢迎使用ArikeDB Rust库!这个库提供了与Arike数据库(一种高级实时数据库解决方案)交互的接口。本文档将指导您如何有效设置和使用此库。

入门指南

安装

要使用ArikeDB Rust库,将其添加到您的Cargo.toml

[dependencies]
arikedb = "*"

连接到ArikeDB

要连接到ArikeDB服务器实例,将ArikedbClient引入作用域,并通过调用connect函数来实例化它。

基本连接

use arikedb::ArikedbClient;
use arikedb::{Variable, VariableType, Epoch, VarEvent, Event};

#[tokio::main]
async fn main() {
    let mut client = ArikedbClient::connect(
        "127.0.0.1", // host
        6923,        // port
        false,       // use_ssl
        None,        // ca_cert
        None,        // client_cert
        None         // client_key
    ).await.unwrap();
}

带有身份验证的连接

如果服务器需要身份验证,您需要在连接后进行身份验证。

use arikedb::ArikedbClient;
use arikedb::{Variable, VariableType, Epoch, VarEvent, Event};

#[tokio::main]
async fn main() {
    let mut client = ArikedbClient::connect(
        "127.0.0.1", // host
        6923,        // port
        false        // use_ssl
        None,        // ca_cert
        None,        // client_cert
        None         // client_key
    ).await.unwrap();

    client.authenticate("username", "password").await.unwrap();
}

创建集合

ArikeDB将数据组织到集合中。每个集合都有一个名称和一组变量。要在单个调用中创建多个集合

client.create_collections(&vec!["collection1", "collection2", "collection3"]).await.unwrap();

删除集合

client.delete_collections(&vec!["collection2", "collection3"]).await.unwrap();

列出集合

let collections = client.list_collections().await.unwrap();

for collection in collections {
    println!("{:?}", collection);
}

输出

Collection { name: "collection1" }
Collection { name: "collection2" }

创建变量

let variables = vec![
    Variable { name: String::from("var1"), vtype: VariableType::I32, buffer_size: 10 },
    Variable { name: String::from("var2"), vtype: VariableType::I32, buffer_size: 5 },
    Variable { name: String::from("var3"), vtype: VariableType::I32, buffer_size: 4 },
    Variable { name: String::from("var4"), vtype: VariableType::I32, buffer_size: 4 },
];

client.create_variables("collection1", variables).await.unwrap();

删除变量

client.delete_variables("collection1", &vec!["var3", "var4"]).await.unwrap();

列出变量

let variables = client.list_variables("collection1").await.unwrap();

for variable in variables {
    println!("{:?}", variable);
}

输出

Variable { name: "var1", vtype: I32, buffer_size: 10 }
Variable { name: "var2", vtype: I32, buffer_size: 5 }

设置变量值

client.set_variables(
    "collection1",
    vec!["var1", "var2"],
    SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_micros(),
    vec!["-235", "48"],
    Epoch::Microsecond
).await.unwrap();

获取变量值

let derived_order = 0;
let data = client.get_variables("collection1", vec!["var1", "var2"], derived_order, Epoch::Nanosecond).await.unwrap();

for point in data {
    println!("{:?}", point);
}

输出

DataPoint { name: "var1", vtype: I32, timestamp: "1720927547279880000", epoch: Nanosecond, value: "-235" }
DataPoint { name: "var2", vtype: I32, timestamp: "1720927547279880000", epoch: Nanosecond, value: "48" }

订阅变量事件

当变量设置并满足事件条件时,会生成事件。

tokio::spawn(
    client.subscribe_variables(
        "collection1",
        vec!["var1", "var2"],
        vec![
            VarEvent { event: Event::OnRise, ..Default::default() },
            VarEvent { event: Event::OnValueEqVal, value: String::from("56"), ..Default::default() },
        ],
        |point| {
            println!("{:?}", point);
        }
    ).await.unwrap()
);

for i in 0..10 {
    client.set_variables(
        "collection1",
        vec!["var1", "var2"],
        SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_micros(),
        vec![(50 + i).to_string().as_str(), (60 - i).to_string().as_str()],
        Epoch::Microsecond
    ).await.unwrap();
    tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}

输出

DataPoint { name: "var1", vtype: I32, timestamp: "1720927881619292000", epoch: Nanosecond, value: "50" }
DataPoint { name: "var2", vtype: I32, timestamp: "1720927881619292000", epoch: Nanosecond, value: "60" }
DataPoint { name: "var1", vtype: I32, timestamp: "1720927882629171000", epoch: Nanosecond, value: "51" }
DataPoint { name: "var1", vtype: I32, timestamp: "1720927883638571000", epoch: Nanosecond, value: "52" }
DataPoint { name: "var1", vtype: I32, timestamp: "1720927884652619000", epoch: Nanosecond, value: "53" }
DataPoint { name: "var1", vtype: I32, timestamp: "1720927885664589000", epoch: Nanosecond, value: "54" }
DataPoint { name: "var2", vtype: I32, timestamp: "1720927885664589000", epoch: Nanosecond, value: "56" }
DataPoint { name: "var1", vtype: I32, timestamp: "1720927886673866000", epoch: Nanosecond, value: "55" }
DataPoint { name: "var1", vtype: I32, timestamp: "1720927887693254000", epoch: Nanosecond, value: "56" }
DataPoint { name: "var1", vtype: I32, timestamp: "1720927888703026000", epoch: Nanosecond, value: "57" }
DataPoint { name: "var1", vtype: I32, timestamp: "1720927889717038000", epoch: Nanosecond, value: "58" }
DataPoint { name: "var1", vtype: I32, timestamp: "1720927890728150000", epoch: Nanosecond, value: "59" }

依赖项

~12–23MB
~418K SLoC