From: Rob Sargent <robjsargent@gmail.com>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: pinker <pinker@onet.eu>
Cc: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: Loading 500m json files to database
Date: Mon, 23 Mar 2020 19:51:27 -0600
Message-ID: <3A9D9AC0-AD61-45C6-87AD-BD8C9E255A37@gmail.com> (raw)
In-Reply-To: <CAKFQuwbhc02XWAJ9DNyao5M1EUf=ECxMQX0U+RftgAeDJNmKTw@mail.gmail.com>
References: <1584959088557-0.post@n3.nabble.com>
<CAKFQuwbhc02XWAJ9DNyao5M1EUf=ECxMQX0U+RftgAeDJNmKTw@mail.gmail.com>
> On Mar 23, 2020, at 7:11 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:
>
> On Mon, Mar 23, 2020 at 3:24 AM pinker <pinker@onet.eu <mailto:pinker@onet.eu>> wrote:
> time for i in datafiles/*; do
> psql -c "\copy json_parts(json_data) FROM $i"&
> done
>
> Don't know whether this is faster but it does avoid spinning up a connection multiple times.
>
> #bash, linux
> function append_each_split_file_to_etl_load_script() {
> for filetoload in ./*; do
> ronumber="$(basename $filetoload)"
> # only process files since subdirs can be present
> if [[ -f "$filetoload" ]]; then
> echo ""
> echo "\set invoice"' `cat '"'""$filetoload""'"'`'
> echo ", ('$ronumber',:'invoice')"
> fi >> "$PSQLSCRIPT"
> done
>
> echo "" >> "$PSQLSCRIPT"
> echo ";" >> "$PSQLSCRIPT"
> echo "" >> "$PSQLSCRIPT"
> }
>
> There is a bit other related code that is needed (for my specific usage) but this is the core of it. Use psql variables to capture the contents of each file into a variable and then just perform a normal insert (specifically, a VALUES (...), (...) variant). Since you can intermix psql and SQL you basically output a bloody long script, that has memory issues at scale - but you can divide and conquer - and then "psql --file bloody_long_script_part_1_of_100000.psql".
>
> David J.
>
Can one put 550M files in a single directory? I thought it topped out at 16M or so.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: robjsargent@gmail.com, david.g.johnston@gmail.com, pinker@onet.eu
Subject: Re: Loading 500m json files to database
In-Reply-To: <3A9D9AC0-AD61-45C6-87AD-BD8C9E255A37@gmail.com>
References: <1584959088557-0.post@n3.nabble.com> <CAKFQuwbhc02XWAJ9DNyao5M1EUf=ECxMQX0U+RftgAeDJNmKTw@mail.gmail.com> <3A9D9AC0-AD61-45C6-87AD-BD8C9E255A37@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox