我需要将多个JSON文件中的数据加载到Postgres表中,每个JSON文件中都有多个记录。我使用以下代码,但它不起作用(我在Windows上使用pgAdmin III)
COPY tbl_staging_eventlog1 ("EId", "Category", "Mac", "Path", "ID")
from 'C:\\SAMPLE.JSON'
delimiter ','
;
SAMPLE.JSON文件的内容是这样的(给出了许多这样的记录):
[{"EId":"104111","Category":"(0)","Mac":"ABV","Path":"C:\\Program Files (x86)\\Google","ID":"System.Byte[]"},{"EId":"104110","Category":"(0)","Mac":"BVC","Path":"C:\\Program Files (x86)\\Google","ID":"System.Byte[]"}]
答案 0 :(得分:22)
试试这个:
-- let's create a temp table to bulk data into
create temporary table temp_json (values text) on commit drop;
copy temp_json from 'C:\SAMPLE.JSON';
-- uncomment the line above to insert records into your table
-- insert into tbl_staging_eventlog1 ("EId", "Category", "Mac", "Path", "ID")
select values->>'EId' as EId,
values->>'Category' as Category,
values->>'Mac' as Mac,
values->>'Path' as Path,
values->>'ID' as ID
from (
select json_array_elements(replace(values,'\','\\')::json) as values
from temp_json
) a;
答案 1 :(得分:0)
如Andrew Dunstan's PostgreSQL and Technical blog
中所述在文本模式下,由于JSON中存在反斜杠,因此COPY将被简单击败。因此,例如,任何包含嵌入式双引号或嵌入式换行符的字段,或根据JSON规范需要转义的其他任何字段,都会导致失败。在文本模式下,您几乎无法控制其工作方式-例如,您不能指定其他ESCAPE字符。所以文本模式根本行不通。
所以我们必须转到CSV
格式模式。
copy the_table(jsonfield)
from '/path/to/jsondata'
csv quote e'\x01' delimiter e'\x02';
在官方文档sql-copy中,一些参数在此处列出:
COPY table_name [ ( column_name [, ...] ) ]
FROM { 'filename' | PROGRAM 'command' | STDIN }
[ [ WITH ] ( option [, ...] ) ]
[ WHERE condition ]
where option can be one of:
FORMAT format_name
FREEZE [ boolean ]
DELIMITER 'delimiter_character'
NULL 'null_string'
HEADER [ boolean ]
QUOTE 'quote_character'
ESCAPE 'escape_character'
FORCE_QUOTE { ( column_name [, ...] ) | * }
FORCE_NOT_NULL ( column_name [, ...] )
FORCE_NULL ( column_name [, ...] )
ENCODING 'encoding_name'