2016-12-17 32 views
3

我正在学习Spark + Scala,并且我被这个问题困住了。我有两件事。包含许多字一个文件(我告诉你,例如2个第一行):斯卡拉Spark rdd组合在一个文件中匹配对

scala> val text = sc.textFile("hdfs://master:9000/data/words.txt") 
text: org.apache.spark.rdd.RDD[String] = hdfs://master:9000/data/words.txt MapPartitionsRDD[1] at textFile at <console>:24 

scala> text.take(2) 
res0: Array[String] = Array("a b c d ", "e r t y u i o p ") 

而且我有一个“组合”变量代表对频繁出现的词汇(我告诉你,例如2第一行):

scala> val combinations = l.cartesian(l).filter{case(x,y) => x < y} 
combinations: org.apache.spark.rdd.RDD[(String, String)] = MapPartitionsRDD[9] at filter at <console>:32 

scala> combinations.take(2) 
res1: Array[(String, String)] = Array((a,b), (a,c)) 

我想从“文本” varable的每一行创造组合对,以与“组合”变量对匹配它们。

例如,在输入文本的第一行,我想有:

(a,b) (a,c) (a,d) (b,c) (b,d) (c,d) 

,这样我可以用“组合”变量相匹配他们

请帮帮忙!这真让我抓狂。谢谢,

回答

4

如果我理解正确,我们希望得到每个条目text中包含的字母的组合。例如:

"a b c d " => (a,b) (a,c) (a,d) (b,c) (b,d) (c,d) 

我们可以使用Scala集合API做到这一点:

val textCombinations = text.map(t => t.split(" ").combinations(2).toList) 
+0

你好maasg,你的代码似乎是好的,但我有: – Bebec

+0

斯卡拉> textCombinations.take(10).foreach (println) 16/12/17 11:45:42 ERROR executor.Executor:阶段5.0(TID 5)中任务0.0中的异常 java.io.NotSerializableException:scala.collection.SeqLike $ CombinationsItr 序列化堆栈:.. 。 – Bebec

+0

@Bebec我明白了。使它成为'.combinations(2).toList'而不是'.toSeq'。看起来'toSeq'实现默认为不可序列化的'Stream'。我也更新了答案。 – maasg