【发布时间】:2017-06-07 15:59:51
【问题描述】:
我正在尝试使用函数“mapValues”将在原始流“textlines”中获得的字符串值转换为 JSONObject 消息到 newStream。然后将我在 newStream 中获得的任何内容流式传输到一个名为“testoutput”的主题上。但是每次消息实际上通过转换块时,我都会收到一个 NullPointerException 错误,该错误仅指向 kafka 流库。不知道发生了什么:((
附:当我从原始流派生/创建新的 kafka 流时,新流是否属于原始构建器?由于我需要构建器来创建 KafkaStreams 对象并开始流式传输,因此我不确定是否需要对新流执行其他操作,而不仅仅是指定它的去向 .to("topic")
//Testing a Kafka Stream Application
public class testStream {
public static void main(String[] args) throws Exception {
//Configurations
Properties props = new Properties();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-teststream");
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "xxxxxxxxxxxx:xxxx");
props.put(StreamsConfig.KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
props.put(StreamsConfig.VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
props.put(StreamsConfig.TIMESTAMP_EXTRACTOR_CLASS_CONFIG, WallclockTimestampExtractor.class);
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
//Building Stream
KStreamBuilder builder = new KStreamBuilder();
KStream<String, String> textlines = builder.stream("mytest2");
//Printout The Inputs just for testing purposes
textlines.foreach(new ForeachAction<String, String>(){
public void apply(String key, String value){
for(int y=0; y<value.length(); y++){
System.out.print(value.charAt(y));
}
System.out.print("\n");
}
});
//Transform String Records to JSON Objects
KStream<String, JSONObject> newStream = textlines.mapValues(new ValueMapper<String,JSONObject>(){
@Override
public JSONObject apply(String value) {
JSONObject jsnobj = new JSONObject();
//If the first 4 letters of the message is "xxxx" then parse it to a
//JSON Object, otherwise create a dummy
if(value.substring(0, 4).equals("xxxx")){
jsnobj.put("Header_Title", value.substring(0, 4));
jsnobj.put("Data_Part", value.substring(4));
}else{
jsnobj.put("Header_Title", "Not xxxx");
jsnobj.put("Data_Part", "None");
}
return jsnobj;
}
});
//Specify target
newStream.to("testoutput");
//Off you go
KafkaStreams streams=new KafkaStreams(builder, props);
streams.start();
}
}
【问题讨论】:
-
您应该包含错误消息 /stacktrace,以便本问题的读者可以帮助您了解发生 NullPointerException 的原因。
标签: java stream apache-kafka kafka-consumer-api apache-kafka-streams