DEV Community

Michael
Michael

Posted on

Loading JSON Data Files in GBase 8a with data_format 6

GBase 8a can load data files in JSON format. This post collects the working samples and the caveats for the China-domestically developed GBase 8a MPP database from GBASE. The feature needs a newer version — the tests here use the April 2023 build 9.5.3.28, and older versions may not support it.

The JSON load parameter

Use data_format 6 to tell the loader the data is JSON.

A single line with a single record

The file holds exactly one line and one record.

gbase> system cat /home/gbase/json1.txt
{"id":"1","name":"First"}
gbase> load data infile 'sftp://gbase:gbase1234@10.0.2.103/home/gbase/json1.txt' into table json data_format 6;
Query OK, 1 row affected (Elapsed: 00:00:00.77)
Task 11 finished, Loaded 1 records, Skipped 0 records

gbase> select * from json;
+------+-------+
| id   | name  |
+------+-------+
|    1 | First |
+------+-------+
1 row in set (Elapsed: 00:00:00.01)
Enter fullscreen mode Exit fullscreen mode

One record per line

Each line is one record, with several lines. Parsing is line-based.

gbase> system cat /home/gbase/json2.txt
{"id":"1","name":"First"}
{"id":"2","name":"second no comer between line"}
gbase> truncate table json;
Query OK, 5 rows affected (Elapsed: 00:00:00.13)

gbase> load data infile 'sftp://gbase:gbase1234@10.0.2.103/home/gbase/json2.txt' into table json data_format 6;
Query OK, 2 rows affected (Elapsed: 00:00:00.73)
Task 28 finished, Loaded 2 records, Skipped 0 records

gbase> select * from json;
+------+------------------------------+
| id   | name                         |
+------+------------------------------+
|    2 | second no comer between line |
|    1 | First                        |
+------+------------------------------+
2 rows in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

A trailing comma between lines does not affect the result — with or without it is fine, because parsing treats the line as the basic unit. In the sample below, a trailing comma produces one skipped record:

{"id":"1","name":"First"},
{"id":"2","name":"second no comer between line"}
gbase> truncate table json;
Query OK, 2 rows affected (Elapsed: 00:00:00.12)

gbase> load data infile 'sftp://gbase:gbase1234@10.0.2.103/home/gbase/json2_2.txt' into table json data_format 6;
Query OK, 2 rows affected (Elapsed: 00:00:00.68)
Task 30 finished, Loaded 2 records, Skipped 1 records

gbase> select * from json;
+------+------------------------------+
| id   | name                         |
+------+------------------------------+
|    1 | First                        |
|    2 | second no comer between line |
+------+------------------------------+
2 rows in set (Elapsed: 00:00:00.01)
Enter fullscreen mode Exit fullscreen mode

Several records on one line

Concatenated, with no delimiter

gbase> system cat /home/gbase/json2_3.txt
{"id":"1","name":"First"}{"id":"2","name":"second no comer between line"}
gbase> truncate table json;
Query OK, 4 rows affected (Elapsed: 00:00:00.12)

gbase> load data infile 'sftp://gbase:gbase1234@10.0.2.103/home/gbase/json2_3.txt' into table json data_format 6;
Query OK, 2 rows affected (Elapsed: 00:00:00.83)
Task 33 finished, Loaded 2 records, Skipped 0 records

gbase> select * from json;
+------+------------------------------+
| id   | name                         |
+------+------------------------------+
|    1 | First                        |
|    2 | second no comer between line |
+------+------------------------------+
2 rows in set (Elapsed: 00:00:00.00)
Enter fullscreen mode Exit fullscreen mode

If a delimiter such as a comma sits between the records, only the first one is loaded and the rest fail:

gbase> system cat /home/gbase/json2_4.txt
{"id":"1","name":"First"},{"id":"2","name":"second no comer between line"}
gbase> truncate table json;
Query OK, 2 rows affected (Elapsed: 00:00:00.13)

gbase> load data infile 'sftp://gbase:gbase1234@10.0.2.103/home/gbase/json2_4.txt' into table json data_format 6;
Query OK, 1 row affected (Elapsed: 00:00:00.59)
Task 35 finished, Loaded 1 records, Skipped 1 records

gbase> select * from json;
+------+-------+
| id   | name  |
+------+-------+
|    1 | First |
+------+-------+
1 row in set (Elapsed: 00:00:00.00)

Enter fullscreen mode Exit fullscreen mode

A JSON array, comma-separated

Square brackets wrap the records and commas separate them:

[{"id":"1","name":"First"},{"id":"2","name":"second no comer between line"}]
gbase> truncate table json;
Query OK, 1 row affected (Elapsed: 00:00:00.12)

gbase> load data infile 'sftp://gbase:gbase1234@10.0.2.103/home/gbase/json2_5.txt' into table json data_format 6;
Query OK, 2 rows affected (Elapsed: 00:00:02.53)
Task 37 finished, Loaded 2 records, Skipped 0 records

gbase> select * from json;
+------+------------------------------+
| id   | name                         |
+------+------------------------------+
|    1 | First                        |
|    2 | second no comer between line |
+------+------------------------------+
2 rows in set (Elapsed: 00:00:00.01)
Enter fullscreen mode Exit fullscreen mode

An array without the comma between records gives a wrong result:

gbase> load data infile 'sftp://gbase:gbase1234@10.0.2.103/home/gbase/json2_5.txt' into table json data_format 6;
Query OK, 0 rows affected (Elapsed: 00:00:00.67)
Task 38 finished, Loaded 0 records, Skipped 1 records

gbase> system cat /home/gbase/json2_5.txt
[{"id":"1","name":"First"}{"id":"2","name":"second no comer between line"}]
gbase>
Enter fullscreen mode Exit fullscreen mode

Several lines, several records per line

Each line still follows the single-line rules above: multiple records on one line can be concatenated or wrapped in an array, and different lines may use different formats.

gbase> system cat /home/gbase/json2_6.txt
[{"id":"1","name":"First"},{"id":"2","name":"second no comer between line"}]
{"id":"3","name":"Third"}
gbase> truncate table json;
Query OK, 2 rows affected (Elapsed: 00:00:00.11)

gbase> load data infile 'sftp://gbase:gbase1234@10.0.2.103/home/gbase/json2_6.txt' into table json data_format 6;
Query OK, 3 rows affected (Elapsed: 00:00:00.71)
Task 40 finished, Loaded 3 records, Skipped 0 records

gbase> select * from json;
+------+------------------------------+
| id   | name                         |
+------+------------------------------+
|    1 | First                        |
|    3 | Third                        |
|    2 | second no comer between line |
+------+------------------------------+
3 rows in set (Elapsed: 00:00:00.01)
Enter fullscreen mode Exit fullscreen mode

Caveats

Nesting

A JSON value nested inside another JSON value is not supported, for example {"id":{"id1":"1","id2":"2"}}.

Data splitting

A large file can be split into chunks handled by different nodes to speed things up. Splitting is not supported in the following cases; the whole data file is then treated as one single JSON object and cannot be divided. It can also exceed the gbase_loader_max_line_length upper limit for a single line, but every data format must be completely correct — no malformed data is allowed.

  • Data stored continuously: no delimiter, with line terminated by '' set to empty.
  • The data value contains a line separator: the having lines separator parameter is specified.

The constant values true, false and null

Per the JSON spec these must be written as false, true and null in lowercase, and must not be wrapped in double quotes.

Going forward, settle the file layout before a load on a gbase cluster: one record per line, several concatenated records with no delimiter, or a proper comma-separated array. A gbase database parses JSON line by line, so a stray comma inside one line is the difference between all records landing and only the first one being loaded.

Top comments (0)