pojoResult.windowAll(TumblingProcessingTimeWindows.of(Time.seconds(1)))
.process(new WindowAllResultProcess)
class WindowAllResultProcess extends ProcessAllWindowFunction[pojo, Object,TimeWindow] {
override def process(context: Context, elements: Iterable[pojo], out: Collector[Object]): Unit = {
val queue = new PriorityQueue[pojo](3, new Comparator[pojo] {
override def compare(o1: pojo, o2: pojo): Int = {
if (o1.price >= o2.price) {
1
} else -1
}
})
var totalPrice:Double = 0.0
val iterator = elements.iterator
while (iterator.hasNext) {
val pojo1 = iterator.next()
if (queue.size() < 3) {
queue.add(pojo1)
} else {
//计算topN的时候需要小顶堆,也就是要去掉堆顶比较小的元素
//取出堆顶元素
val tmp = queue.peek()
//堆顶元素的价格小于新元素 删除堆顶元素 加入新的
if(tmp.price < pojo1.price){
queue.poll() //移出
queue.add(pojo1)
}
}
Flink求topKey问题
最新推荐文章于 2023-03-28 18:38:06 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)