[英]Scala - Operation in case (x,y)=> x++y
I am new to scala. 我是斯卡拉的新手。 I was reading one code not able to understand .
我正在阅读一个无法理解的代码。 Can someone please help me to understand below code ?
有人可以帮我理解下面的代码吗?
def intersectByKey[K: ClassTag, V: ClassTag](rdd1: RDD[(K, V)], rdd2: RDD[(K, V)]): RDD[(K, V)] = {
rdd1.cogroup(rdd2).flatMapValues{
case (Nil, _) => None
case (_, Nil) => None
case (x, y) => x++y
}
}
What does below line means ? 下线是什么意思? How it will be evaluated ?
如何评估?
case (x, y) => x++y
Thanks 谢谢
rdd1.cogroup(rdd2)
returns a value of type RDD[(K, (Iterable[V], Iterable[V]))]
. rdd1.cogroup(rdd2)
返回RDD[(K, (Iterable[V], Iterable[V]))]
。 So - in case (x, y)
both x
and y
are Iterable[V]
. 所以 - 在
case (x, y)
, x
和y
都是可Iterable[V]
。 Iterable
overloads the ++
operator, with an implementation that simply means union
- returning an iterable with all of x
's values followed by all of y
's values. Iterable
重载++
运算符,其实现只是表示union
- 返回一个带有x
的所有值的迭代,后跟所有y
的值。
The function cogroup
returns a RDD[(K, (Seq[V], Seq[W]))]
. 函数
cogroup
返回RDD[(K, (Seq[V], Seq[W]))]
。
So the value is of type Tuple2
. 所以值是
Tuple2
类型。 When you use flatMapValues
it will flatmap over the values, which are of Type Seq
. 当您使用
flatMapValues
,它将对值进行平面映射,这些值是Seq
类型。 ++
for Seqs mean concatenating them. ++
for Seqs意味着连接它们。 Resulting in a combined Seq. 结果是Seq。
case (x, y)
means that you're using pattern matching. case (x, y)
表示您正在使用模式匹配。 In your case if none of the values of your Tuple are Nil, the function will return x ++ y
. 在你的情况下,如果你的元组的值都不是Nil,函数将返回
x ++ y
。
The advantage of using flatMapValues
in this case is that it will flatten the result, therefore losing all the None
values. 在这种情况下使用
flatMapValues
的优点是它会使结果变平,因此会丢失所有None
值。
You can check out the documentation here. 你可以在这里查看文档。 Also if you're not sure what exactly Pattern matching or flatmaps are check this for pattern matching and this for flatmap
此外,如果您不确定模式匹配或平面图的确切位置,请检查此模式匹配,以及平面图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.