我想将 json 数据导入 postgres。我拥有的数据大约为一百万行,大小至少为 700 MB,并且一直延伸到 3 GB。
这是我根据我拥有的数据结构创建的示例数据。我尝试将其导入 postgres,但出现错误。
样本(1) 数据
{"offers":{"offer":[
{"url": "https://some1-value.com", "nested": {"id":4,"value":"some1 text value"}, "quotes": "5\" side"},
{"url": "https://some2-value.com", "nested": {"id":5,"value":"some2 text value"}, "quotes": "6\" side"},
{"url": "https://some3-value.com", "nested": {"id":6,"value":"some3 text value"}, "quotes": "7\" side"}]}}
我使用的命令和我得到的错误
# copy contrial from '/home/ubuntu/sample-data.json';
ERROR: invalid input syntax for type json
DETAIL: The input string ended unexpectedly.
CONTEXT: JSON data, line 1: {"offers":{"offer":[
COPY contrial, line 1, column info: "{"offers":{"offer":["
我修改了文件以删除前两个键,并且只有一个如下所示的 json 列表,但我仍然收到错误消息。
样本(2) 数据
[
{"url": "https://some1-value.com", "nested": {"id":4,"value":"some1 text value"}, "quotes": "5\" side"},
{"url": "https://some2-value.com", "nested": {"id":5,"value":"some2 text value"}, "quotes": "6\" side"},
{"url": "https://some3-value.com", "nested": {"id":6,"value":"some3 text value"}, "quotes": "7\" side"}]
错误
# copy contrial from '/home/ubuntu/sample2-data.json';
ERROR: invalid input syntax for type json
DETAIL: The input string ended unexpectedly.
CONTEXT: JSON data, line 1: [
COPY contrial, line 1, column info: "["
Sample(3) 数据我进一步修改
[{"url": "https://some1-value.com", "nested": {"id":4,"value":"some1 text value"}, "quotes": "5\" side"},
{"url": "https://some2-value.com", "nested": {"id":5,"value":"some2 text value"}, "quotes": "6\" side"},
{"url": "https://some3-value.com", "nested": {"id":6,"value":"some3 text value"}, "quotes": "7\" side"}]
不同的错误
# copy contrial from '/home/ubuntu/sample2-data.json';
ERROR: invalid input syntax for type json
DETAIL: Token "side" is invalid.
CONTEXT: JSON data, line 1: ...,"value":"some1 text value"}, "quotes": "5" side...
COPY contrial, line 1, column info: "[{"url": "https://some1-value.com", "nested": {"id":4,"value":"some1 text value"}, "quotes": "5" si..."
创建表语句
CREATE TABLE public.contrial (
info json NOT NULL
);
最终目标是创建一个表,其中键作为列,值作为记录。嵌套键需要展平。
+-------------------------+-----------+------------------+----------+
| url | nested_id | nested_value | quotes |
+-------------------------+-----------+------------------+----------+
| https://some1-value.com | 4 | some1 text value | 5\" side |
+-------------------------+-----------+------------------+----------+
| https://some2-value.com | 5 | some2 text value | 6\" side |
+-------------------------+-----------+------------------+----------+
| https://some3-value.com | 6 | some3 text value | 7\" side |
+-------------------------+-----------+------------------+----------+