|
- using Microsoft.Extensions.Logging;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using TDengineDriver;
- using TDengineTMQ;
-
- namespace HealthMonitor.Service.Sub
- {
- public class TDengineDataSubcribe
- {
- private readonly ILogger<TDengineDataSubcribe> _logger;
- private IConsumer _consumer = default!;
- private IntPtr _conn = default!;
-
- public TDengineDataSubcribe(
- ILogger<TDengineDataSubcribe> logger
- )
- {
- _logger = logger;
- _conn = GetConnection();
- }
- public void BeginListen(CancellationToken stoppingToken)
- {
- //var cfg = new ConsumerConfig
- //{
- // GourpId = "group_1",
- // TDConnectUser = "root",
- // TDConnectPasswd = "taosdata",
- // MsgWithTableName = "true",
- // TDConnectIp = "47.116.142.20",
- //};
- //var conn = GetConnection();
- //var consumer = CreateConsumer(cfg, conn);
-
- //ProcessMsg(consumer);
- }
-
- public void CreateConnection()
- {
- var cfg = new ConsumerConfig
- {
- GourpId = "group_1",
- TDConnectUser = "root",
- TDConnectPasswd = "taosdata",
- MsgWithTableName = "true",
- TDConnectIp = "47.116.142.20",
- };
- var conn = GetConnection();
- }
-
-
- // 创建消费者
- public void CreateConsumer()
- {
- var cfg = new ConsumerConfig
- {
- GourpId = "group_1",
- TDConnectUser = "root",
- TDConnectPasswd = "taosdata",
- MsgWithTableName = "true",
- TDConnectIp = "47.116.142.20",
- };
- //IntPtr conn = GetConnection();
- string topic = "topic_name";
- //create topic
- IntPtr res = TDengine.Query(_conn, $"create topic if not exists {topic} as select * from ctb1");
-
- if (TDengine.ErrorNo(res) != 0)
- {
- throw new Exception($"create topic failed, reason:{TDengine.Error(res)}");
- }
-
- // create consumer
- var consumer = new ConsumerBuilder(cfg)
- .Build();
-
- // subscribe
- consumer.Subscribe(topic);
-
- _consumer = consumer;
- }
-
- //public IConsumer CreateConsumer()
- //{
- // var cfg = new ConsumerConfig
- // {
- // GourpId = "group_1",
- // TDConnectUser = "root",
- // TDConnectPasswd = "taosdata",
- // MsgWithTableName = "true",
- // TDConnectIp = "47.116.142.20",
- // };
- // var conn = GetConnection();
- // //IntPtr conn = GetConnection();
- // string topic = "topic_name";
- // //create topic
- // IntPtr res = TDengine.Query(conn, $"create topic if not exists {topic} as select * from ctb1");
-
- // if (TDengine.ErrorNo(res) != 0)
- // {
- // throw new Exception($"create topic failed, reason:{TDengine.Error(res)}");
- // }
-
- // // create consumer
- // var consumer = new ConsumerBuilder(cfg)
- // .Build();
-
- // // subscribe
- // consumer.Subscribe(topic);
-
- // return consumer;
- //}
-
- public void ProcessMsg()
- {
- var consumerRes = _consumer.Consume(300);
- // process ConsumeResult
- foreach (KeyValuePair<TopicPartition, TaosResult> kv in consumerRes.Message)
- {
- Console.WriteLine("topic partitions:\n{0}", kv.Key.ToString());
-
- kv.Value.Metas.ForEach(meta =>
- {
- Console.Write("{0} {1}({2}) \t|", meta.name, meta.TypeName(), meta.size);
- });
- Console.WriteLine("");
- kv.Value.Datas.ForEach(data =>
- {
- Console.WriteLine(data.ToString());
- });
- }
-
- _consumer.Commit(consumerRes);
- Console.WriteLine("\n================ {0} done ");
- }
-
- public IntPtr GetConnection()
- {
- string host = "47.116.142.20";
- short port = 6030;
- string username = "root";
- string password = "taosdata";
- string dbname = "tmqdb";
- var conn = TDengine.Connect(host, username, password, dbname, port);
- if (conn == IntPtr.Zero)
- {
- throw new Exception("Connect to TDengine failed");
- }
- else
- {
- Console.WriteLine("Connect to TDengine success");
- }
- return conn;
- }
- // 关闭消费者
- //public void CloseConsumer(IConsumer consumer, IntPtr conn)
- //{
- // List<string> topics = consumer.Subscription();
- // topics.ForEach(t => Console.WriteLine("topic name:{0}", t));
-
- // // unsubscribe
- // consumer.Unsubscribe();
-
- // // close consumer after use.Otherwise will lead memory leak.
- // consumer.Close();
- // TDengine.Close(conn);
- //}
-
- public void CloseConsumer()
- {
- List<string> topics = _consumer.Subscription();
- topics.ForEach(t => Console.WriteLine("topic name:{0}", t));
-
- // unsubscribe
- _consumer.Unsubscribe();
-
- // close consumer after use.Otherwise will lead memory leak.
- _consumer.Close();
- TDengine.Close(_conn);
- }
- }
- }
|