200 lines
5.5 KiB
C
200 lines
5.5 KiB
C
/*
|
|
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
|
*
|
|
* This program is free software: you can use, redistribute, and/or modify
|
|
* it under the terms of the GNU Affero General Public License, version 3
|
|
* or later ("AGPL"), as published by the Free Software Foundation.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
* FITNESS FOR A PARTICULAR PURPOSE.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include "cJSON.h"
|
|
#include "taos.h"
|
|
#include "tmsg.h"
|
|
#include "types.h"
|
|
|
|
void tmq_commit_cb_print(tmq_t* tmq, int32_t code, void* param) {
|
|
printf("commit %d tmq %p param %p\n", code, tmq, param);
|
|
}
|
|
|
|
tmq_t* build_consumer() {
|
|
tmq_conf_t* conf = tmq_conf_new();
|
|
tmq_conf_set(conf, "group.id", "g1");
|
|
tmq_conf_set(conf, "client.id", "my app 1");
|
|
tmq_conf_set(conf, "td.connect.user", "root");
|
|
tmq_conf_set(conf, "td.connect.pass", "taosdata");
|
|
tmq_conf_set(conf, "msg.with.table.name", "true");
|
|
tmq_conf_set(conf, "enable.auto.commit", "true");
|
|
tmq_conf_set(conf, "auto.offset.reset", "earliest");
|
|
tmq_conf_set(conf, "msg.consume.excluded", "1");
|
|
|
|
tmq_conf_set_auto_commit_cb(conf, tmq_commit_cb_print, NULL);
|
|
tmq_t* tmq = tmq_consumer_new(conf, NULL, 0);
|
|
assert(tmq);
|
|
tmq_conf_destroy(conf);
|
|
return tmq;
|
|
}
|
|
|
|
tmq_list_t* build_topic_list() {
|
|
tmq_list_t* topic_list = tmq_list_new();
|
|
tmq_list_append(topic_list, "db_5466_topic");
|
|
return topic_list;
|
|
}
|
|
|
|
int printRow(char *str, TAOS_ROW row, TAOS_FIELD *fields, int numFields) {
|
|
int len = 0;
|
|
char split = ' ';
|
|
|
|
for (int i = 0; i < numFields; ++i) {
|
|
if (i > 0) {
|
|
str[len++] = split;
|
|
}
|
|
|
|
if (row[i] == NULL) {
|
|
len += sprintf(str + len, "%s", "NULL");
|
|
continue;
|
|
}
|
|
|
|
switch (fields[i].type) {
|
|
case TSDB_DATA_TYPE_TINYINT:
|
|
len += sprintf(str + len, "%d", *((int8_t *)row[i]));
|
|
break;
|
|
|
|
case TSDB_DATA_TYPE_UTINYINT:
|
|
len += sprintf(str + len, "%u", *((uint8_t *)row[i]));
|
|
break;
|
|
|
|
case TSDB_DATA_TYPE_SMALLINT:
|
|
len += sprintf(str + len, "%d", *((int16_t *)row[i]));
|
|
break;
|
|
|
|
case TSDB_DATA_TYPE_USMALLINT:
|
|
len += sprintf(str + len, "%u", *((uint16_t *)row[i]));
|
|
break;
|
|
|
|
case TSDB_DATA_TYPE_INT:
|
|
len += sprintf(str + len, "%d", *((int32_t *)row[i]));
|
|
break;
|
|
|
|
case TSDB_DATA_TYPE_UINT:
|
|
len += sprintf(str + len, "%u", *((uint32_t *)row[i]));
|
|
break;
|
|
|
|
case TSDB_DATA_TYPE_BIGINT:
|
|
len += sprintf(str + len, "%" PRId64, *((int64_t *)row[i]));
|
|
break;
|
|
|
|
case TSDB_DATA_TYPE_UBIGINT:
|
|
len += sprintf(str + len, "%" PRIu64, *((uint64_t *)row[i]));
|
|
break;
|
|
|
|
case TSDB_DATA_TYPE_FLOAT: {
|
|
float fv = 0;
|
|
fv = GET_FLOAT_VAL(row[i]);
|
|
len += sprintf(str + len, "%f", fv);
|
|
} break;
|
|
|
|
case TSDB_DATA_TYPE_DOUBLE: {
|
|
double dv = 0;
|
|
dv = GET_DOUBLE_VAL(row[i]);
|
|
len += sprintf(str + len, "%lf", dv);
|
|
} break;
|
|
|
|
case TSDB_DATA_TYPE_BINARY:
|
|
case TSDB_DATA_TYPE_VARBINARY:
|
|
case TSDB_DATA_TYPE_NCHAR:
|
|
case TSDB_DATA_TYPE_GEOMETRY: {
|
|
int32_t charLen = varDataLen((char *)row[i] - VARSTR_HEADER_SIZE);
|
|
memcpy(str + len, row[i], charLen);
|
|
len += charLen;
|
|
} break;
|
|
|
|
case TSDB_DATA_TYPE_TIMESTAMP:
|
|
len += sprintf(str + len, "%" PRId64, *((int64_t *)row[i]));
|
|
break;
|
|
|
|
case TSDB_DATA_TYPE_BOOL:
|
|
len += sprintf(str + len, "%d", *((int8_t *)row[i]));
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
return len;
|
|
}
|
|
|
|
/**
|
|
* @brief print column name and values of each row
|
|
*
|
|
* @param res
|
|
* @return int
|
|
*/
|
|
static int printResult(TAOS_RES *res) {
|
|
TAOS_ROW row = NULL;
|
|
int32_t cnt = 0;
|
|
while ((row = taos_fetch_row(res))) {
|
|
|
|
int numFields = taos_num_fields(res);
|
|
TAOS_FIELD *fields = taos_fetch_fields(res);
|
|
char header[256] = {0};
|
|
int len = 0;
|
|
for (int i = 0; i < numFields; ++i) {
|
|
len += sprintf(header + len, "%s ", fields[i].name);
|
|
}
|
|
puts(header);
|
|
|
|
char temp[1024] = {0};
|
|
printRow(temp, row, fields, numFields);
|
|
if (cnt == 0){
|
|
ASSERT(strcmp(temp, "1538721485000 10.300000 219 0.310000 NULL NULL California.SanFrancisco 2") == 0);
|
|
}else if(cnt == 1){
|
|
ASSERT(strcmp(temp, "1538807885000 10.300000 219 0.310000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1 California.SanFrancisco 2") == 0);
|
|
}
|
|
cnt++;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void basic_consume_loop(tmq_t* tmq, tmq_list_t* topics) {
|
|
int32_t code;
|
|
|
|
if ((code = tmq_subscribe(tmq, topics))) {
|
|
fprintf(stderr, "%% Failed to start consuming topics: %s\n", tmq_err2str(code));
|
|
printf("subscribe err\n");
|
|
return;
|
|
}
|
|
int32_t cnt = 0;
|
|
while (1) {
|
|
TAOS_RES* tmqmessage = tmq_consumer_poll(tmq, 5000);
|
|
if (tmqmessage) {
|
|
cnt++;
|
|
printResult(tmqmessage);
|
|
taos_free_result(tmqmessage);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
code = tmq_consumer_close(tmq);
|
|
if (code)
|
|
fprintf(stderr, "%% Failed to close consumer: %s\n", tmq_err2str(code));
|
|
else
|
|
fprintf(stderr, "%% Consumer closed\n");
|
|
}
|
|
|
|
int main(int argc, char* argv[]) {
|
|
tmq_t* tmq = build_consumer();
|
|
tmq_list_t* topic_list = build_topic_list();
|
|
basic_consume_loop(tmq, topic_list);
|
|
tmq_list_destroy(topic_list);
|
|
} |