Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使用librdkafka发送json数据?_C_Json_Librdkafka_Jansson - Fatal编程技术网

C 如何使用librdkafka发送json数据?

C 如何使用librdkafka发送json数据?,c,json,librdkafka,jansson,C,Json,Librdkafka,Jansson,我正在尝试使用librdkafka c api发送json负载。我现在想做的是 #include <jansson.h> #include <librdkafka/rdkafka.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> typedef struct my_data { char *id;

我正在尝试使用librdkafka c api发送json负载。我现在想做的是

#include <jansson.h>
#include <librdkafka/rdkafka.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

typedef struct my_data {
    char *id;
    char *value;
    unsigned long timestamp;
} my_data;

char * my_data_to_json(const my_data *ev);
void rk_dr_callback(rd_kafka_t *rk, const rd_kafka_message_t *msg, void *opaque);

int main(int argc, char * argv[])
{
    // configure producer
    rd_kafka_conf_t *conf = rd_kafka_conf_new();
    char errstr[512];

    rd_kafka_conf_set(conf, "bootstrap.servers", "k1.example.com:9093", errstr, sizeof(errstr));    
    
    rd_kafka_conf_set_dr_msg_cb(conf, rk_dr_callback);

    rd_kafka_t *rk = rd_kafka_new(RD_KAFKA_PRODUCER, conf, errstr, sizeof(errstr));    

    if (!rk) {
        fprintf(stderr, "failed to create kafka producer: %s\n", errstr);
        return -1;
    }

    // create json
    my_data ev = {
        .id = "test-id",
        .value = "test-value",
        .timestamp = (unsigned long) time(NULL)
    };

    char *json = my_data_to_json(&ev);
    printf("json dump: %s\n", json);

    // publish data
    rd_kafka_resp_err_t err = rd_kafka_producev(rk, RD_KAFKA_V_TOPIC("topic"),
        RD_KAFKA_V_MSGFLAGS(RD_KAFKA_MSG_F_COPY), RD_KAFKA_V_VALUE(json, strlen(json)),
        RD_KAFKA_V_KEY("key-1", strlen("key-1")), RD_KAFKA_V_OPAQUE(NULL),
        RD_KAFKA_V_END);

    // cleanup
    rd_kafka_flush(rk, 5 * 1000);
    
    if (rd_kafka_outq_len(rk) > 0) {
        fprintf(stderr, "%d message(s) were not delivered\n", rd_kafka_outq_len(rk));
    }

    rd_kafka_destroy(rk);
    free(json);

    return 0;
}

void rk_dr_callback(rd_kafka_t *rk, const rd_kafka_message_t *msg, void *opaque)
{
    if (msg->err) {
        printf("failed to send message: %s\n", rd_kafka_err2str(msg->err));
    }
    else {
        printf("delivered %zd bytes to partition %d\n", msg->len, msg->partition);
    }
}

char * my_data_to_json(const my_data *ev)
{
    /* build the JSON object {"id": "id", "value": "value", "timestamp": 12345678} */
    json_t *json = json_pack("{sssssi}", "id", ev->id, "value", ev->value, "timestamp", ev->timestamp);

    if (!json) {
        fprintf(stderr, "failed to construct json from data\n");
    }

    char *str = json_dumps(json, JSON_COMPACT);

    if (!str) {
        fprintf(stderr, "failed to encode json object\n");
    }

    return str;
}

我不太清楚我的错误是在构造json对象、将其编码为字符串还是在使用librdkafka发布json字符串的方式上。

“我不太清楚我的错误是在构造json对象、将其编码为字符串还是在使用librdkafka发布json字符串的方式上”-然后,您应该将这三种代码分别隔离开来,看看哪一种出错了。如果您在创建或编码JSON时遇到错误,您只是在记录错误。我认为如果这些步骤中的任何一个失败,您应该返回一个
NULL
字符串,并检查调用函数
main()
是否
my_data\u to_json
返回
NULL
。此外,您是否能够看到在发布端(C端)记录的任何错误消息?@kiner_shah我可以看到发送到代理的字节数不为零,并且发布服务器端没有错误。我想知道json的编码是否有问题,因为它需要utf-8,而我没有做任何事情来确保它是utf-8。我现在正在仔细阅读。
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: . Path '', line 0, position 0.\n at Newtonsoft.Json.JsonTextReader.ParseValue()\n at ...