Spark SQL中Dataframe join操作含null值的列
阿新 • • 發佈:2018-03-16
dataframe util pre table log n-n dram blog between
當在Spark SQL中對兩個Dataframe使用join時,當作為連接的字段的值含有null值。由於null表示的含義是未知,既不知道有沒有,在SQL中null值與任何其他值的比較(即使是null)永遠不會為真。故在進行連接操作時null == null不為True,所以結果中不會出現該條記錄,即左側表格的這條記錄對應右側的值均為null。示例如下:
table_a:
date | serverId | lvSection |
---|---|---|
2018-03-04 | 1 | 10 |
2018-03-05 | null | 9 |
2018-03-07 | 5 | 15 |
table_b:
date | serverId | num |
---|---|---|
2018-03-04 | 1 | 13 |
2018-03-05 | null | 4 |
2018-03-07 | 5 | 6 |
Dataset<Row> table_c = table_a.join(table_b, ScalaUtils.getScalaSeq(Lists.newArrayList("date","serverId")), "left")
table_c:
date | serverId | lvSection | num |
---|---|---|---|
2018-03-04 | 1 | 10 | 13 |
2018-03-05 | null | 9 | null |
2018-03-07 | 5 | 15 | 6 |
所以在使用join時應註意作為連接的字段出現null值的情況。
參考資料:
[1].Difference between === null and isNull in Spark DataDrame
Spark SQL中Dataframe join操作含null值的列