Apache kafka 如何将整数值传递给kafka生产者,并使用kafka中的IntegerSerializer在kafka使用者控制台上重新读取整数值
我试图使用Kafka提供的API IntegerSerializer通过Kafka producer发送整数值,但未正确解析整数值,并且该整数值以随机未知符号的形式显示在Kafka使用者控制台上Apache kafka 如何将整数值传递给kafka生产者,并使用kafka中的IntegerSerializer在kafka使用者控制台上重新读取整数值,apache-kafka,kafka-consumer-api,kafka-producer-api,Apache Kafka,Kafka Consumer Api,Kafka Producer Api,我试图使用Kafka提供的API IntegerSerializer通过Kafka producer发送整数值,但未正确解析整数值,并且该整数值以随机未知符号的形式显示在Kafka使用者控制台上 public static void main(String[] args) throws Exception{ int i; // Check arguments length value if(args.length == 0){ System.out.prin
public static void main(String[] args) throws Exception{
int i;
// Check arguments length value
if(args.length == 0){
System.out.println("Enter topic name");
return;
}
//Assign topicName to string variable
String topicName = args[0].toString();
// create instance for properties to access producer configs
Properties props = new Properties();
//Assign localhost id
props.put("bootstrap.servers", "localhost:9092");
//Set acknowledgements for producer requests.
props.put("acks", "all");
//If the request fails, the producer can automatically retry,
props.put("retries", "0");
//Specify buffer size in config
props.put("batch.size"," 16384");
//Reduce the no of requests less than 0
props.put("linger.ms", "1");
//The buffer.memory controls the total amount of memory available to the producer for buffering.
props.put("buffer.memory", "33554432");
props.put("key.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer",
"org.apache.kafka.common.serialization.IntegerSerializer");
KafkaProducer<String,Integer> producerRcrd = new KafkaProducer<String,Integer>(props);
producerRcrd.send(new ProducerRecord<String,Integer>(topicName, "Key1",100));
System.out.println("Message sent successfully");
producerRcrd.flush();
producerRcrd.close();
}
}
publicstaticvoidmain(字符串[]args)引发异常{
int i;
//检查参数长度值
如果(args.length==0){
System.out.println(“输入主题名称”);
返回;
}
//将topicName分配给字符串变量
字符串topicName=args[0]。toString();
//为访问生产者配置的属性创建实例
Properties props=新属性();
//分配本地主机id
put(“bootstrap.servers”,“localhost:9092”);
//为生产者请求设置确认。
道具放置(“阿克斯”、“全部”);
//如果请求失败,生产者可以自动重试,
道具放置(“重试”,“0”);
//在配置中指定缓冲区大小
道具放置(“批量大小”,“16384”);
//将请求数减少到0以下
道具放置(“玲儿小姐”,“1”);
//buffer.memory控制生产者可用于缓冲的内存总量。
道具放置(“缓冲区内存”,“33554432”);
props.put(“key.serializer”,
“org.apache.kafka.common.serialization.StringSerializer”);
props.put(“value.serializer”,
“org.apache.kafka.common.serialization.IntegerSerializer”);
卡夫卡制作者制作者rRCRD=新卡夫卡制作者(道具);
producerRcrd.send(新产品记录(主题名称,“Key1”,100));
System.out.println(“消息发送成功”);
producerRcrd.flush();
producerRcrd.close();
}
}
然后,卡夫卡消费品控制台上不会显示100。追加
public static void main(String[] args) throws Exception{
int i;
// Check arguments length value
if(args.length == 0){
System.out.println("Enter topic name");
return;
}
//Assign topicName to string variable
String topicName = args[0].toString();
// create instance for properties to access producer configs
Properties props = new Properties();
//Assign localhost id
props.put("bootstrap.servers", "localhost:9092");
//Set acknowledgements for producer requests.
props.put("acks", "all");
//If the request fails, the producer can automatically retry,
props.put("retries", "0");
//Specify buffer size in config
props.put("batch.size"," 16384");
//Reduce the no of requests less than 0
props.put("linger.ms", "1");
//The buffer.memory controls the total amount of memory available to the producer for buffering.
props.put("buffer.memory", "33554432");
props.put("key.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer",
"org.apache.kafka.common.serialization.IntegerSerializer");
KafkaProducer<String,Integer> producerRcrd = new KafkaProducer<String,Integer>(props);
producerRcrd.send(new ProducerRecord<String,Integer>(topicName, "Key1",100));
System.out.println("Message sent successfully");
producerRcrd.flush();
producerRcrd.close();
}
}
--property key.deserializer=org.apache.kafka.common.serialization.StringDeserializer --property value.deserializer=org.apache.kafka.common.serialization.IntegerDeserializer
到
kafka console consumer.sh
,让console消息格式化程序知道如何反序列化消息正文。谢谢。我试图通过使用上述java代码发送整数值。在这个java代码示例中,我使用IntegerSerializer for value发送整数,但它不起作用。不过,我可以轻松地将整数值从kafka的生产者控制台发送到消费者控制台。但当我试图通过java代码来实现这一点时,我面临着一个问题。请建议。我只是在跟踪您的使用情况并获得了成功。如何运行控制台使用者?kafka-console-consumer.bat--zookeeper localhost:2181--topic试试这个:kafka-console-consumer.bat--bootstrap server localhost:9092--topic--从一开始--property key.deserializer=org.apache.kafka.common.serialization.StringDeserializer--propertyvalue.deserializer=org.apache.kafka.common.serialization.IntegerDeserializer