【发布时间】:2016-10-12 04:25:35
【问题描述】:
我正在尝试使用 DataFrame Scala API 执行 selfjoin。 这是我的代码sn-ps; 你能告诉我第一个解决方案有什么问题吗?
val df=sqlc.read.json("empMgr.json");
empMgr.json
{"ID":101,"ename":"Peter","sal":24.24,"dept":"11","country":"US","doj":"1/12/2017 ","经理":201} {"ID":201,"ename":"John","sal":1300,"dept":"232","country":"IN","doj":"4/22/2016","经理":111} {"ID":301,"ename":"Sam","dept":"22","country":"KR","doj":"5/22/2015","mgr":201}
// 1. following is not working
var df_right=df;
df.join(df_right, df("mgr") === df_right("ID")).show()
df.join(df, df("mgr") === df("ID")).show()
/*
* Output:
* +---+-------+----+---+-----+---+---+---+-------+----+---+-----+---+---+
| ID|country|dept|doj|ename|mgr|sal| ID|country|dept|doj|ename|mgr|sal|
+---+-------+----+---+-----+---+---+---+-------+----+---+-----+---+---+
+---+-------+----+---+-----+---+---+---+-------+----+---+-----+---+---+
* */
//2. following works fine
df_right= sqlc.read.json("file:///opt/data/empMgr.json");
df.join(df_right, df("mgr") === df_right("ID")).show()
/*
*Output:
* +---+-------+----+---------+-----+---+-----+---+-------+----+---------+-----+---+------+
| ID|country|dept| doj|ename|mgr| sal| ID|country|dept| doj|ename|mgr| sal|
+---+-------+----+---------+-----+---+-----+---+-------+----+---------+-----+---+------+
|101| US| 11|1/12/2017|Peter|201|24.24|201| IN| 232|4/22/2016| John|111|1300.0|
|301| KR| 22|5/22/2015| Sam|201| null|201| IN| 232|4/22/2016| John|111|1300.0|
+---+-------+----+---------+-----+---+-----+---+-------+----+---------+-----+---+------+
* */
//3. following works fine
df.registerTempTable("empMgr")
sqlc.sql("select b.ename, a.ename as mgr,b.mgr from empMgr a join empMgr b on a.ID=b.mgr").show();
/*
* output
* +-----+----+---+
|ename| mgr|mgr|
+-----+----+---+
|Peter|John|201|
| Sam|John|201|
+-----+----+---+
* */
【问题讨论】:
-
您的问题是什么?我是不是弄错了,或者第 1 点中有一条不应该存在的额外线?请澄清。
标签: scala apache-spark spark-dataframe