如果使用Flink CEP库找到Hello和world,我试图打印出一个字符串。我的源代码是Kafka,并使用console-producer输入数据。那部分是有效的。我可以打印出我在主题中输入的内容。但是,它不会打印出我的最后一条消息“世界真是太棒了!”。它甚至不会打印出它进入lambda。下面是班级
package kafka;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer08;
import org.apache.flink.streaming.util.serialization.SimpleStringSchema;
import org.apache.flink.util.Collector;
import java.util.Map;
import java.util.Properties;
/**
* Created by crackerman on 9/16/16.
*/
public class WordCount {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.put("bootstrap.servers", "localhost:9092");
properties.put("zookeeper.connect", "localhost:2181");
properties.put("group.id", "test");
StreamExecutionEnvironment see = StreamExecutionEnvironment.getExecutionEnvironment();
see.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
DataStream<String> src = see.addSource(new FlinkKafkaConsumer08<>("complexString",
new SimpleStringSchema(),
properties));
src.print();
Pattern<String, String> pattern = Pattern.<String>begin("first")
.where(evt -> evt.contains("Hello"))
.followedBy("second")
.where(evt -> evt.contains("World"));
PatternStream<String> patternStream = CEP.pattern(src, pattern);
DataStream<String> alerts = patternStream.flatSelect(
(Map<String, String> in, Collector<String> out) -> {
System.out.println("Made it to the lambda");
String first = in.get("first");
String second = in.get("second");
System.out.println("First: " + first);
System.out.println("Second: " + second);
if (first.equals("Hello") && second.equals("World")) {
out.collect("The world is so nice!");
}
});
alerts.print();
see.execute();
}
}
非常感谢任何帮助。
谢谢!
答案 0 :(得分:1)
问题是以下一行
see.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
如果删除它,它会按照我预期的方式工作。