1

この例のFlink CEPを使用しています。ここでは、アプリケーションを Kafka に送信するアプリケーションと、Ka​​fka から読み取る別のアプリケーションを作成したため、データを分離しています...クラスTemperatureWarningのプロデューサーを生成しました。つまり、Kafka でデータを送信していました。以下は、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