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)
      }
    )
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flink 中,对已经通过 `keyBy` 分好组的数据再次进行分区,可以使用 `partitionCustom` 或 `partitionCustomWithOperator` 函数。 `partitionCustom` 函数可以接收一个 `Partitioner` 对象作为参数,该对象用于定义自定义的分区逻辑。`partitionCustomWithOperator` 函数可以接收一个 `KeySelector` 对象和一个 `Partitioner` 对象作为参数,前者用于从数据中提取 Key,后者用于定义自定义的分区逻辑。 下面是一个示例代码,演示了如何对已经通过 `keyBy` 分好组的数据再次进行分区: ``` DataStream<Tuple2<String, Integer>> dataStream = ...; // 按照 Tuple2 的第一个元素进行分区 KeyedStream<Tuple2<String, Integer>, String> keyedStream = dataStream .keyBy(data -> data.f0); // 对分区中的数据再次进行分区 DataStream<Tuple2<String, Integer>> resultStream = keyedStream .partitionCustom(new MyPartitioner()) .map(data -> new Tuple2<>(data.f0, data.f1 * 2)); // 自定义分区逻辑 public static class MyPartitioner implements Partitioner<Integer> { @Override public int partition(Integer key, int numPartitions) { if (key % 2 == 0) { return 0; } else { return 1; } } } ``` 在上面的代码中,我们首先使用 `keyBy` 按照 Tuple2 的第一个元素进行分区,然后使用 `partitionCustom` 对每个分区内的数据再次进行分区,最后使用 `map` 对每个数据进行处理。 注意,`partitionCustom` 和 `partitionCustomWithOperator` 都是宽依赖算子,因此在分布式计算时,它们会引起数据重分区,因此可能会影响程序的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值