簡體   English   中英

Spark-Scala計算

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM