Kafka AVRO消费者:MySQL十进制到Java十进制
我试图使用MySQL表中的记录,该表包含3列Kafka AVRO消费者:MySQL十进制到Java十进制,java,mysql,apache-kafka,apache-kafka-connect,confluent-platform,Java,Mysql,Apache Kafka,Apache Kafka Connect,Confluent Platform,我试图使用MySQL表中的记录,该表包含3列(Axis、Price、lastname),数据类型分别为(int、decimal(14,4)、varchar(50)) 我插入了一条记录,其中包含以下数据(1,5.0000,John) 下面的Java代码(使用Confluent platform中MySQL连接器创建的主题中的AVRO记录)将十进制列:Price读取为Java.nio.HeapByteBuffer类型,因此在收到该列时无法获取该列的值 有没有办法将接收到的数据提取或转换为Java d
(Axis、Price、lastname)
,数据类型分别为(int、decimal(14,4)、varchar(50))
我插入了一条记录,其中包含以下数据(1,5.0000,John)
下面的Java代码(使用Confluent platform中MySQL连接器创建的主题中的AVRO记录)将十进制列:Price读取为Java.nio.HeapByteBuffer类型,因此在收到该列时无法获取该列的值
有没有办法将接收到的数据提取或转换为Java decimal或double数据类型
以下是MySQL连接器属性文件:-
{
"name": "mysql-source",
"config": {
"connector.class": "io.confluent.connect.jdbc.JdbcSourceConnector",
"key.converter": "io.confluent.connect.avro.AvroConverter",
"key.converter.schema.registry.url": "http://localhost:8081",
"value.converter": "io.confluent.connect.avro.AvroConverter",
"value.converter.schema.registry.url": "http://localhost:8081",
"incrementing.column.name": "Axis",
"tasks.max": "1",
"table.whitelist": "ticket",
"mode": "incrementing",
"topic.prefix": "mysql-",
"name": "mysql-source",
"validate.non.null": "false",
"connection.url": "jdbc:mysql://localhost:3306/ticket?
user=user&password=password"
}
}
以下是代码:-
public static void main(String[] args) throws InterruptedException,
IOException {
Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "group1");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
"org.apache.kafka.common.serialization.StringDeserializer");
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
"io.confluent.kafka.serializers.KafkaAvroDeserializer");
props.put("schema.registry.url", "http://localhost:8081");
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
String topic = "sql-ticket";
final Consumer<String, GenericRecord> consumer = new KafkaConsumer<String, GenericRecord>(props);
consumer.subscribe(Arrays.asList(topic));
try {
while (true) {
ConsumerRecords<String, GenericRecord> records = consumer.poll(100);
for (ConsumerRecord<String, GenericRecord> record : records) {
System.out.printf("value = %s \n", record.value().get("Price"));
}
}
} finally {
consumer.close();
}
}
publicstaticvoidmain(String[]args)抛出InterruptedException,
IOException{
Properties props=新属性();
put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,“localhost:9092”);
props.put(ConsumerConfig.GROUP_ID_CONFIG,“group1”);
props.put(ConsumerConfig.KEY\u反序列化程序\u类\u配置,
“org.apache.kafka.common.serialization.StringDeserializer”);
props.put(ConsumerConfig.VALUE\u反序列化程序\u类\u配置,
“io.confluent.kafka.serializers.Kafkavrodeserializer”);
put(“schema.registry.url”http://localhost:8081");
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,“最早”);
字符串topic=“sql票证”;
最终消费者=新卡夫卡消费者(道具);
consumer.subscribe(Arrays.asList(topic));
试一试{
while(true){
ConsumerRecords记录=consumer.poll(100);
对于(消费者记录:记录){
System.out.printf(“value=%s\n”,record.value().get(“Price”);
}
}
}最后{
consumer.close();
}
}
好吧,我终于找到了解决办法
Heapbytebuffer
需要转换为byte[]
数组,然后我使用BigInteger
从创建的字节数组构造值,然后我创建了一个BigDecimal
变量,该变量接受BigInteger的值,并使用movePointLeft(4)设置小数点
这是一个标尺(在我的例子中是4),一切都按预期进行
ByteBuffer buf = (ByteBuffer) record.value().get(("Price"));
byte[] arr = new byte[buf.remaining()];
buf.get(arr);
BigInteger bi =new BigInteger(1,arr);
BigDecimal bd = new BigDecimal(bi).movePointLeft(4);
System.out.println(bd);
以下是结果(左边是输出,右边是MySQL):-
您的消费者代码正在使用旧的、不推荐使用的消费者API。。。你是从哪里得到这些代码的?@cricket_007我正在使用confluent 4.0.0和这个版本中的java示例如果我使用最新版本,它能解决问题吗?嗯,文档的那部分已经过时了。。。我可以提交一个问题并更新它,但请看这个。具体来说,Zookeeper不再需要客户端与Kafka交互,否则,请尝试
avroRecord.get(“Price”).getFloat())
1)我没有使用JDBC源代码连接器,因此很遗憾,我不知道适合您的解决方案。必须有某种HeapByteBuffer
方法将字节带入您想要的类型。2) 我通常使用用AVROMaven插件构建的SpecificRecord avro类型,而不是普通的GenericRecords。