可理解为时间轴,可将无界流切分成有界流
滑动窗口
滚动窗口
会话窗口
会话窗口:由一系列事件指定事件长度间隙组成,类比wed应用的session
group windows
flinkSQL中通过Groupby Windows函数来定义分组窗口
data_time,price,product_id,buyername
1666620609,44,1,白天磊
1666620610,45,1,陈智渊
1666620611,46,1,崔钰轩
1666620612,47,1,吴鹏飞
1666620613,48,1,毛明辉
1666620614,49,1,侯弘文
1666620615,50,1,曾伟祺
1666620616,51,1,郝瑞霖
1666620617,52,1,陆熠彤
1666620618,53,1,余弘文
1666620619,54,1,石哲瀚
1666620620,55,1,任擎苍
1666620621,56,1,卢文轩
1666620622,57,1,吕晋鹏
1666620623,58,1,罗晟睿
1666620624,59,1,周建辉
1666620625,60,1,卢皓轩
1666620626,61,1,沈煜城
1666620627,62,1,万鑫鹏
1666620628,63,1,沈思远
//使用插件生成有无参构造器以及重写一些方法
@Data//完成了Getter,Setter,equals,hasCode,toString 等方法
@Builder//省去写很多构造函数的麻烦
@NoArgsConstructor//自动添加一个无参构造函数
@AllArgsConstructor//为自动添加一个构造函数
public class Userproduct {
private Integer product_id;
private String buyer_name;
private Long date_time;
private Double price;
}
StreamExecutionEnvironment senv = StreamExecutionEnvironment.getExecutionEnvironment();
senv.setParallelism(1);//设置并行度
StreamTableEnvironment tEnv = StreamTableEnvironment.create(senv);
//泛型指定为Userproduct对象
//指定乱序时间两秒
//复写方法extractTimestamp
WatermarkStrategy<Userproduct> watermarkStrategy = WatermarkStrategy.<Userproduct>forBoundedOutOfOrderness(Duration.ofSeconds(2))
.withTimestampAssigner(new SerializableTimestampAssigner<Userproduct>() {
@Override
public long extractTimestamp(Userproduct userproduct, long l) {
return userproduct.getDate_time() * 1000;//需要得到毫秒值
}
});
//从socket读取数据,指定水位线
DataStream<Userproduct> userProductDataStream = senv.socketTextStream("hadoop1", 9999)
.map(event -> {
String[] arr = event.split(",");
Userproduct userproduct = Userproduct.builder()
.product_id(Integer.parseInt(arr[2]))
.buyer_name(arr[3])
.date_time(Long.valueOf(arr[0]))
.price(Double.valueOf(arr[1]))
.build();
return userproduct;
}).assignTimestampsAndWatermarks(watermarkStrategy);
Table table = tEnv.fromDataStream(userProductDataStream,
$("product_id"),//跟上字段
$("buyer_name"),
$("price"),
$("date_time").rowtime());//通过调用rowtime来指定event_time为准
这边TUMBLE指的是定义滚动窗口,select后面的窗口字段要在groupby也要出现
Table resultTable = tEnv.sqlQuery(
"select product_id,max(price),TUMBLE_START(date_time,INTERVAL '5' second) as winstart " +
"from " + table +
" GROUP BY product_id , TUMBLE(date_time, INTERVAL '5' second)");//间隔5秒
public class FlinkSQLTumbEvtWindowTime {
public static void main(String[] args) {
//构建表执行环境
StreamExecutionEnvironment senv = StreamExecutionEnvironment.getExecutionEnvironment();
senv.setParallelism(1);
StreamTableEnvironment tEnv = StreamTableEnvironment.create(senv);
//定义一个水位线
//泛型指定为Userproduct对象
//指定乱序时间两秒
//复写方法extractTimestamp
WatermarkStrategy<Userproduct> watermarkStrategy = WatermarkStrategy.<Userproduct>forBoundedOutOfOrderness(Duration.ofSeconds(2))
.withTimestampAssigner(new SerializableTimestampAssigner<Userproduct>() {
@Override
public long extractTimestamp(Userproduct userproduct, long l) {
return userproduct.getDate_time() * 1000;//需要得到毫秒值
}
});
//从socket读取数据,指定水位线
DataStream<Userproduct> userProductDataStream = senv.socketTextStream("hadoop1", 9999)
.map(event -> {
String[] arr = event.split(",");
Userproduct userproduct = Userproduct.builder()
.product_id(Integer.parseInt(arr[2]))
.buyer_name(arr[3])
.date_time(Long.valueOf(arr[0]))
.price(Double.valueOf(arr[1]))
.build();
return userproduct;
}).assignTimestampsAndWatermarks(watermarkStrategy);
//将流式数据给转换成为动态表
Table table = tEnv.fromDataStream(userProductDataStream,
$("product_id"),//跟上字段
$("buyer_name"),
$("price"),
$("date_time").rowtime());//通过调用rowtime来指定event_time为准
//执行flink的sql程序
Table resultTable = tEnv.sqlQuery(
"select product_id,max(price),TUMBLE_START(date_time,INTERVAL '5' second) as winstart " +
"from " + table +
" GROUP BY product_id , TUMBLE(date_time, INTERVAL '5' second)");
resultTable.execute().print();
}
}
resultTable.execute().print();
//使用HOP,滑动大小2秒,窗口大小4秒
Table resultTable = tEnv.sqlQuery("select product_id,max(price),HOP_START(date_time,INTERVAL '2' second,INTERVAL '4' second) as winstart " +
"from " + table +
" group by product_id ,HOP(date_time,INTERVAL '2' second,INTERVAL '4' second)");
Table resultTable = tEnv.sqlQuery("select product_id,max(price) , SESSION_START(date_time,INTERVAL '5' second ) as winstart " +
"from " + table +
" group by product_id ,SESSION(date_time,INTERVAL '5' second)");
select 分析函数 over (partitionBy 字段 orderby 字段 <开窗范围> ) from group by
--范围间隔,例如开窗范围选择当前行之前 1 小时的数据
RANGE BETWEEN INTERVAL '1' HOUR PRECEDING AND CURRENT ROW
--行间隔,例如开窗范围选择当前行之前的 5 行数据(含当前行6行数据)
ROWS BETWEEN 5 PRECEDING AND CURRENT ROW
1666620609,44,1,白天磊
1666620610,45,1,陈智渊
1666620611,46,1,崔钰轩
1666620612,47,1,吴鹏飞
1666620613,48,1,毛明辉
1666620614,49,1,侯弘文
1666620615,50,1,曾伟祺
1666620616,51,1,郝瑞霖
1666620617,52,1,陆熠彤
1666620618,53,1,余弘文
1666620619,54,1,石哲瀚
1666620620,55,1,任擎苍
1666620621,56,1,卢文轩
1666620622,57,1,吕晋鹏
1666620623,58,1,罗晟睿
1666620624,59,1,周建辉
1666620625,60,1,卢皓轩
1666620626,61,1,沈煜城
1666620627,62,1,万鑫鹏
1666620628,63,1,沈思远
Table resultTable = tEnv.sqlQuery(
"select product_id,
max(price) " + "OVER w AS max_price, " +
"avg(price) OVER w AS avg_price " +
"from " + table +
" WINDOW w AS ( PARTITION BY product_id ORDER BY date_time RANGE BETWEEN INTERVAL '5' second PRECEDING AND CURRENT ROW) ");
当然也可以这么写
Table resultTable = tEnv.sqlQuery(
"select product_id,
max(price) " + "OVER ( PARTITION BY product_id ORDER BY date_time RANGE BETWEEN INTERVAL '5' second PRECEDING AND CURRENT ROW) AS max_price, " +
"avg(price) OVER ( PARTITION BY product_id ORDER BY date_time RANGE BETWEEN INTERVAL '5' second PRECEDING AND CURRENT ROW) AS avg_price " +
"from " + table
Table resultTable = tEnv.sqlQuery("select product_id,max(price) " +
"OVER w AS max_price, avg(price) OVER w AS avg_price " +
" from "
+ table
+ " WINDOW w AS ( PARTITION BY product_id ORDER BY date_time ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) ");