gpt4 book ai didi

java - Flink readCsvFile 方法与 pojoTypes

转载 作者:行者123 更新时间:2023-11-30 10:21:45 25 4
gpt4 key购买 nike

我的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 实现了我想要的,但这可能更昂贵。

最佳答案

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

关于java - Flink readCsvFile 方法与 pojoTypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47682317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com