使用pojoTypes

时间:2017-12-06 19:46:23

标签: java casting apache-flink pojo

我的flink项目中有以下代码:

public class Test {

    public static void main(String[] args) throws Exception {

        // set up the execution environment
        final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        DataSet<Event> events =
            env.readCsvFile(args[0]).pojoType(
               Event.class,
               "time",
               "vid",
               "speed",
               "xWay",
               "lane",
               "dir",
               "seg",
               "pos"
            );

        System.out.println("----> " + events.count());
    }
}

这是班级Event

class Event {
    public int time;
    public int vid;
    public int speed;
    public int xWay;
    public int lane;
    public int dir;
    public int seg;
    public int pos;

    public Event() { }

    public Event(int time_in, int vid_in, int speed_in, int xWay_in, int lane_in, int dir_in, int seg_in, int pos_in) {
        this.time = time_in;
        this.vid = vid_in;
        this.speed = speed_in;
        this.xWay = xWay_in;
        this.lane = lane_in;
        this.dir = dir_in;
        this.seg = seg_in;
        this.pos = pos_in;
    }
}

项目编译但是当我运行它时,出现错误:

java.lang.ClassCastException: org.apache.flink.api.java.typeutils.GenericTypeInfo cannot be cast to org.apache.flink.api.java.typeutils.PojoTypeInfo

CSV文件有8个整数值,每行用逗号分隔。

documentation有以下示例:

DataSet<Person>> csvInput = env.readCsvFile("hdfs:///the/CSV/file")
                     .pojoType(Person.class, "name", "age", "zipcode");

我不知道POJO定义是否错误,肯定是错误的。我使用mapreadTextFile达到了我想要的效果,但这可能会更贵。

1 个答案:

答案 0 :(得分:1)

ClassCastException是一个将被修复的错误soon,并被更有意义的异常所取代。 EventGenericType而不是PojoType。我认为原因可能是Event是成员类而不是全局可访问类。添加static修饰符可以解决问题。