如何按键分组并计算与spark中的键对应的值的出现次数
How to groupByKey and count occurances of values coressponsing to the keys in spark
t: Array[(Int, String)] = Array((24210720,s503), (24210742,s500), (24210742,s500), (24210748,s503))
我有一组键值对。我想按键 reduce/groupby (我不确定要使用哪个),并希望将核心值作为值计数的映射。应该看起来像
24210720 => {s503=>1}, 24210742 => {s500=>2}, 24210748 => {s503=>1}
所以最后我想打印
24210720:s503:1
24210742:s500:2
24210748:s503:1
如果您的目标只是打印每对值的计数,那么您可以简单地这样做:
rdd=sc.parallelize(t);
rdd.map(x=>( x, 1) ).reduceByKey(_+_).map(x => x._1._1+":"+x._1._2+":"+x._2 )
t: Array[(Int, String)] = Array((24210720,s503), (24210742,s500), (24210742,s500), (24210748,s503))
我有一组键值对。我想按键 reduce/groupby (我不确定要使用哪个),并希望将核心值作为值计数的映射。应该看起来像
24210720 => {s503=>1}, 24210742 => {s500=>2}, 24210748 => {s503=>1}
所以最后我想打印
24210720:s503:1
24210742:s500:2
24210748:s503:1
如果您的目标只是打印每对值的计数,那么您可以简单地这样做:
rdd=sc.parallelize(t);
rdd.map(x=>( x, 1) ).reduceByKey(_+_).map(x => x._1._1+":"+x._1._2+":"+x._2 )