Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flink Streaming Python API-reduce()生成增量结果,而不是最终值_Python_Apache Flink_Flink Streaming - Fatal编程技术网

Flink Streaming Python API-reduce()生成增量结果,而不是最终值

Flink Streaming Python API-reduce()生成增量结果,而不是最终值,python,apache-flink,flink-streaming,Python,Apache Flink,Flink Streaming,我正试图在Flink上使用PythonAPI实现Kmeans聚类算法。我正在根据第0个索引执行一个key\u by,然后尝试对每个组执行reduce(),以获得某种计数聚合 类质心累加器(减速功能): def减少(自身、val1、val2): id1,point1,count1=val1 id2,点2,count2=val2 返回(id1,point1.add(point2),count1+count2) 类选择器(键选择器): def getKey(自身,值): 返回值[0] 最近的点=点 .

我正试图在Flink上使用PythonAPI实现Kmeans聚类算法。我正在根据第0个索引执行一个
key\u by
,然后尝试对每个组执行
reduce()
,以获得某种计数聚合

类质心累加器(减速功能):
def减少(自身、val1、val2):
id1,point1,count1=val1
id2,点2,count2=val2
返回(id1,point1.add(point2),count1+count2)
类选择器(键选择器):
def getKey(自身,值):
返回值[0]
最近的点=点
.map(选择最近点(质心))\
.key_by(选择器()).reduce(质心累加器())
最近的_点。将_写为_文本(“output.txt”)
预期结果:

(1, <tuple>, count)
(2, <tuple>, count)
(3, <tuple>, count)
(4, <tuple>, count)
(1,计数)
(2,计数)
(3,伯爵)
(4,计数)
实际结果:

我得到写入文件的所有迭代的输出(我测试的样本中有40个点,因此输出有40行,如下所示)

(1,1)
(3, 1)                                                                                                                                
(2, 1)                                                                                                                                
(2, 2)                                                                                                                                
.
.
.                                                                                                                
(2, 13)                                                                                                                              
(2, 14)                                                                                                                              
(1, 10)                                                                                                                              
(4, 4)                                                                                                                               
(2, 15)                                                                                                                              
(2, 16)                                                                                                                              
(1, 11)                                                                                                                              
(4, 5)                                                                                                                               
(2, 17)                                                                                                                              
(2, 18) 

问题是它正在还原,但我只想得到每个组的reduce转换的最后一个值(据我所知,reduce应该如何工作)。我做错了什么?

你没有做错什么;这是streaming reduce函数的预期行为。从概念上讲,数据流是无止境的数据流,因此“等到最后”生成结果是没有意义的。流媒体节目的标准行为是为每个事件生成结果


当然,这可能有点不方便。如果你只想看到最后的结果,那么必须有某种方式来表明结束已经到来。对于批处理程序,这是很自然的。对于流式应用程序,有限的数据源发送一个带有MAX_水印值的水印,该值可用于检测输入是否已到达其终点——您可以在带有事件时间计时器的ProcessFunction中捕捉到这一点,但这是一个有点复杂的解决方案。您还可以使用windows实现某种解决方法。

有意义。使用
.key\u by(Selector()).time\u window(毫秒(50)).reduce(centroidacculator())
在ProcessFunction解决方案中使用捕捉功能,我有点力不从心。
(1, <kmeans_clustering.Point instance at 0x2>, 1)                                                                                                                                
(3, <kmeans_clustering.Point instance at 0x3>, 1)                                                                                                                                
(2, <kmeans_clustering.Point instance at 0x4>, 1)                                                                                                                                
(2, <kmeans_clustering.Point instance at 0x5>, 2)                                                                                                                                
.
.
.                                                                                                                
(2, <kmeans_clustering.Point instance at 0x20>, 13)                                                                                                                              
(2, <kmeans_clustering.Point instance at 0x21>, 14)                                                                                                                              
(1, <kmeans_clustering.Point instance at 0x22>, 10)                                                                                                                              
(4, <kmeans_clustering.Point instance at 0x23>, 4)                                                                                                                               
(2, <kmeans_clustering.Point instance at 0x24>, 15)                                                                                                                              
(2, <kmeans_clustering.Point instance at 0x25>, 16)                                                                                                                              
(1, <kmeans_clustering.Point instance at 0x26>, 11)                                                                                                                              
(4, <kmeans_clustering.Point instance at 0x27>, 5)                                                                                                                               
(2, <kmeans_clustering.Point instance at 0x28>, 17)                                                                                                                              
(2, <kmeans_clustering.Point instance at 0x29>, 18)