【问题标题】:How to insert multiple JSON files into postgresql table at a time?如何一次将多个 JSON 文件插入 postgresql 表?
【发布时间】:2015-06-12 09:48:04
【问题描述】:

我有多个 JSON 文件,它们都具有相同的格式,但值因每笔交易而异。我想将此数据迁移到 postgresql 表。进行此操作的最佳方法是什么?

现在,我正在使用以下查询:

CREATE TABLE TEST (MULTIPROCESS VARCHAR(20), HTTP_REFERER VARCHAR(50));
INSERT INTO TEST SELECT MULTIPROCESS, HTTP_REFERER FROM json_populate_record(NULL::test, '{"multiprocess": true,"http_referer": "http://localhost:9000/"}');

但是,一旦文件数量变大,使用这种技术就变得非常困难。有没有其他方法可以有效地做到这一点?

【问题讨论】:

    标签: sql json database postgresql data-migration


    【解决方案1】:

    您可以使用LATERAL JOIN 一次插入多行:

     WITH 
      json AS(
       VALUES('{"multiprocess": true,"http_referer":"http://localhost:9000"}')
         ,('{"multiprocess": false,"http_referer": "http://localhost:9001/"}')
         ,('{"multiprocess": true,"http_referer": "http://localhost:9002/"}')
    ) INSERT INTO test 
       SELECT multiprocess, http_referer 
       FROM   json, LATERAL json_populate_record(NULL::test, json.column1::json); 
    

    或者你可以先插入一个临时表,然后填充你的另一个表。

    【讨论】:

      猜你喜欢
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 1970-01-01
      • 2014-01-15
      • 2017-01-06
      • 1970-01-01
      • 2017-10-26
      相关资源
      最近更新 更多