Apache Flink:不能将writeAsCsv()与子类元组的数据流一起使用

时间:2017-11-27 11:46:14

标签: java apache-flink flink-streaming

根据此处的建议:Best Practices - Naming large TupleX types。我正在为我的数据流使用POJO而不是Tuple。

这是我POJO的定义:

public class PositionEvent extends Tuple8<Integer, String, Integer, 
    Integer, Integer, Integer, Integer, Integer>

如果我尝试将PositionEvent的数据流保存到csv文件,则会抛出异常:

source.filter((PositionEvent e) -> e.speed > MAXIMUM_SPEED)
            .writeAsCsv(String.format("%s/%s", outputFolder, SPEED_RADAR_FILE))
  

线程“main”中的异常java.lang.IllegalArgumentException:writeAsCsv()方法只能用于元组的数据流。

但是,如果我明确地将PositionEvent强制转换为Tuple8,它可以工作:

source.filter((PositionEvent e) -> e.speed > MAXIMUM_SPEED)
            .map((PositionEvent e) ->
                    (Tuple8<Integer, String, Integer, Integer,
                            Integer, Integer, Integer, Integer>) e)
            .writeAsCsv(String.format("%s/%s", outputFolder, SPEED_RADAR_FILE))

Flink不应该检测到数据流中的对象属于Tuple子类吗?

====================

编辑:(感谢twalthr)

好的,这是我现在的POJO:

import org.apache.flink.api.java.tuple.Tuple8;

public class PositionEvent extends Tuple8<Integer, String, Integer,
        Integer, Integer, Integer, Integer, Integer> {

    public PositionEvent() {
    }

    public PositionEvent(int timestamp, String vid, int speed, int xway,
                         int lane, int dir, int seg, int pos) {
        super(timestamp, vid, speed, xway, lane, dir, seg, pos);
    }

    public int getSpeed() {
        return f2;
    }
}

这是我之前的POJO:

public class PositionEvent extends Tuple8<Integer, String, Integer,
        Integer, Integer, Integer, Integer, Integer> {

    public int timestamp;

    public String vid;

    public int speed;

    public int xway;

    public int lane;

    public int dir;

    public int seg;

    public int pos;

    public PositionEvent() {
    }

    public PositionEvent(int timestamp, String vid, int speed, int xway,
                         int lane, int dir, int seg, int pos) {
        super(timestamp, vid, speed, xway, lane, dir, seg, pos);
    }
}

现在我不需要明确地投射我的POJO。

0 个答案:

没有答案