TDengine.cpp 9.3 KB

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