Java 阿帕奇·弗林克:can';t将writeAsCsv()与子类tuple的数据流一起使用
如此处所建议的:。我在数据流中使用POJO而不是元组 以下是我的POJO的定义:Java 阿帕奇·弗林克:can';t将writeAsCsv()与子类tuple的数据流一起使用,java,apache-flink,flink-streaming,Java,Apache Flink,Flink Streaming,如此处所建议的:。我在数据流中使用POJO而不是元组 以下是我的POJO的定义: public class PositionEvent extends Tuple8<Integer, String, Integer, Integer, Integer, Integer, Integer, Integer> 线程“main”java.lang.IllegalArgumentException中的异常:writeAsCsv()方法只能用于元组的数据流 但是,如果我显式地将Po
public class PositionEvent extends Tuple8<Integer, String, Integer,
Integer, Integer, Integer, Integer, Integer>
线程“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))
这是我以前的工作:
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);
}
}
public类PositionEvent扩展Tuple8{
公共整数时间戳;
公共字符串视频;
公共交通速度;
公共通道;
公共内巷;;
公共int dir;
公共int seg;
公共int pos;
公共事件(){
}
公共位置事件(int时间戳、字符串视频、int速度、int xway、,
内部车道、内部方向、内部分段、内部位置){
超级(时间戳、视频、速度、X方向、车道、方向、seg、pos);
}
}
现在我不需要显式地强制转换POJO。似乎您不仅扩展了
Tuple8
,还添加了其他字段,如e.speed
。这隐式地使您的类型成为POJO。为了命名字段并保持高效的元组类型,您可以简单地实现一个getter,但不添加额外的字段。否则,您可以简单地使用POJO而不是元组
这也可能值得研究。它旨在通过自动处理所有类型来简化开发。非常感谢您的回复。它起作用了,但我不明白为什么。我从POJO中删除了所有成员,并添加了一个getSpeed()。我同意文档中对此不是很具体。只有在
TupleXX
类的字段未更改的情况下,元组才是有效的元组类型。否则,该对象需要为其他字段使用不同的序列化逻辑,从而使其成为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;
}
}
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);
}
}