1

我正在使用这个Flink CEP示例,我在其中分离出数据,因为我创建了一个应用程序,该应用程序正在将应用程序发送到 Kafka 和另一个从 Kafka 读取的应用程序......我为类TemperatureWarning生成了生产者,即在 Kafka 中,我正在发送数据与TemperatureWarning相关 以下是我的代码,它正在使用来自 Kafka 的数据......

StreamExecutionEnvironment env=StreamExecutionEnvironment.getExecutionEnvironment();
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
env.enableCheckpointing(5000);
Properties properties=new Properties();
properties.setProperty("bootstrap.servers", "PUBLICDNS:9092");
properties.setProperty("zookeeper.connect", "PUBLICDNS:2181");
properties.setProperty("group.id", "test");



DataStream<TemperatureWarning> dstream=env.addSource(new FlinkKafkaConsumer09<TemperatureWarning>("MonitoringEvent", new MonitoringEventSchema(), properties));

  Pattern<TemperatureWarning, ?> alertPattern = Pattern.<TemperatureWarning>begin("first")
          .next("second")
          .within(Time.seconds(20));


  PatternStream<TemperatureWarning> alertPatternStream = CEP.pattern(
          dstream.keyBy("rackID"),
          alertPattern);


  DataStream<TemperatureAlert> alerts = alertPatternStream.flatSelect(
      (Map<String, TemperatureWarning> pattern, Collector<TemperatureAlert> out) -> {
          TemperatureWarning first = pattern.get("first");
          TemperatureWarning second = pattern.get("second");

          if (first.getAverageTemperature() < second.getAverageTemperature()) {
              out.collect(new TemperatureAlert(second.getRackID(),second.getAverageTemperature(),second.getTimeStamp()));
          }
      });
dstream.print();
alerts.print();

env.execute("Flink Kafka Consumer");

但是当我执行这个应用程序时,它会抛出以下异常:

Exception in thread "main" java.lang.NullPointerException
at org.apache.flink.api.common.operators.Keys$ExpressionKeys.<init>(Keys.java:329)
at org.apache.flink.streaming.api.datastream.DataStream.keyBy(DataStream.java:274)
at com.yash.consumer.KafkaFlinkConsumer.main(KafkaFlinkConsumer.java:49)

以下是我的课TemperatureWarning

public class TemperatureWarning {

private int rackID;
private double averageTemperature;
private long timeStamp;

public TemperatureWarning(int rackID, double averageTemperature,long timeStamp) {
    this.rackID = rackID;
    this.averageTemperature = averageTemperature;
    this.timeStamp=timeStamp;
}

public TemperatureWarning() {
    this(-1, -1,-1);
}

public int getRackID() {
    return rackID;
}

public void setRackID(int rackID) {
    this.rackID = rackID;
}

public double getAverageTemperature() {
    return averageTemperature;
}

public void setAverageTemperature(double averageTemperature) {
    this.averageTemperature = averageTemperature;
}


public long getTimeStamp() {
    return timeStamp;
}

public void setTimeStamp(long timeStamp) {
    this.timeStamp = timeStamp;
}

@Override
public boolean equals(Object obj) {
    if (obj instanceof TemperatureWarning) {
        TemperatureWarning other = (TemperatureWarning) obj;

        return rackID == other.rackID && averageTemperature == other.averageTemperature;
    } else {
        return false;
    }
}

@Override
public int hashCode() {
    return 41 * rackID + Double.hashCode(averageTemperature);
}

@Override
public String toString() {
   //return "TemperatureWarning(" + getRackID() + ", " + averageTemperature + ")";
    return "TemperatureWarning(" + getRackID() +","+averageTemperature + ") "+ "," + getTimeStamp(); 
}

}

以下是我的课程MonitoringEventSchema

public class MonitoringEventSchema implements DeserializationSchema<TemperatureWarning>,SerializationSchema<TemperatureWarning>

{

@Override
public TypeInformation<TemperatureWarning> getProducedType() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public byte[] serialize(TemperatureWarning element) {
    // TODO Auto-generated method stub
    return element.toString().getBytes();
}

@Override
public TemperatureWarning deserialize(byte[] message) throws IOException {
    // TODO Auto-generated method stub
    if(message!=null)
    {
    String str=new String(message,"UTF-8");
    String []val=str.split(",");
    TemperatureWarning warning=new TemperatureWarning(Integer.parseInt(val[0]),Double.parseDouble(val[1]),Long.parseLong(val[2]));
    return warning;
    }
    return null;
}

@Override
public boolean isEndOfStream(TemperatureWarning nextElement) {
    // TODO Auto-generated method stub
    return false;
}

}

现在执行keyBy操作需要什么,因为我已经提到流分区所需的密钥?这里需要做什么来解决这个错误??

4

1 回答 1

1

问题出在这个函数中:

@Override
public TypeInformation<TemperatureWarning> getProducedType() {
    // TODO Auto-generated method stub
    return null;
}   

你不能在这里返回 null。

于 2016-06-02T15:50:31.517 回答