[英]Spark - Scala calculation
我想使用Spark和Scala从csv文件中使用数据的格式为研究人员( https://en.wikipedia.org/wiki/H-index )计算h-ndex
R1:B,R1:A,R1:B,R2:C,R2:B,R2:A,R1:D,R1:B,R1:D,R2:B,R1:A,R1:B
h指数是研究者的学术指标,它的计算方法是为所有食肉动物创建一个单子列表,并对其出版物进行排序,例如R1:{A:10,B:5,C:1},然后找到该指数。值大于其索引的最后位置(这里是位置2,因为1 <3)。
我找不到使用Scala的Spark解决方案。 有人可以帮忙吗?
如果您有这样的文件:
R1:B, R1:A, R1:B, R2:C, R2:B, R2:A, R1:D, R1:B, R1:D, R2:B, R1:A, R1:B
R1:B, R1:A, R1:B, R2:C, R2:B, R2:A, R1:D, R1:B, R1:D, R2:B, R1:A, R1:B
R1:B, R1:A, R1:B, R2:C, R2:B, R2:A, R1:D, R1:B, R1:D, R2:B, R1:A, R1:B
这里有一些想法:
// add a count field to each researcher:paper pair
input.flatMap(line => line.split(", ").map(_ -> 1)).
// count with research:paper as the key
reduceByKey(_+_).map{ case (ra, count) => {
// split research:paper
val Array(author, article) = ra.split(":")
// map so that the researcher will be new key
author -> (article, count)
// group result by the researcher
}}.groupByKey.collect
// res15: Array[(String, Iterable[(String, Int)])] = Array((R2,CompactBuffer((B,6), (A,3), (C,3))), (R1,CompactBuffer((A,6), (B,12), (D,6))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.