Java 使用KStream将字符串更改为avro inKafka Streams时出现空指针异常
我被困在这个问题上,搞不清楚到底发生了什么。我正在尝试使用Kafka streams为主题编写日志。另一方面,我让Kafka connect将每个条目输入MySQL。所以,基本上我需要的是一个Kafka streams程序,它将一个主题作为字符串读取,并将其解析为Avro格式,然后将其输入另一个主题 以下是我编写的代码:Java 使用KStream将字符串更改为avro inKafka Streams时出现空指针异常,java,avro,apache-kafka-streams,Java,Avro,Apache Kafka Streams,我被困在这个问题上,搞不清楚到底发生了什么。我正在尝试使用Kafka streams为主题编写日志。另一方面,我让Kafka connect将每个条目输入MySQL。所以,基本上我需要的是一个Kafka streams程序,它将一个主题作为字符串读取,并将其解析为Avro格式,然后将其输入另一个主题 以下是我编写的代码: //Define schema String userSchema = "{" + "\"type\":\"re
//Define schema
String userSchema = "{"
+ "\"type\":\"record\","
+ "\"name\":\"myrecord\","
+ "\"fields\":["
+ " { \"name\":\"ID\", \"type\":\"int\" },"
+ " { \"name\":\"COL_NAME_1\", \"type\":\"string\" },"
+ " { \"name\":\"COL_NAME_2\", \"type\":\"string\" }"
+ "]}";
String key = "key1";
Schema.Parser parser = new Schema.Parser();
Schema schema = parser.parse(userSchema);
//Settings
System.out.println("Kafka Streams Demonstration");
//Settings
Properties settings = new Properties();
// Set a few key parameters
settings.put(StreamsConfig.APPLICATION_ID_CONFIG, APP_ID);
// Kafka bootstrap server (broker to talk to); ubuntu is the host name for my VM running Kafka, port 9092 is where the (single) broker listens
settings.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
// Apache ZooKeeper instance keeping watch over the Kafka cluster; ubuntu is the host name for my VM running Kafka, port 2181 is where the ZooKeeper listens
settings.put(StreamsConfig.ZOOKEEPER_CONNECT_CONFIG, "localhost:2181");
// default serdes for serialzing and deserializing key and value from and to streams in case no specific Serde is specified
settings.put(StreamsConfig.KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
settings.put(StreamsConfig.VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
settings.put(StreamsConfig.STATE_DIR_CONFIG ,"/tmp");
// to work around exception Exception in thread "StreamThread-1" java.lang.IllegalArgumentException: Invalid timestamp -1
// at org.apache.kafka.clients.producer.ProducerRecord.<init>(ProducerRecord.java:60)
// see: https://groups.google.com/forum/#!topic/confluent-platform/5oT0GRztPBo
// Create an instance of StreamsConfig from the Properties instance
StreamsConfig config = new StreamsConfig(getProperties());
final Serde < String > stringSerde = Serdes.String();
final Serde < Long > longSerde = Serdes.Long();
final Serde<byte[]> byteArraySerde = Serdes.ByteArray();
// building Kafka Streams Model
KStreamBuilder kStreamBuilder = new KStreamBuilder();
// the source of the streaming analysis is the topic with country messages
KStream<byte[], String> instream =
kStreamBuilder.stream(byteArraySerde, stringSerde, "sqlin");
final KStream<byte[], GenericRecord> outstream = instream.mapValues(new ValueMapper<String, GenericRecord>() {
@Override
public GenericRecord apply(final String record) {
System.out.println(record);
GenericRecord avroRecord = new GenericData.Record(schema);
String[] array = record.split(" ", -1);
for (int i = 0; i < array.length; i = i + 1) {
if (i == 0)
avroRecord.put("ID", Integer.parseInt(array[0]));
if (i == 1)
avroRecord.put("COL_NAME_1", array[1]);
if (i == 2)
avroRecord.put("COL_NAME_2", array[2]);
}
System.out.println(avroRecord);
return avroRecord;
}
});
outstream.to("sqlout");
主题sqlin包含一些消息,这些消息由一个数字后跟两个单词组成。注意两行打印:函数获取一条消息,并在捕获空指针之前成功解析它。问题是我对Java、Kafka和Avro还不熟悉,所以我不确定我会错在哪里。我是否正确设置了Avro模式?还是我用错了kstream?非常感谢您的帮助。我认为问题出在以下几行:
outstream.to("sqlout");
默认情况下,您的应用程序配置为使用字符串
serde作为记录键和记录值:
settings.put(StreamsConfig.KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
settings.put(StreamsConfig.VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
由于extream
具有类型KStream
,因此在调用to()
时必须提供明确的SERDE:
仅供参考:Confluent Platform的下一版本(预计时间:本月=6月)
2017)将与
这与。这
应该让你的生活更轻松
有关更多详细信息,请参阅我的答案。我认为问题在于以下几行:
outstream.to("sqlout");
默认情况下,您的应用程序配置为使用字符串
serde作为记录键和记录值:
settings.put(StreamsConfig.KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
settings.put(StreamsConfig.VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
由于extream
具有类型KStream
,因此在调用to()
时必须提供明确的SERDE:
仅供参考:Confluent Platform的下一版本(预计时间:本月=6月)
2017)将与
这与。这
应该让你的生活更轻松
有关更多详细信息,请参见我的答案。看看这个看看这个顺便说一下,您可能还想看看我们新的Kafka Connect+Kafka Streams集成端到端演示:顺便说一句,您可能还想看看我们新的Kafka Connect+Kafka Streams集成端到端演示: