使用 Seq("key") 语法的 Spark DataFrames 的左外部复杂连接
Left outer Complex Join of Spark DataFrames using Seq("key") syntax
我需要使用数据帧转换以下 sql 连接。问题是我得到了重复的 "key" 列
val result_sql = sparkSession.sql(" select * from TAB_A a left outer join TAB_B b on a.key = b.key AND a.e_date between b.start_date and b.end_date ")
result_sql.printSchema()
root
|-- key: string (nullable = true)
|-- key: string (nullable = true)
|-- VAL: double (nullable = true)
所以我已经尝试过了,但是得到了相同的重复列 "key"
val result_df = TAB_A.join(TAB_B,TAB_A.col("key") === TAB_B.col("key")
&& TAB_A.col("e_date").between(TAB_B.col("start_date"),TAB_B.col("start_date")),
"left_outer")
root
|-- key: string (nullable = true)
|-- key: string (nullable = true)
|-- VAL: double (nullable = true)
然后我尝试使用 Seq ,但无法实现复杂的连接并面临错误
val result_df = TAB_A.join(TAB_B,Seq("key") && TAB_A.col("e_date").between(TAB_B.col("start_date"),TAB_B.col("start_date")),
"left_outer")
预期架构:
root
|-- key: string (nullable = true)
|-- VAL: double (nullable = true)
在没有重复列的情况下实现上述逻辑的任何最佳解决方案。
注意:我正在寻找使用 spark 数据帧而不是 spark_sql 查询的解决方案。
SQL 的问题是,结果有两个列(键)来自两个连接 tables。
解决方案 #1 为键指定不同的名称。
例如设置左边的列名table为k1
设置右边table的列名为k2
解决方案 #2 指定要保留在结果中的列 table
SELECT a.*, b.val1, b.val2
FROM TAB_A a left outer join TAB_B b on a.key = b.key AND a.e_date between b.start_date and b.end_date
// Since you you only want to keep one key, please change the code you have
val result_df = TAB_A.join(TAB_B,TAB_A.col("key") === TAB_B.col("key")
&& TAB_A.col("e_date").between(TAB_B.col("start_date"),TAB_B.col("start_date")),
"left_outer")
// drop the key from TAB_B or TAB_A
val result_df = TAB_A.join(TAB_B,TAB_A.col("key") === TAB_B.col("key")
&& TAB_A.col("e_date").between(TAB_B.col("start_date"),TAB_B.col("start_date")),
"left_outer").drop(TAB_B("key"))
我需要使用数据帧转换以下 sql 连接。问题是我得到了重复的 "key" 列
val result_sql = sparkSession.sql(" select * from TAB_A a left outer join TAB_B b on a.key = b.key AND a.e_date between b.start_date and b.end_date ")
result_sql.printSchema()
root
|-- key: string (nullable = true)
|-- key: string (nullable = true)
|-- VAL: double (nullable = true)
所以我已经尝试过了,但是得到了相同的重复列 "key"
val result_df = TAB_A.join(TAB_B,TAB_A.col("key") === TAB_B.col("key")
&& TAB_A.col("e_date").between(TAB_B.col("start_date"),TAB_B.col("start_date")),
"left_outer")
root
|-- key: string (nullable = true)
|-- key: string (nullable = true)
|-- VAL: double (nullable = true)
然后我尝试使用 Seq ,但无法实现复杂的连接并面临错误
val result_df = TAB_A.join(TAB_B,Seq("key") && TAB_A.col("e_date").between(TAB_B.col("start_date"),TAB_B.col("start_date")),
"left_outer")
预期架构:
root
|-- key: string (nullable = true)
|-- VAL: double (nullable = true)
在没有重复列的情况下实现上述逻辑的任何最佳解决方案。
注意:我正在寻找使用 spark 数据帧而不是 spark_sql 查询的解决方案。
SQL 的问题是,结果有两个列(键)来自两个连接 tables。
解决方案 #1 为键指定不同的名称。
例如设置左边的列名table为k1
设置右边table的列名为k2
解决方案 #2 指定要保留在结果中的列 table
SELECT a.*, b.val1, b.val2
FROM TAB_A a left outer join TAB_B b on a.key = b.key AND a.e_date between b.start_date and b.end_date
// Since you you only want to keep one key, please change the code you have
val result_df = TAB_A.join(TAB_B,TAB_A.col("key") === TAB_B.col("key")
&& TAB_A.col("e_date").between(TAB_B.col("start_date"),TAB_B.col("start_date")),
"left_outer")
// drop the key from TAB_B or TAB_A
val result_df = TAB_A.join(TAB_B,TAB_A.col("key") === TAB_B.col("key")
&& TAB_A.col("e_date").between(TAB_B.col("start_date"),TAB_B.col("start_date")),
"left_outer").drop(TAB_B("key"))