首先创建一个流
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Nan {
private String xing;
private String name;
private Long ts;
}
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
DataStreamSource<String> sourceNan = env.socketTextStream("hdp01", 1111);
DataStreamSource<String> sourceNv = env.socketTextStream("hdp01", 2222);
System.setProperty("java.net.preferIPv4Stack", "true");
SingleOutputStreamOperator<Nan> beanNan = sourceNan.map(new MapFunction<String, Nan>() {
@Override
public Nan map(String s) throws Exception {
try {
String[] split = s.split(",");
return new Nan(split[0].substring(0, 1), split[1], Long.parseLong(split[2]));
} catch (Exception e) {
return null;
}
}
}).filter(Objects::nonNull).assignTimestampsAndWatermarks(
WatermarkStrategy.<Nan>forMonotonousTimestamps().withTimestampAssigner(new SerializableTimestampAssigner<Nan>() {
@Override
public long extractTimestamp(Nan nan, long l) {
return nan.getTs();
}
})).returns(TypeInformation.of(Nan.class));
流转表
流转表的时候有一个点要注意,watermark必须要重新指定,否则会丢失,常用的方式如下
1、沿用流上的watermark
tenv.createTemporaryView("nan", beanNan, Schema.newBuilder()
.column("xing", DataTypes.STRING())
.column("name", DataTypes.STRING())
.column("ts", DataTypes.TIMESTAMP_LTZ(3))
.watermark("rt","source_watermark()")
.build() );
2、重新创建watermark
tenv.createTemporaryView("nan", beanNan, Schema.newBuilder()
.column("xing", DataTypes.STRING())
.column("name", DataTypes.STRING())
.column("rt", DataTypes.BIGINT())
.watermark("rt","rt - interval '1' second")
.build() );
3、根据内置属性rowtime创建watermark
tenv.createTemporaryView("nan1", beanNan, Schema.newBuilder()
.column("xing", DataTypes.STRING())
.column("name", DataTypes.STRING())
.column("ts", DataTypes.BIGINT())
.columnByMetadata("rt", DataTypes.TIMESTAMP_LTZ(3),"rowtime")
.watermark("rt","rt - interval '1' second")
.build() );
使用表
TableResult tableResult = tenv.executeSql("select *,current_watermark(rt) from nan");
tableResult.print();