flink分组取最小值

Posted nohert

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flink分组取最小值相关的知识,希望对你有一定的参考价值。

import org.apache.flink.api.common.functions.{FilterFunction, ReduceFunction, RichMapFunction}
import org.apache.flink.configuration.Configuration
import org.apache.flink.streaming.api.scala._
object TransformTest {
  def main(args: Array[String]): Unit = {
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    env.setParallelism(1)
    //0.从文件中读取数据
    val inputPath = "D:\\\\ideaDemo\\\\maven_flink\\\\src\\\\main\\\\resources\\\\sensor.txt";
    val inputStream = env.readTextFile(inputPath)

    //1.先转换成样例类类型(简单转换操作)
    val dataStream = inputStream.map(data => {
      val arr = data.split(",")
      SensorReding(arr(0), arr(1).toLong, arr(2).toDouble)
    })
    //2.分组聚合,输出 每个传感器当前最小值
    val aggSteam = dataStream
      .keyBy("id") //根据id进行分组
      .minBy("temperature")

    aggSteam.print()

   env.execute()
  }

 数据

sensor_1,1547718101,35.8
sensor_1,1547718102,22.2
sensor_1,1547718101,55.3
sensor_1,1547718102,24.1
sensor_1,1547718103,57
sensor_1,1547718103,58
sensor_1,1547718103,59
sensor_6,1547718101,15.4
sensor_7,1547718102,6.7
sensor_10,1547718205,38.1

  

以上是关于flink分组取最小值的主要内容,如果未能解决你的问题,请参考以下文章

Java8 Stream针对List先分组再求和最大值最小值平均值等

mysql 分组总和求最大值 的两种方式

mssql 2005查询 请问我要实现分组获取最大值,怎么写sql查询指令

(叨逼叨)基于Flink实现粒子群算法

取最大值(最小值)简介代码

如何分组聚合最小值/最大值并绘制分组条形图