apache-spark - 如何在Spark SQL中进行左外部联接?

标签 apache-spark pyspark apache-spark-sql

我正在尝试在spark(1.6.2)中进行左外部连接,但它不起作用。我的SQL查询是这样的:

sqlContext.sql("select t.type, t.uuid, p.uuid
from symptom_type t LEFT JOIN plugin p 
ON t.uuid = p.uuid 
where t.created_year = 2016 
and p.created_year = 2016").show()

结果是这样的:
+--------------------+--------------------+--------------------+
|                type|                uuid|                uuid|
+--------------------+--------------------+--------------------+
|              tained|89759dcc-50c0-490...|89759dcc-50c0-490...|
|             swapper|740cd0d4-53ee-438...|740cd0d4-53ee-438...|

使用LEFT JOIN或LEFT OUTER JOIN(第二个uuid不为null),我得到相同的结果。

我希望第二个uuid列只能为null。如何正确进行左外连接?

===其他信息==

如果我使用数据框执行左外部连接,则结果正确。
s = sqlCtx.sql('select * from symptom_type where created_year = 2016')
p = sqlCtx.sql('select * from plugin where created_year = 2016')

s.join(p, s.uuid == p.uuid, 'left_outer')
.select(s.type, s.uuid.alias('s_uuid'), 
        p.uuid.alias('p_uuid'), s.created_date, p.created_year, p.created_month).show()

我得到这样的结果:
+-------------------+--------------------+-----------------+--------------------+------------+-------------+
|               type|              s_uuid|           p_uuid|        created_date|created_year|created_month|
+-------------------+--------------------+-----------------+--------------------+------------+-------------+
|             tained|6d688688-96a4-341...|             null|2016-01-28 00:27:...|        null|         null|
|             tained|6d688688-96a4-341...|             null|2016-01-28 00:27:...|        null|         null|
|             tained|6d688688-96a4-341...|             null|2016-01-28 00:27:...|        null|         null|

谢谢,

最佳答案

我看不到您的代码中的任何问题。 “左连接”或“左外部连接”都可以正常工作。请再次检查数据,显示的数据是否匹配。

您还可以使用以下命令执行Spark SQL连接:

//显式的左外部联接

df1.join(df2, df1["col1"] == df2["col1"], "left_outer")

关于apache-spark - 如何在Spark SQL中进行左外部联接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40421356/

相关文章:

scala - 迭代 DataFrame 时更新列

scala - 如何使用 scala 比较 Spark 中的一行与所有其他行

python - 混合模式 CSV 导入 Pyspark

scala - 向 Spark DataFrame 添加嵌套列

python - Spark DataFrame 的通用 "reduceBy"或 "groupBy + aggregate"功能

python - 需要 RDD 实例但返回类 'pyspark.rdd.PipelinedRDD'

python - 如何在 pyspark.sql.functions.when() 中使用多个条件?

python - 使用pyspark中的函数进行行操作

python - PySpark 中的列过滤

scala - 无法在简单示例上从 spark ML 运行 RandomForestClassifier