jay*_*les 6 scala for-comprehension apache-spark rdd
在使用RDD进行扩展时我收到警告,我不确定这是不是我做错了.如果我这样做:
val sc = new SparkContext(...)
val anRDD = sc.parallelize(List(
("a", List(1, 2, 3)),
("b", List(4),
("c", List(5, 6))
)
for {
(someString, listOfInts) <- anRDD
someInt <- listOfInts
} yield (someString, someInt)
Run Code Online (Sandbox Code Playgroud)
然后我得到这个输出:
warning: `withFilter' method does not yet exist on org.apache.spark.rdd.RDD[(String, List[Int])], using `filter' method instead
(s, li) <- rl
Run Code Online (Sandbox Code Playgroud)
但它仍然成功返回FlatMappedRDD [(String,Int)].难道我做错了什么?或者忽略此警告是否安全?
更新:我也会接受for-comprehension如何将这些操作转换为map/flatMap/filter调用,因为我认为不需要任何过滤器或withFilter调用.我认为这相当于类似的东西:
anRDD.flatMap(tuple => tuple.map(someInt => (tuple._1, someInt)))
Run Code Online (Sandbox Code Playgroud)
但这不包括任何过滤器或withFilter调用,这似乎是警告的来源.
哦,我正在使用Spark 1.2.0,Scala 2.10.4,这都在REPL中.
首先,我不是专家,但做了一些挖掘,这是我发现的:
我使用(因为 JavaDecompiler 由于某种原因失败)编译了代码-print
,它将打印出删除了所有 Scala 特定功能的程序。在那里,我看到:
test.this.anRDD().filter({
(new anonymous class anonfun$1(): Function1)
}).flatMap({
(new anonymous class anonfun$2(): Function1)
}, ClassTag.apply(classOf[scala.Tuple2]));
Run Code Online (Sandbox Code Playgroud)
你会注意到filter
......所以,我检查了anonfun$1
:
public final boolean apply(Tuple2<String, List<Object>> check$ifrefutable$1)
{
Tuple2 localTuple2 = check$ifrefutable$1;
boolean bool;
if (localTuple2 != null) {
bool = true;
} else {
bool = false;
}
return bool;
}
Run Code Online (Sandbox Code Playgroud)
所以,如果你把所有这些放在一起,似乎filter
在理解中发生了,因为它过滤掉了任何不是 的东西Tuple2
。
并且,首选是使用withFilter
而不是filter
(不知道为什么是 atm)。您可以通过反编译常规列表而不是反编译列表来看到RDD
object test {
val regList = List(
("a", List(1, 2, 3)),
("b", List(4)),
("c", List(5, 6))
)
val foo = for {
(someString, listOfInts) <- regList
someInt <- listOfInts
} yield (someString, someInt)
}
Run Code Online (Sandbox Code Playgroud)
反编译为:
test.this.regList().withFilter({
(new anonymous class anonfun$1(): Function1)
}).flatMap({
(new anonymous class anonfun$2(): Function1)
}, immutable.this.List.canBuildFrom()).$asInstanceOf[List]();
Run Code Online (Sandbox Code Playgroud)
所以,它是同样的东西,除了它使用withFilter
它可以使用的地方
归档时间: |
|
查看次数: |
1745 次 |
最近记录: |