合并两个数据帧pyspark

时间:2017-01-26 15:45:31

标签: python pyspark pyspark-sql

我有2个输入文件:

a)原始文件(orig_file.json),包含如下记录:

{"id": 1, "app": test_a, "description": test_app_a }
{"id": 2, "app": test_b, "description": test_app_b }
{"id": 3, "app": test_c, "description": test_app_c }
{"id": 4, "app": test_d, "description": test_app_d }
{"id": 5, "app": test_e, "description": test_app_e }

b)'deltas'文​​件(deltas_file.json),包含如下记录:

{"id": 1, "app": test_aaaxxx, "description": test_app_aaaxxx }
{"id": 6, "app": test_ffffff, "description": test_app_ffffff }

我正在尝试合并两个文件(原始+增量),以便像这样的输出结果

{"id": 1, "app": test_aaaxxx, "description": test_app_aaaxxx }
{"id": 2, "app": test_b, "description": test_app_b }
{"id": 3, "app": test_c, "description": test_app_c }
{"id": 4, "app": test_d, "description": test_app_d }
{"id": 5, "app": test_e, "description": test_app_e }
{"id": 6, "app": test_ffffff, "description": test_app_ffffff }

*基本上通过添加任何新应用程序将原始文件与增量文件合并,只更新已存在的文件的记录。

我尝试使用不同的连接但无法获得解决方案。

有人可以指导我解决这个问题的方法吗? 感谢

2 个答案:

答案 0 :(得分:0)

左外连接和合并:

from pyspark.sql.functions import *


deltas.join(origin, ["id"], "leftouter") \
  .select("id", 
      coalesce(deltas["app"], origin["app"]).alias("app"),
      coalesce(deltas["description"], origin["description"]).alias("description"))

答案 1 :(得分:-1)

尝试python panda merge。

rake db:rollback

希望这有帮助!

相关问题