栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Flink对keyBy汇总后的数据排序

Flink对keyBy汇总后的数据排序

val env = StreamExecutionEnvironment.getExecutionEnvironment
      env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)
      env.setParallelism(1)

    val sourceStream = env.fromCollection(List(
      "a,1646150400000",
      "a,1646150403000",
      "a,1646150406000",
      "a,1646150410000",
      "a,1646150415000",
      "b,1646150400000",
      "b,1646150403000",
      "b,1646150410000",
      "b,1646150415000",
      "c,1646150400000",
      "c,1646150403000",
      "c,1646150415000"
    )).assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor[String](Time.seconds(3)){
      override def extractTimestamp(t: String): Long = t.split(",")(1).toLong
    })

    val aggdata: DataStream[(Long, String, Int)] = sourceStream.map(
      line => {
        val splits = line.split(",")
        (splits(0), 1)
      }
    ).keyBy(_._1)
      .timeWindow(Time.seconds(10), Time.seconds(5))
      //IN, OUT, KEY, W
      .reduce((v1:(String,Int), v2:(String,Int)) => {
        (v1._1, v1._2 + v2._2)
      }, new ProcessWindowFunction[(String, Int), (Long, String, Int), String, TimeWindow] {
        override def process(key: String, ctx: Context, elements: Iterable[(String, Int)], out: Collector[(Long, String, Int)]): Unit = {
          val windowTag = ctx.window.getEnd
          for (e <- elements) {
            out.collect((windowTag, e._1, e._2))
          }
        }
      })

//    aggData.print()

    aggData
      .keyBy(_._1)
      .timeWindowAll(Time.seconds(5))
      .apply(new AllWindowFunction[(Long,String,Int),String,TimeWindow]{
        override def apply(window: TimeWindow, input: Iterable[(Long, String, Int)], out: Collector[String]): Unit = {
          out.collect(input.seq.toList.sortBy(_._3).reverse.toList.toString())
        }
      })
      .print()

    env.execute()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/753787.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号