TDengineClient.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "TDengineClient.h"
  2. #include "taos.h"
  3. #include <QtCore/QDebug>
  4. #include <QtConcurrent/QtConcurrent>
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include "taos.h"
  11. int32_t TDengineClient::msgProcess(TAOS_RES* msg)
  12. {
  13. char buf[1024];
  14. int32_t rows = 0;
  15. const char* topicName = tmq_get_topic_name(msg);
  16. const char* dbName = tmq_get_db_name(msg);
  17. int32_t vgroupId = tmq_get_vgroup_id(msg);
  18. //qDebug() << __FILE__ << __LINE__ << "db: " << dbName;
  19. //qDebug() << __FILE__ << __LINE__ << "topic: " << topicName;
  20. //qDebug() << __FILE__ << __LINE__ << "vgroup id:" << vgroupId;
  21. while (1)
  22. {
  23. TAOS_ROW row = taos_fetch_row(msg);
  24. if (row == NULL)
  25. break;
  26. TAOS_FIELD* fields = taos_fetch_fields(msg);
  27. int32_t numOfFields = taos_field_count(msg);
  28. rows++;
  29. taos_print_row(buf, row, fields, numOfFields);
  30. QJsonObject jsonObject;
  31. for(int k = 0;k < numOfFields;k++)
  32. {
  33. QString fieldName = fields[k].name;
  34. if(fields[k].type == TSDB_DATA_TYPE_TIMESTAMP)
  35. {
  36. uint64_t value = *((int64_t *)row[k]);
  37. int intvalue = (int)value;
  38. jsonObject.insert(fieldName,intvalue);
  39. }
  40. else if(fields[k].type == TSDB_DATA_TYPE_VARCHAR)
  41. {
  42. QString value((char *)row[k]);
  43. jsonObject.insert(fieldName,value);
  44. }
  45. else if(fields[k].type == TSDB_DATA_TYPE_DOUBLE)
  46. {
  47. double value = *(double *)(row[k]);
  48. jsonObject.insert(fieldName,value);
  49. }
  50. else if(fields[k].type == TSDB_DATA_TYPE_FLOAT)
  51. {
  52. float value = *(float *)(row[k]);
  53. jsonObject.insert(fieldName,value);
  54. }
  55. else if(fields[k].type == TSDB_DATA_TYPE_INT)
  56. {
  57. int value = *(int *)(row[k]);
  58. jsonObject.insert(fieldName,value);
  59. }
  60. else if(fields[k].type == TSDB_DATA_TYPE_BIGINT)
  61. {
  62. int64_t value = *(int64_t *)(row[k]);
  63. int intvalue = (int)value;
  64. jsonObject.insert(fieldName,intvalue);
  65. }
  66. }
  67. std::string content = QJsonDocument(jsonObject).toJson(QJsonDocument::Compact).toStdString();
  68. //if( g_pSubCB != nullptr )
  69. {
  70. std::string topic = topicName;
  71. //std::string content = buf;
  72. callback((char*)topic.c_str(),(char*)content.c_str(), usrData);
  73. }
  74. //qDebug() << __FILE__ << __LINE__ << "row content: " << buf;
  75. }
  76. return rows;
  77. }
  78. //构建消费者
  79. tmq_t* TDengineClient::buildConsumer()
  80. {
  81. tmq_conf_res_t code;
  82. tmq_conf_t* conf = tmq_conf_new();
  83. code = tmq_conf_set(conf, "td.connect.ip", host.toStdString().c_str());
  84. if (TMQ_CONF_OK != code) {
  85. tmq_conf_destroy(conf);
  86. return NULL;
  87. }
  88. //code = tmq_conf_set(conf, "enable.auto.commit", "true");
  89. code = tmq_conf_set(conf, "enable.auto.commit", "false"); //禁用提交回调
  90. if (TMQ_CONF_OK != code) {
  91. tmq_conf_destroy(conf);
  92. return NULL;
  93. }
  94. // code = tmq_conf_set(conf, "auto.commit.interval.ms", "1000");
  95. // if (TMQ_CONF_OK != code) {
  96. // tmq_conf_destroy(conf);
  97. // return NULL;
  98. // }
  99. code = tmq_conf_set(conf, "group.id", "cgrpName");
  100. if (TMQ_CONF_OK != code) {
  101. tmq_conf_destroy(conf);
  102. return NULL;
  103. }
  104. code = tmq_conf_set(conf, "client.id", "user defined name");
  105. if (TMQ_CONF_OK != code) {
  106. tmq_conf_destroy(conf);
  107. return NULL;
  108. }
  109. code = tmq_conf_set(conf, "td.connect.user", user.toStdString().c_str());
  110. if (TMQ_CONF_OK != code) {
  111. tmq_conf_destroy(conf);
  112. return NULL;
  113. }
  114. code = tmq_conf_set(conf, "td.connect.pass", password.toStdString().c_str());
  115. if (TMQ_CONF_OK != code) {
  116. tmq_conf_destroy(conf);
  117. return NULL;
  118. }
  119. //v3.2以后默认为最新的latest
  120. // code = tmq_conf_set(conf, "auto.offset.reset", "latest");
  121. // if (TMQ_CONF_OK != code) {
  122. // tmq_conf_destroy(conf);
  123. // return NULL;
  124. // }
  125. //tmq_conf_set_auto_commit_cb(conf, tmq_commit_cb_print, NULL); //禁用提交回调
  126. tmq_t* tmq = tmq_consumer_new(conf, NULL, 0);
  127. //qDebug()<< __FILE__ << __LINE__ << "tmq" << tmq;
  128. tmq_conf_destroy(conf);
  129. return tmq;
  130. }
  131. //构建主题
  132. tmq_list_t* TDengineClient::buildTopicList()
  133. {
  134. tmq_list_t* tmqList = tmq_list_new();
  135. std::set<std::string>::iterator itr;
  136. for( itr = topicList.begin(); itr != topicList.end(); ++itr )
  137. {
  138. std::string szTopic = *itr;
  139. int32_t code = tmq_list_append(tmqList, szTopic.c_str());
  140. if (code) {
  141. return NULL;
  142. }
  143. }
  144. return tmqList;
  145. }
  146. //轮询主题
  147. void TDengineClient::topicLoop()
  148. {
  149. int32_t code;
  150. tmq_t* tmq = buildConsumer();
  151. if (NULL == tmq) {
  152. qDebug()<< __FILE__ << __LINE__ << "err" << stderr;
  153. return;
  154. }
  155. tmq_list_t* topic_list = buildTopicList();
  156. code = tmq_subscribe(tmq, topic_list);
  157. if ( code ) {
  158. qDebug() << __FILE__ << __LINE__ << "Failed to tmq_subscribe(): " << tmq_err2str(code);
  159. }
  160. tmq_list_destroy(topic_list);
  161. int32_t totalRows = 0;
  162. int32_t msgCnt = 0;
  163. int32_t timeout = 100; //多个主题共用1个线程轮询,超时时间太长会影响其它主题实时性
  164. while (true) {
  165. TAOS_RES* tmqmsg = tmq_consumer_poll(tmq, timeout);
  166. if (tmqmsg) {
  167. msgCnt++;
  168. totalRows += msgProcess(tmqmsg);
  169. taos_free_result(tmqmsg);
  170. } else {
  171. //超时
  172. //break;
  173. }
  174. }
  175. code = tmq_consumer_close(tmq);
  176. if (code) {
  177. fprintf(stderr, "%% Failed to close consumer: %s\n", tmq_err2str(code));
  178. } else {
  179. fprintf(stderr, "%% Consumer closed\n");
  180. }
  181. qDebug()<< __FILE__ << __LINE__ << "stderr" << stderr;
  182. }
  183. TDengineClient::TDengineClient(QObject *parent)
  184. : QObject(parent)
  185. {
  186. pConn = nullptr;
  187. //g_pSubCB = nullptr;
  188. topicList.clear();
  189. }
  190. TDengineClient::~TDengineClient()
  191. {
  192. taos_close(pConn);
  193. if(future.isRunning()){
  194. future.waitForFinished();
  195. }
  196. }
  197. void TDengineClient::exec(QString sql)
  198. {
  199. if (pConn)
  200. {
  201. TAOS_RES* pRes = taos_query(pConn, sql.toStdString().c_str());
  202. if (taos_errno(pRes) != 0) {
  203. qDebug() << __FILE__ << __LINE__ << "failed to insert into tab, reason:" << taos_errstr(pRes);
  204. qDebug()<< __FILE__ << __LINE__ << "err sql" << sql;
  205. }
  206. else
  207. {
  208. //qDebug()<< "exec ok";
  209. }
  210. taos_free_result(pRes);
  211. }
  212. }
  213. void TDengineClient::subscribe(QString ch, std::function<void(const char* topic, const char* data, void*usr)>& fn , void* usrdata)
  214. {
  215. callback = fn;
  216. usrData = usrdata;
  217. topicList.insert(ch.toLocal8Bit().toStdString());
  218. }
  219. void TDengineClient::psubscribe(QString ch, std::function<void(const char* topic, const char* data, void*usr)>& fn, void*usrdata)
  220. {
  221. callback = fn;
  222. usrData = usrdata;
  223. topicList.insert(ch.toLocal8Bit().toStdString());
  224. }
  225. void TDengineClient::Setup(const char* _host, const char* _user, const char* _passwd, uint16_t _port)
  226. {
  227. // host = ts.addr.c_str();
  228. // user = ts.user.c_str();
  229. // dbName = ts.db.c_str();
  230. // password = ts.password.c_str();
  231. // port = ts.port;
  232. host = _host;
  233. user = _user;
  234. password = _passwd;
  235. port = _port;
  236. }
  237. void TDengineClient::start()
  238. {
  239. pConn = taos_connect(host.toStdString().c_str(),
  240. user.toStdString().c_str(),
  241. password.toStdString().c_str(),
  242. dbName.toStdString().c_str(),
  243. port);
  244. if (pConn == NULL) {
  245. qDebug()<< __FILE__ << __LINE__ << "td conn err.";
  246. return;
  247. }
  248. //开启一个线程轮询订阅的主题
  249. future = QtConcurrent::run(this, &TDengineClient::topicLoop);
  250. }