C#中调用iotDB

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;
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#调用Python程序可以通过以下步骤实现: 1. 首先,将Python程序转换为动态链接库(dll)。这可以通过使用Cython或者使用Python的ctypes库来实现。在转换为dll之前,确保已经安装了Python运行环境,并且安装了所需的第三方库(如numpy)\[1\]。 2. 在C#,使用DllImport特性来导入Python的dll文件。这样可以在C#调用Python的函数。确保在C#项目引用了Python的dll文件。 3. 在C#调用Python函数时,需要传递参数并接收返回值。可以使用C#的InteropServices命名空间的Marshal类来处理参数和返回值的转换。 下面是一个示例代码,演示了如何在C#调用Python程序: ```csharp using System; using System.Runtime.InteropServices; public class Program { \[DllImport("python_dll_path", CallingConvention = CallingConvention.Cdecl)\] public static extern double func(string a, string b); public static void Main(string\[\] args) { string a = "2"; string b = "3"; double result = func(a, b); Console.WriteLine(result); } } ``` 在上面的代码,`python_dll_path`是Python程序转换为的dll文件的路径。`func`是Python程序的函数名,通过DllImport特性导入。 请注意,这只是一个简单的示例,实际情况可能会更复杂。在实际使用,还需要处理异常、错误检查等情况。 希望这个回答对您有帮助!\[1\] #### 引用[.reference_title] - *1* [c#调用python的四种方法(尝试了四种,只详细讲解本人成功的后两种,其余方法只列出,详细用法请自行谷歌...](https://blog.csdn.net/qq_42063091/article/details/82418630)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [c#调用python的三种方法](https://blog.csdn.net/qq_36744449/article/details/116134794)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值