合并两个数据框pyspark

Merging two dataframes pyspark

我有 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 }

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

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

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

尝试 python 熊猫合并。

import panda as pd
# create your data frames here
pd.merge(delta_frame,orig_frame)  # Try various required arguments in function

希望对您有所帮助!

左外连接和合并:

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"))