繁体   English   中英

如何将 JSON 文件导入 postgresql 数据库?

[英]How to import a JSON file into postgresql databse?

我只是试图在我的 PostgreSQL 数据库中读取一个 .JSON 文件,但它无法读取它。 我是用 a.csv 文件做的,但是用 a.JSON 文件做不到。

这是 .JSON 文件的样子:

{"s":"S1","p":"P1","j":"J1","qty":200}
{"s":"S1","p":"P1","j":"J4","qty":700}
{"s":"S2","p":"P3","j":"J1","qty":400}
{"s":"S2","p":"P3","j":"J2","qty":200}

这是我试过的代码,我先创建表,然后将文件中的数据复制到数据库中。

create table notifies(
    s varchar(999),
    p varchar(999),
    j varchar(999),
    qty varchar(999)
);

copy public.notifies(s,p,j,qty)
from 'C:\temp\spj.json';

您可以将您的 json 文件导入临时表,然后从那里填充表notifies 例如:

创建一个 tmp 表..

CREATE TABLE tmp (c text);

.. 使用COPY将您的 json 文件导入表tmp ..

mydb=# \copy tmp from 'C:\temp\spj.json'

...最后填充表格notifies

INSERT INTO notifies 
SELECT q.* FROM tmp, json_to_record(c::json) AS q
 (s text, p text, j text, qty int);

SELECT * FROM notifies;

 s  | p  | j  | qty 
----+----+----+-----
 S1 | P1 | J1 | 200
 S1 | P1 | J4 | 700
 S2 | P3 | J1 | 400
 S2 | P3 | J2 | 200
(4 Zeilen)

之后,您可能想要删除表tmp

DROP TABLE tmp;

编辑:一个非常优雅的替代方法是使用json_populate_record ,正如@Jeremy 所建议的那样。 谢谢。 请参阅下面的评论。

INSERT INTO notifies 
SELECT q.* FROM tmp, json_populate_record(null::notifies, c::json) AS q;

SELECT * FROM notifies ;
 s  | p  | j  | qty 
----+----+----+-----
 S1 | P1 | J1 | 200
 S1 | P1 | J4 | 700
 S2 | P3 | J1 | 400
 S2 | P3 | J2 | 200
(4 Zeilen)

一种方法是使用吊索。 请参阅此博客文章,其中介绍了将 JSON 文件加载到 PG 中。

# in Windows Powershell
$ set POSTGRES 'postgresql://...'

$ sling conns list
+------------+------------------+-----------------+
| CONN NAME  | CONN TYPE        | SOURCE          |
+------------+------------------+-----------------+
| POSTGRES   | DB - PostgreSQL  | env variable    |
+------------+------------------+-----------------+

$ sling run --src-stream file://C:/temp/records.json --tgt-conn POSTGRES --tgt-object public.records --mode full-refresh
11:09AM INF connecting to target database (postgres)
11:09AM INF reading from source file system (file)
11:09AM INF writing to target database [mode: full-refresh]
11:09AM INF streaming data
11:09AM INF dropped table public.records
11:09AM INF created table public.records
11:09AM INF inserted 500 rows in 0 secs [1,556 r/s]
11:09AM INF execution succeeded

如果不存在,使用debug模式将显示create table if not exists public.records ("data" jsonb) 如果您想展平 JSON,sling 也可以通过添加--src-options 'flatten: true'选项来做到这一点:

$ sling run --src-stream file://C:/temp/records.json --src-options 'flatten: true' --tgt-conn POSTGRES --tgt-object public.records --mode full-refresh

这种情况下的 DDL 类似于:

create table if not exists public.records ("_id" varchar(255),
"age" integer,
"balance" varchar(255),
"company__about" text,
"company__address" varchar(255),
"company__email" varchar(255),
"company__latitude" numeric,
"company__longitude" numeric,
"company__name" varchar(255),
"company__phone" varchar(255),
"company__registered" varchar(255),
"isactive" bool,
"name" varchar(255),
"picture" varchar(255),
"tags" jsonb)

仅供参考,我是 sling 的作者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM