简体   繁体   English

使用Scala在Apache Spark中连接不同RDD的数据集

[英]Concatenating datasets of different RDDs in Apache spark using scala

Is there a way to concatenate datasets of two different RDD s in spark? 有没有办法在Spark中串联两个不同RDD的数据集?

Requirement is - I create two intermediate RDDs using scala which has same column names, need to combine these results of both the RDDs and cache the result for accessing to UI. 要求是-我使用具有相同列名的scala创建两个中间RDD,需要将两个RDD的这些结果组合在一起并缓存该结果以访问UI。 How do I combine the datasets here? 如何在此处合并数据集?

RDDs are of type spark.sql.SchemaRDD RDD的类型为spark.sql.SchemaRDD

I think you are looking for RDD.union 我认为您正在寻找RDD.union

val rddPart1 = ???
val rddPart2 = ???
val rddAll = rddPart1.union(rddPart2)

Example (on Spark-shell) 示例(在Spark-shell上)

val rdd1 = sc.parallelize(Seq((1, "Aug", 30),(1, "Sep", 31),(2, "Aug", 15),(2, "Sep", 10)))
val rdd2 = sc.parallelize(Seq((1, "Oct", 10),(1, "Nov", 12),(2, "Oct", 5),(2, "Nov", 15)))
rdd1.union(rdd2).collect

res0: Array[(Int, String, Int)] = Array((1,Aug,30), (1,Sep,31), (2,Aug,15), (2,Sep,10), (1,Oct,10), (1,Nov,12), (2,Oct,5), (2,Nov,15))

I had the same problem. 我有同样的问题。 To combine by row instead of column use unionAll: 要按行而不是列进行合并,请使用unionAll:

val rddPart1= ???
val rddPart2= ???
val rddAll = rddPart1.unionAll(rddPart2)

I found it after reading the method summary for data frame. 我在阅读数据框的方法摘要后找到了它。 More information at: https://spark.apache.org/docs/latest/api/java/org/apache/spark/sql/DataFrame.html 有关更多信息, 访问: https : //spark.apache.org/docs/latest/api/java/org/apache/spark/sql/DataFrame.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM