Kafka AVRO 消费者:MySQL 十进制到 Java 十进制
Kafka AVRO Consumer: MySQL Decimal to Java Decimal
我正在尝试使用 MySQL table 中的记录,其中包含 3 列 (Axis, Price, lastname)
,它们的数据类型分别为 (int, decimal(14,4), varchar(50))
。
我插入了一条包含以下数据的记录(1, 5.0000, John)
。
下面的 Java 代码(它从 Confluent 平台中的 MySQL 连接器创建的主题中使用 AVRO 记录)读取小数列:价格,作为 java.nio.HeapByteBuffer 类型所以当我收到它时,我无法达到该列的值。
有没有办法将接收到的数据提取或转换为 Java 十进制或双精度数据类型?
这是 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();
}
}
好吧,我终于找到了解决方案。
Heapbytebuffer
需要转换为 byte[]
数组,然后我使用 BigInteger
从创建的字节数组构造值,然后我创建了一个 BigDecimal
取 BigInteger 值的变量,我用 movePointLeft(4)
设置小数点,这是 Scale(在我的例子中: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):-
我正在尝试使用 MySQL table 中的记录,其中包含 3 列 (Axis, Price, lastname)
,它们的数据类型分别为 (int, decimal(14,4), varchar(50))
。
我插入了一条包含以下数据的记录(1, 5.0000, John)
。
下面的 Java 代码(它从 Confluent 平台中的 MySQL 连接器创建的主题中使用 AVRO 记录)读取小数列:价格,作为 java.nio.HeapByteBuffer 类型所以当我收到它时,我无法达到该列的值。
有没有办法将接收到的数据提取或转换为 Java 十进制或双精度数据类型?
这是 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();
}
}
好吧,我终于找到了解决方案。
Heapbytebuffer
需要转换为 byte[]
数组,然后我使用 BigInteger
从创建的字节数组构造值,然后我创建了一个 BigDecimal
取 BigInteger 值的变量,我用 movePointLeft(4)
设置小数点,这是 Scale(在我的例子中: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):-