将 json 数据加载到舞台时如何解决错误 "Field delimiter ',' found while expecting record delimiter '\n'"

How to solve error "Field delimiter ',' found while expecting record delimiter '\n'" while loading json data to the stage

我正在尝试使用“COPY INTO”命令将数据从 s3 加载到雪花

以下是我创建舞台并将文件从舞台加载到 Snowflake 所遵循的步骤

JSON 文件

{
   "Name":"Umesh",
   "Desigantion":"Product Manager",
   "Location":"United Kingdom"
}
create or replace stage emp_json_stage
url='s3://mybucket/emp.json'
credentials=(aws_key_id='my id' aws_secret_key='my key');

# create the table with variant
CREATE TABLE emp_json_raw (
  json_data_raw VARIANT
);

#load data from stage to snowflake

COPY INTO emp_json_raw from @emp_json_stage;

我低于错误

Field delimiter ',' found while expecting record delimiter '\n' File 'emp.json', line 2, character 18 Row 2, column "emp_json_raw"["JSON_DATA_RAW":1]

我正在使用一个简单的 JSON 文件,我不明白这个错误。

是什么原因造成的,我该如何解决?

尝试:

file_format = (type = csv field_optionally_enclosed_by='"')

默认设置不希望 " 环绕您的数据。 所以你可以去掉所有的“或者...只是将 field_optionally_enclosed_by 设置为一个”。这确实意味着如果您的数据中有 ",事情就会变得混乱。

https://docs.snowflake.com/en/user-guide/getting-started-tutorial-copy-into.html

https://docs.snowflake.com/en/sql-reference/sql/create-file-format.html#type-csv

未指定文件格式,默认为 CSV 格式,因此出现错误。

试试这个:

COPY INTO emp_json_raw 
from @emp_json_stage
file_format=(TYPE=JSON);

除了 TYPE 之外,还有其他选项可以用 file_format 指定。请参阅此处的文档:https://docs.snowflake.com/en/sql-reference/sql/copy-into-table.html#type-json

还有一个标准做法是提及文件类型,它可以是 CSV、JSON、AVRO、Parquet 等。

https://docs.snowflake.com/en/sql-reference/sql/create-file-format.html