spark dataframe中join與sql很像,都有inner join, left join, right join, full join;
型別說明
inner join
內連線left join
左連線right join
右連線full join
全連線def join(right : dataframe, usingcolumns : seq[string], jointype : string) : dataframe
def join(right : dataframe, joinexprs : column, jointype : string) : dataframe
jointype可以是」inner」、「left」、「right」、「full」分別對應inner join, left join, right join, full join,預設值是」inner」,代表內連線
例子:
a表
idjob1張3
2李四3王武
b表
idjob
parent_id123
12342
3344內連線:內連線查詢操作列出與連線條件匹配的資料行,它使用比較運算子比較被連線列的列值。
df.join(df, seq("city", "state"), "inner").showdf.join(df, seq("city", "state")).show
seq是指連線的字段,這個相當於
selecta.au_fname, a.au_lname, p.pub_name
from authors as a inner
join publishers as
p
on a.city =
p.city
and a.state =
p.state
order
by a.au_lname asc, a.au_fname asc
結果是
1 張三 1 23 1
2 李四 2 34 2
內連線指定列名
df.join(df, $"city
"===$"
city
", "
inner
").show
df.join(df, $
"city
"===$"
city
").show
左聯接:是以左表為基準,將a.stuid = b.stuid的資料進行連線,然後將左表沒有的對應項顯示,右表的列為null
df.join(df, seq("city
", "
state
"), "
left
").show
結果是1 張三 1 23 1
2 李四 2 34 2
3 王武 null null null
Spark DataFrame中的join型別
spark dataframe中join與sql很像,都有inner join,left join,right join,full join 那麼join方法如何實現不同的join型別呢?看其原型 def join right dataframe,usingcolumns seq string jo...
spark dataframe筆記(鏈結彙總)
spark dataframe筆記 dataframe行轉列 spark dataframe筆記 對dataframe一列值保留4位小數 spark dataframe筆記 按照dataframe某一列的數值排序,並增加一列索引 2 降序排 spark dataframe筆記 按照dataframe...
Spark DataFrame關於資料常用操作
sql語法 1.檢視全表資料 dataframe.show 2.檢視部分字段資料 有4種方法 1 dataframe.select 欄位名稱 show 2 dataframe.select 欄位名稱 show 推薦 3 dataframe.select col 欄位名稱 show 4 datafra...