报错:org.apache.flink.table.api.TableException: Only the first field can reference an atomic type.
//流执行环境
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//table Environment
StreamTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(env);
//创建datasource,从一个静态的集合类中创建
DataStream<pojo> ds = env.fromCollection( Arrays.asList(
new pojo("123",1),
new pojo("234",2)
) );
//根据stream注册table
tableEnv.registerDataStream("myTable", ds, "name,num");
//执行table操作
String sql = "SELECT * FROM myTable";
Table sqlResult = tableEnv.sqlQuery(sql);
//解释执行计划
// String explanation = tableEnv.explain(sqlResult);
// System.out.println(explanation);
DataStream<Tuple2<Boolean, Row>> retractStream =
tableEnv.toRetractStream(sqlResult, Row.class);
//注册sink,常用的有concole 和spark的差不多(可以自定义sink实现数据库操作等)
retractStream.print();
// retractStream.addSink( new PrintSinkFunction());
try {
env.execute("Socket Window WordCount");
} catch (Exception e) {
e.printStackTrace();
}
我的pojo类为
public class pojo implements Serializable {
public String name;
public int num;
public pojo(){
}
public pojo(String name,int num){
// super(pojo.class, li);
this.name = name;
this.num = num;
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public String toString() {
return this.name+"$"+this.num;
}
@Override
public boolean equals(Object obj) {
return this.equals( obj );
}
}
这样直接执行会报错,因为pojo类貌似必须携带 public pojo(){ }无参构造器。
tuple要使用java包里面的(scala import org.apache.flink.api.java.tuple._),而不是scala自带的tuple,不然也会认为是geneic类型,导致报错
具体源码可以查看方法 env.fromCollection(),
这里面会维护一个TypeInformation<OUT>类型用于后续类解析。
flink 大神链接:http://ju.outofmemory.cn/feed/1264/?page=1