Kafka producing message key as STRING even though the REST program has INT?

橙三吉。 提交于 2020-06-17 14:20:28

问题


I am using following program to produce records in kafka:

import java.io.IOException;
import java.security.SecureRandom;

public class SensorStatusProducer {
  private final static String TOPIC = "SENSOR_STATUS_DETAILS";
  private final static String PRODUCER_URI = "http://xxx.xxx.xxx.xxx:8082/topics/" + TOPIC;
  private final static SecureRandom randomNumber = new SecureRandom();
  private final static SensorDetails sensorDetails = new SensorDetails();

  public static void main(String[] args) {
    int[] sensorid = sensorDetails.getSensorid(); //this will return [1001,1002,1003,1004,1005]
    try {
      HttpRestProxyUtil rest = new HttpRestProxyUtil(); //this is defined in another class
      for (int sid : sensorid) {
        rest.produceRecords(PRODUCER_URI, String.format("{\"records\":[{\"key\": %d," +
          "\"value\":{" +
          "\"sensorid\":%d," +
          "\"status\":%s," +
          "\"lastconnectedtime\":%s}}]}", sid, sid, "\"CONNECTED\"", String.format("\"%s\"", sensorDetails.currentTimestamp()))); //currentTimestamp() function in defined in another class
      }
    } catch (InterruptedException | IOException me) {
      me.printStackTrace();
    }
  }
}

The key has format specifier as %d but the record produced has key of STRING type.

This is evident by following:

When trying to make table:

CREATE TABLE STATUS_IB_TABLE (ROWKEY INT KEY,
  sensorid INTEGER,
  status VARCHAR,
  lastconnectedtime STRING)
WITH (TIMESTAMP='lastconnectedtime', TIMESTAMP_FORMAT='yyyy-MM-dd HH:mm:ss', KAFKA_TOPIC='SENSOR_STATUS_DETAILS', VALUE_FORMAT='JSON', KEY='sensorid');

The KEY is serialized as STRING as pointed out by @Andrew Coates

I don't know how's that possible.

can someone please clarify this for me, what am I doing wrong?

PS:

=> this is a follow up question for my earlier question ksqlDB not taking rowkey properly

=> Confluent Platform version: 5.5

=> This is the main class of the program.


回答1:


The REST Proxy supports various content types, but not including the primitive type to write a serialized 32-bit integer.

Your code is thus producing data to the topic with a string key. For an example of how to produce an INT see the example here which uses kafkacat.

Since you're using Java, you could use the native Java Producer API to control exactly how the data is produced to Kafka (which is also more performant and flexible than the REST API).



来源:https://stackoverflow.com/questions/62152765/kafka-producing-message-key-as-string-even-though-the-rest-program-has-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!