多个 RDD 的 Spark 联合
Spark union of multiple RDDs
在我的 pig 代码中我这样做了:
all_combined = Union relation1, relation2,
relation3, relation4, relation5, relation 6.
我想对 spark 做同样的事情。但是,不幸的是,我发现我必须成对地继续这样做:
first = rdd1.union(rdd2)
second = first.union(rdd3)
third = second.union(rdd4)
# .... and so on
是否有联合运算符可以让我一次对多个 rdds 进行运算:
例如union(rdd1, rdd2,rdd3, rdd4, rdd5, rdd6)
这是一个方便的问题。
不幸的是,这是在 Spark 中 UNION
表的唯一途径。然而,而不是
first = rdd1.union(rdd2)
second = first.union(rdd3)
third = second.union(rdd4)
...
您可以像这样以更简洁的方式执行它:
result = rdd1.union(rdd2).union(rdd3).union(rdd4)
如果这些是 RDD,您可以使用 SparkContext.union
方法:
rdd1 = sc.parallelize([1, 2, 3])
rdd2 = sc.parallelize([4, 5, 6])
rdd3 = sc.parallelize([7, 8, 9])
rdd = sc.union([rdd1, rdd2, rdd3])
rdd.collect()
## [1, 2, 3, 4, 5, 6, 7, 8, 9]
没有 DataFrame
等价物,但这只是一个简单的单行问题:
from functools import reduce # For Python 3.x
from pyspark.sql import DataFrame
def unionAll(*dfs):
return reduce(DataFrame.unionAll, dfs)
df1 = sqlContext.createDataFrame([(1, "foo1"), (2, "bar1")], ("k", "v"))
df2 = sqlContext.createDataFrame([(3, "foo2"), (4, "bar2")], ("k", "v"))
df3 = sqlContext.createDataFrame([(5, "foo3"), (6, "bar3")], ("k", "v"))
unionAll(df1, df2, df3).show()
## +---+----+
## | k| v|
## +---+----+
## | 1|foo1|
## | 2|bar1|
## | 3|foo2|
## | 4|bar2|
## | 5|foo3|
## | 6|bar3|
## +---+----+
如果 DataFrames
的数量很大,在 RDD 上使用 SparkContext.union
并重新创建 DataFrame
可能是避免 issues related to the cost of preparing an execution plan:
的更好选择
def unionAll(*dfs):
first, *_ = dfs # Python 3.x, for 2.x you'll have to unpack manually
return first.sql_ctx.createDataFrame(
first.sql_ctx._sc.union([df.rdd for df in dfs]),
first.schema
)
你也可以在 RDD 之间使用 UNION 加法
rdd = sc.parallelize([1, 1, 2, 3])
(rdd + rdd).collect()
## [1, 1, 2, 3, 1, 1, 2, 3]
在我的 pig 代码中我这样做了:
all_combined = Union relation1, relation2,
relation3, relation4, relation5, relation 6.
我想对 spark 做同样的事情。但是,不幸的是,我发现我必须成对地继续这样做:
first = rdd1.union(rdd2)
second = first.union(rdd3)
third = second.union(rdd4)
# .... and so on
是否有联合运算符可以让我一次对多个 rdds 进行运算:
例如union(rdd1, rdd2,rdd3, rdd4, rdd5, rdd6)
这是一个方便的问题。
不幸的是,这是在 Spark 中 UNION
表的唯一途径。然而,而不是
first = rdd1.union(rdd2)
second = first.union(rdd3)
third = second.union(rdd4)
...
您可以像这样以更简洁的方式执行它:
result = rdd1.union(rdd2).union(rdd3).union(rdd4)
如果这些是 RDD,您可以使用 SparkContext.union
方法:
rdd1 = sc.parallelize([1, 2, 3])
rdd2 = sc.parallelize([4, 5, 6])
rdd3 = sc.parallelize([7, 8, 9])
rdd = sc.union([rdd1, rdd2, rdd3])
rdd.collect()
## [1, 2, 3, 4, 5, 6, 7, 8, 9]
没有 DataFrame
等价物,但这只是一个简单的单行问题:
from functools import reduce # For Python 3.x
from pyspark.sql import DataFrame
def unionAll(*dfs):
return reduce(DataFrame.unionAll, dfs)
df1 = sqlContext.createDataFrame([(1, "foo1"), (2, "bar1")], ("k", "v"))
df2 = sqlContext.createDataFrame([(3, "foo2"), (4, "bar2")], ("k", "v"))
df3 = sqlContext.createDataFrame([(5, "foo3"), (6, "bar3")], ("k", "v"))
unionAll(df1, df2, df3).show()
## +---+----+
## | k| v|
## +---+----+
## | 1|foo1|
## | 2|bar1|
## | 3|foo2|
## | 4|bar2|
## | 5|foo3|
## | 6|bar3|
## +---+----+
如果 DataFrames
的数量很大,在 RDD 上使用 SparkContext.union
并重新创建 DataFrame
可能是避免 issues related to the cost of preparing an execution plan:
def unionAll(*dfs):
first, *_ = dfs # Python 3.x, for 2.x you'll have to unpack manually
return first.sql_ctx.createDataFrame(
first.sql_ctx._sc.union([df.rdd for df in dfs]),
first.schema
)
你也可以在 RDD 之间使用 UNION 加法
rdd = sc.parallelize([1, 1, 2, 3])
(rdd + rdd).collect()
## [1, 1, 2, 3, 1, 1, 2, 3]