如何使用pyspark将JSON插入雪花变体列

问题描述 投票:0回答:1

我有一个从 API 中提取的 JSON 数据。以下是该数据的示例:

{"Clients" : [{"id" : "123", "name" : "client ABC inc"},
{"id" : "456", "name" : "client XYZ inc"}]}

我想使用 pyspark 数据帧将其完整插入到雪花表的变体列中。我怎样才能这样做呢?我正在寻找架构定义和数据帧写入步骤。预先感谢。

pyspark snowflake-cloud-data-platform
1个回答
0
投票

您可以从 json 创建一个数据框,然后将其附加到现有表中(在下面的示例中,使用

json_example
称为
mode = 'append'
。,此模式会附加到表(如果存在),否则创建一个新表。在下面的示例中credential2 是一个包含所有连接详细信息的 .py 文件。

from snowflake.snowpark import Session
from credentials2 import connection_parameters

session = Session.builder.configs(connection_parameters).create()

df = session.create_dataframe([['{"Clients" : [{"id" : "123", "name" : "client ABC inc"},\
                                {"id" : "456", "name" : "client XYZ inc"}]}']], schema=["json_var"])
df.write.mode('append').save_as_table('json_example')
© www.soinside.com 2019 - 2024. All rights reserved.