Kafka AVRO-从长到日期时间的转换 [英] Kafka AVRO - conversion from long to datetime

查看:359
本文介绍了Kafka AVRO-从长到日期时间的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想发送一条AVRO消息,其中包含长类型的字段时,出现以下错误:

I get the following error when I want to send an AVRO message which contains a field that has the type long:

Caused by: org.apache.kafka.common.errors.SerializationException: Error deserializing Avro message for id 61
Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to org.joda.time.DateTime

我使用Confluent 3.2.0和Apache Spark 2.2.0。在Spark Job中抛出此错误,该Spark Job处理AVRO消息并将其打印在控制台中。在AVRO模式中,相应的字段定义如下:

I use Confluent 3.2.0 and Apache Spark 2.2.0. This error is thrown in a Spark Job which processes AVRO messages and prints them in a console. In the AVRO schema, the corresponding field is defined like this:

{\"name\": \"event_time\", \"type\": { \"type\" : \"long\", \"logicalType\": \"timestamp-millis\"}}

.avsc 文件生成的Java类中,则该字段的定义如下:

In the Java class generated from the .avsc file, the field is defined as below:

private DateTime event_time;


推荐答案

我在使用Confluent 4.0.0和Avro 1.8.2。我有一个流处理器试图将long转换为DateTime。我通过添加正确的转换解决了这个问题。在开始任何处理逻辑之前,我使用了特定数据静态实用程序类,并添加正确的逻辑类型转换。

I encountered a similar issue using Confluent 4.0.0 and Avro 1.8.2. I had a stream processor that was attempting to convert a long to a DateTime. I overcame the issue by adding the correct conversion. Before I begin any processing logic, I used the Specific Data static utilities class and adding the correct Logical type conversion.

SpecificData.get().addLogicalTypeConversion(new TimeConversions.TimestampConversion());

这篇关于Kafka AVRO-从长到日期时间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆