Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache spark 如何使用columns连接spark中的嵌套列_Apache Spark_Join_Apache Spark Sql - Fatal编程技术网

Apache spark 如何使用columns连接spark中的嵌套列

Apache spark 如何使用columns连接spark中的嵌套列,apache-spark,join,apache-spark-sql,Apache Spark,Join,Apache Spark Sql,我有两个数据帧,我想加入 DF1: DF2: 我的join语句是 df1.join(df2, Seq("id", "region"), "leftouter") 但这对我们来说是失败的 USING column `id` cannot be resolved on the left side of the join. The left-side columns: myStruct, first_name 我在Scala上运行Spark 2.2,这是因为在DF1中,id是struct类型的my

我有两个数据帧,我想加入

DF1:

DF2:

我的join语句是

df1.join(df2, Seq("id", "region"), "leftouter")
但这对我们来说是失败的

USING column `id` cannot be resolved on the left side of the join. The left-side columns: myStruct, first_name
我在Scala上运行Spark 2.2,这是因为在DF1中,id是struct类型的myStruct列的一个元素。为了加入,你可以做一些事情,比如

val df = df1
.withColumn("id", col("myStruct.id"))
.withColumn("region", col("myStruct.region"))

df.join(df2, Seq("id", "region"), "leftouter")
这实际上是从struct列中提取id和region。

您可以使用。从结构列中选择元素的符号。因此,要从df1中选择id,您必须执行myStruct.id,要选择region,您必须使用myStruct.region

由于要使用的列名不同,您可以使用===符号进行比较

df1.join(df2, df1("myStruct.id") === df2("id") && df1("myStruct.region") === df2("region"), "leftouter")
您应该具有具有以下架构的联接数据帧

可以在联接后删除不必要的列,也可以在联接后仅选择所需的列

我希望答案是有帮助的

val df = df1
.withColumn("id", col("myStruct.id"))
.withColumn("region", col("myStruct.region"))

df.join(df2, Seq("id", "region"), "leftouter")
df1.join(df2, df1("myStruct.id") === df2("id") && df1("myStruct.region") === df2("region"), "leftouter")
root
 |-- myStruct: struct (nullable = true)
 |    |-- id: string (nullable = true)
 |    |-- region: long (nullable = false)
 |-- first_name: string (nullable = true)
 |-- id: string (nullable = true)
 |-- region: integer (nullable = true)
 |-- second_name: string (nullable = true)