[英]Join Row DataSets In Spark Java API
I want two join two DataSets DS1 and DS2 to get DS3 我想要两个联接两个数据集DS1和DS2以获得DS3
DS1 : DS1:
+---------+--------------------+-----------+------------+
|Compte | Lib |ReportDebit|ReportCredit|
+---------+--------------------+-----------+------------+
| 447105|Autres impôts, ta...| 77171.0| 0.0|
| 753000|Jetons de présenc...| 6839.0| 0.0|
| 511107|Valeurs à l’encai...| 0.0| 77171.0|
+---------+--------------------+-----------+------------+
DS2: DS2:
+---------+------------+
|Compte |SoldeBalance|
+---------+------------+
| 447105 | 992.13|
| 111111 | 35065.0|
I want to get DS3 like this: 我想要这样的DS3:
+---------+--------------------+-----------+------------+------------+
|Compte | CompteLib|ReportDebit|ReportCredit|SoldeBalance|
+---------+--------------------+-----------+------------+------------+
| 447105|Autres impôts, ta...| 77171.0| 0.0| 992.13|
| 753000|Jetons de présenc...| 6839.0 | 0.0| 0.0|
| 511107|Valeurs à l’encai...| 0.0| 77171.0| 0.0|
111111| | 0.0| 0.0| 35065.0|
+---------+--------------------+-----------+------------+------------+
Can somebody guide me with a sample Spark Java expression. 有人可以指导我介绍一下示例Spark Java表达式。 Thanks in advance.
提前致谢。
You can achieve this by applying a full outer join and then replacing null values with the desired values. 您可以通过应用完整的外部联接,然后将空值替换为所需的值来实现此目的。
import static org.apache.spark.sql.functions.*;
...
ds1.join(ds2, ds1.col("Compte").equalTo(ds2.col("Compte")), "full_outer")
.select(ds1.col("Compte").alias("Compte1"),
ds2.col("Compte").alias("Compte2"),
ds1.col("Lib"),
ds1.col("ReportDebit"),
ds1.col("ReportCredit"),
ds2.col("SoldeBalance"))
.withColumn("Compte", when(col("Compte1").isNull(), col("Compte2")).otherwise(col("Compte1")))
.drop("Compte1", "Compte2")
.na().fill(0.0, new String[] { "ReportDebit", "ReportCredit", "SoldeBalance" })
.na().fill("", new String[] { "Lib" })
.show();
Output: 输出:
+--------------------+-----------+------------+------------+------+
| Lib|ReportDebit|ReportCredit|SoldeBalance|Compte|
+--------------------+-----------+------------+------------+------+
|Valeurs à l’encai...| 0.0| 77171.0| 0.0|511107|
|Autres impôts, ta...| 77171.0| 0.0| 992.13|447105|
| | 0.0| 0.0| 35065.0|111111|
|Jetons de présenc...| 6839.0| 0.0| 0.0|753000|
+--------------------+-----------+------------+------------+------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.