Querying NDJSON Files in Stage
Query NDJSON Files in Stage
Syntax:
SELECT [<alias>.]$1:<column> [, $1:<column> ...]
FROM {@<stage_name>[/<path>] [<table_alias>] | '<uri>' [<table_alias>]}
[(
[<connection_parameters>],
[ PATTERN => '<regex_pattern>'],
[ FILE_FORMAT => 'NDJSON| <custom_format_name>'],
[ FILES => ( '<file_name>' [ , '<file_name>' ] [ , ... ] ) ]
)]
Tips
NDJSON is a variant for the whole row, the column is $1:<column> [, $1:<column> ...]
.
Tutorial
Step 1. Create an External Stage
Create an external stage with your own S3 bucket and credentials where your NDJSON files are stored.
CREATE STAGE ndjson_query_stage
URL = 's3://load/ndjson/'
CONNECTION = (
ACCESS_KEY_ID = '<your-access-key-id>'
SECRET_ACCESS_KEY = '<your-secret-access-key>'
);
Step 2. Create Custom NDJSON File Format
CREATE FILE FORMAT ndjson_query_format
TYPE = NDJSON,
COMPRESSION = AUTO;
- More NDJSON file format options refer to NDJSON File Format Options
Step 3. Query NDJSON Files
SELECT $1:title, $1:author
FROM @ndjson_query_stage
(
FILE_FORMAT => 'ndjson_query_format',
PATTERN => '.*[.]ndjson'
);
If the NDJSON files are compressed with gzip, we can use the following query:
SELECT $1:title, $1:author
FROM @ndjson_query_stage
(
FILE_FORMAT => 'ndjson_query_format',
PATTERN => '.*[.]ndjson[.]gz'
);