You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.7KB

  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using TDengineDriver;
  8. using TDengineTMQ;
  9. namespace HealthMonitor.Service.Sub
  10. {
  11. public class TDengineDataSubcribe
  12. {
  13. private readonly ILogger<TDengineDataSubcribe> _logger;
  14. private IConsumer _consumer = default!;
  15. private IntPtr _conn = default!;
  16. public TDengineDataSubcribe(
  17. ILogger<TDengineDataSubcribe> logger
  18. )
  19. {
  20. _logger = logger;
  21. _conn = GetConnection();
  22. }
  23. public void BeginListen(CancellationToken stoppingToken)
  24. {
  25. //var cfg = new ConsumerConfig
  26. //{
  27. // GourpId = "group_1",
  28. // TDConnectUser = "root",
  29. // TDConnectPasswd = "taosdata",
  30. // MsgWithTableName = "true",
  31. // TDConnectIp = "47.116.142.20",
  32. //};
  33. //var conn = GetConnection();
  34. //var consumer = CreateConsumer(cfg, conn);
  35. //ProcessMsg(consumer);
  36. }
  37. public void CreateConnection()
  38. {
  39. var cfg = new ConsumerConfig
  40. {
  41. GourpId = "group_1",
  42. TDConnectUser = "root",
  43. TDConnectPasswd = "taosdata",
  44. MsgWithTableName = "true",
  45. TDConnectIp = "47.116.142.20",
  46. };
  47. var conn = GetConnection();
  48. }
  49. // 创建消费者
  50. public void CreateConsumer()
  51. {
  52. var cfg = new ConsumerConfig
  53. {
  54. GourpId = "group_1",
  55. TDConnectUser = "root",
  56. TDConnectPasswd = "taosdata",
  57. MsgWithTableName = "true",
  58. TDConnectIp = "47.116.142.20",
  59. };
  60. //IntPtr conn = GetConnection();
  61. string topic = "topic_name";
  62. //create topic
  63. IntPtr res = TDengine.Query(_conn, $"create topic if not exists {topic} as select * from ctb1");
  64. if (TDengine.ErrorNo(res) != 0)
  65. {
  66. throw new Exception($"create topic failed, reason:{TDengine.Error(res)}");
  67. }
  68. // create consumer
  69. var consumer = new ConsumerBuilder(cfg)
  70. .Build();
  71. // subscribe
  72. consumer.Subscribe(topic);
  73. _consumer = consumer;
  74. }
  75. //public IConsumer CreateConsumer()
  76. //{
  77. // var cfg = new ConsumerConfig
  78. // {
  79. // GourpId = "group_1",
  80. // TDConnectUser = "root",
  81. // TDConnectPasswd = "taosdata",
  82. // MsgWithTableName = "true",
  83. // TDConnectIp = "47.116.142.20",
  84. // };
  85. // var conn = GetConnection();
  86. // //IntPtr conn = GetConnection();
  87. // string topic = "topic_name";
  88. // //create topic
  89. // IntPtr res = TDengine.Query(conn, $"create topic if not exists {topic} as select * from ctb1");
  90. // if (TDengine.ErrorNo(res) != 0)
  91. // {
  92. // throw new Exception($"create topic failed, reason:{TDengine.Error(res)}");
  93. // }
  94. // // create consumer
  95. // var consumer = new ConsumerBuilder(cfg)
  96. // .Build();
  97. // // subscribe
  98. // consumer.Subscribe(topic);
  99. // return consumer;
  100. //}
  101. public void ProcessMsg()
  102. {
  103. var consumerRes = _consumer.Consume(300);
  104. // process ConsumeResult
  105. foreach (KeyValuePair<TopicPartition, TaosResult> kv in consumerRes.Message)
  106. {
  107. Console.WriteLine("topic partitions:\n{0}", kv.Key.ToString());
  108. kv.Value.Metas.ForEach(meta =>
  109. {
  110. Console.Write("{0} {1}({2}) \t|", meta.name, meta.TypeName(), meta.size);
  111. });
  112. Console.WriteLine("");
  113. kv.Value.Datas.ForEach(data =>
  114. {
  115. Console.WriteLine(data.ToString());
  116. });
  117. }
  118. _consumer.Commit(consumerRes);
  119. Console.WriteLine("\n================ {0} done ");
  120. }
  121. public IntPtr GetConnection()
  122. {
  123. string host = "47.116.142.20";
  124. short port = 6030;
  125. string username = "root";
  126. string password = "taosdata";
  127. string dbname = "tmqdb";
  128. var conn = TDengine.Connect(host, username, password, dbname, port);
  129. if (conn == IntPtr.Zero)
  130. {
  131. throw new Exception("Connect to TDengine failed");
  132. }
  133. else
  134. {
  135. Console.WriteLine("Connect to TDengine success");
  136. }
  137. return conn;
  138. }
  139. // 关闭消费者
  140. //public void CloseConsumer(IConsumer consumer, IntPtr conn)
  141. //{
  142. // List<string> topics = consumer.Subscription();
  143. // topics.ForEach(t => Console.WriteLine("topic name:{0}", t));
  144. // // unsubscribe
  145. // consumer.Unsubscribe();
  146. // // close consumer after use.Otherwise will lead memory leak.
  147. // consumer.Close();
  148. // TDengine.Close(conn);
  149. //}
  150. public void CloseConsumer()
  151. {
  152. List<string> topics = _consumer.Subscription();
  153. topics.ForEach(t => Console.WriteLine("topic name:{0}", t));
  154. // unsubscribe
  155. _consumer.Unsubscribe();
  156. // close consumer after use.Otherwise will lead memory leak.
  157. _consumer.Close();
  158. TDengine.Close(_conn);
  159. }
  160. }
  161. }