iotDB介绍:
IoTDB (Internet of Things Database) 是由清华大学主导的 Apache 孵化项目,是一款聚焦工业物联网、高性能轻量级的时序数据管理系统,也是一款开源时序数据库,为用户提供数据收集、存储和分析等服务。作为一款时序数据库,IoTDB的相关竞品有 KairosDB,InfluxDB,TimescaleDB等。
IoTDB 提供端云一体化的解决方案,在云端,提供高性能的数据读写以及丰富的查询能力,针对物联网场景定制高效的目录组织结构,并与 Apache Hadoop、Spark、Flink 等大数据系统无缝打通;在边缘端,提供轻量化的 TsFile 管理能力,端上的数据写到本地 TsFile,并提供一定的基础查询能力,同时支持将 TsFile 数据同步到云端。
安装依赖Apache.IoTDB:
代码:
using Apache.IoTDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleIotdb
{
internal class Program
{
static async Task Main(string[] args)
{
// 参数定义
string host = "localhost";
int port = 6667;
int pool_size = 2;
// 初始化session
var session_pool = new SessionPool(host, port, pool_size);
// 开启session
await session_pool.Open(false);
// 创建时间序列
await session_pool.CreateTimeSeries("root.test_group.test_device.ts1", TSDataType.TEXT, TSEncoding.PLAIN, Compressor.UNCOMPRESSED);
await session_pool.CreateTimeSeries("root.test_group.test_device.ts2", TSDataType.BOOLEAN, TSEncoding.PLAIN, Compressor.UNCOMPRESSED);
await session_pool.CreateTimeSeries("root.test_group.test_device.ts3", TSDataType.INT32, TSEncoding.PLAIN, Compressor.UNCOMPRESSED);
// 插入record
var measures = new List<string> { "ts1", "ts2", "ts3" };
var values = new List<object> { "test_text", true, (int)123 };
var timestamp = 1666600000109;
var rowRecord = new Apache.IoTDB.DataStructure.RowRecord(timestamp, values, measures);
await session_pool.InsertRecordAsync("root.test_group.test_device", rowRecord);
插入Tablet
var timestamp_lst = new List<long> { timestamp + 1 };
var value_lst = new List<object> { new Data("iotdb", true, (int)12) };
var tablet = new Apache.IoTDB.DataStructure.Tablet("root.test_group.test_device", measures, new List<List<object>> { value_lst }, timestamp_lst);
await session_pool.InsertTabletAsync(tablet);
//查询
var res = await session_pool.ExecuteQueryStatementAsync(
"select * from root.test_group.**");
res.ShowTableNames();
while (res.HasNext()) Console.WriteLine(res.Next());
//var res = await session_pool.ExecuteQueryStatementAsync(
// "select * from " + string.Format("{0}.{1}", test_group_name, test_devices[1]) + " where time<15");
//res.ShowTableNames();
//while (res.HasNext()) Console.WriteLine(res.Next());
// var res = await session_pool.ExecuteQueryStatementAsync("SHOW TIMESERIES root.f1000.IOTDB_2Ns20");
res.ShowTableNames();
// List<Apache.IoTDB.DataStructure.RowRecord> list = new List<Apache.IoTDB.DataStructure.RowRecord>();
// while (res.HasNext())
// {
// Apache.IoTDB.DataStructure.RowRecord rowRecord = res.Next();
// list.Add(rowRecord);
// string test = rowRecord.Values[0].ToString();
// //Console.WriteLine(test);
// }
// Console.WriteLine(rowRecord.Timestamps);
// 关闭Session
await session_pool.Close();
Console.ReadLine();
}
}
internal class Data
{
private string v1;
private bool v2;
private int v3;
public Data(string v1, bool v2, int v3)
{
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
}
}
}