【发布时间】:2020-02-18 21:51:34
【问题描述】:
我有两个数据帧,它们是 large csv 文件,我正在将它们读入 Spark (Scala) 中的数据帧
第一个数据框类似于
key| col1 | col2 |
-------------------
1 | blue | house |
2 | red | earth |
3 | green| earth |
4 | cyan | home |
第二个数据框类似于
key| col1 | col2 | col3
-------------------
1 | blue | house | xyz
2 | cyan | earth | xy
3 | green| mars | xy
我想为不同数据框中的公共键和公共列(键就像主键)获得这样的差异
key| col1 | col2 |
------------------------------------
1 | blue | house |
2 | red --> cyan | earth |
3 | green | home--> mars |
以下是我目前的做法:
//read the files into dataframe
val src_df = read_df(file1)
val tgt_df = read_df(file2)
//truncate dataframe to only contain common keys
val common_src = spark.sql(
"""
select *
from src_df src
where src.key IN(
select tgt.key
from tgt_df tgt
"""
val tgt_common = spark.sql(
"""
select *
from tgt_df tgt
where tgt.key IN(
select src.key
from src_df src
"""
//merge both the dataframes
val joined_df = src_common.join(tgt_common, src_common(key) === tgt_common(key), "inner")
我尝试做这样的事情没有成功
joined_df
.groupby(key)
.apply(some_function(?))
我尝试查看在线发布的现有解决方案。但我无法得到想要的结果。
PS:也希望该解决方案能够针对大数据进行扩展
谢谢
【问题讨论】:
标签: scala apache-spark apache-spark-sql compare