Use PXF File connector to read and write data between NFS and Greengage DB
With the PXF File connector, you can read and write data residing on a Network File System (NFS) mounted on the Greengage DB hosts.
This topic describes how to configure and use the PXF File connector for reading and writing data in NFS by using external tables and provides practical examples.
Before you begin, ensure that:
-
All files are accessible by
gpadminor by the operating system user that started the PXF process. -
The network file system is correctly mounted at the same local mount point on every Greengage DB host.
-
One or more named PXF server configurations are created as described in Configure a PXF network file system server.
Supported file types
The PXF File connector supports reading and writing the following file types from NFS.
| File type | Profile name | Supported operations |
|---|---|---|
Delimited single-line text |
file:text |
Read, write |
Single-line comma-separated text values (CSV) |
file:csv |
Read, write |
Delimited text with quoted linefeeds |
file:text:multi |
Read |
Fixed width single-line text |
file:fixedwidth |
Read, write |
Avro |
file:avro |
Read, write |
JSON |
file:json |
Read, write |
ORC |
file:orc |
Read, write |
Parquet |
file:parquet |
Read, write |
Create an external table using the PXF protocol
To create a Greengage DB external table to read or write data in NFS, use the following general syntax:
CREATE [READABLE | WRITABLE] EXTERNAL TABLE <table_name>
( <column_name> <data_type> [, ...] | LIKE <other_table> )
LOCATION ('pxf://<path_to_data>?PROFILE=file:<file_type>[&SERVER=<server_name>][&<custom-option>=<value>[...]]')
FORMAT '[TEXT|CSV|CUSTOM]' (<formatting-properties>)
[DISTRIBUTED BY (<column_name> [, ... ] ) | DISTRIBUTED RANDOMLY];
| Keyword | Value |
|---|---|
<table_name> |
The name of the table to create |
<column_name> |
The name of the column to create |
<data_type> |
The data type of the column |
LIKE <other_table> |
Specifies a table from which the new external table automatically copies all column names, data types, and distribution policy |
<path_to_data> |
The path to the directory or file in NFS.
The path is considered relative to the |
PROFILE=file:<file_type> |
The profile is specified as the |
SERVER=<server_name> |
The named server configuration that PXF uses to access the data.
If the option is omitted, the |
<custom‑option>=<value> |
One of the custom options provided in the |
FORMAT <value> |
The data format, which can be |
<formatting‑properties> |
Formatting properties supported by the profile. See Custom options, data format, and formatting properties for details |
DISTRIBUTED BY |
When loading data from a Greengage DB table into a writable external table, consider specifying the same distribution policy or column name on both tables. This will avoid extra motion of data between segments on the load operation. Learn more about table distribution in Distribution |
Examples
These examples demonstrate how to configure and use the PXF File connector for reading and writing CSV data in NFS by using external tables.
The examples assume that a network file system with the share point /mnt/extdata/pxf is configured and mounted on each Greengage DB cluster host.
Configure a PXF network file system server
To have PXF connect to NFS, you need to create the corresponding server configuration as described in Configure a PXF server in PXF documentation and then synchronize it to the Greengage DB cluster:
-
On the Greengage DB master host, log in as
gpadmin. -
Go to the $PXF_BASE/servers directory and create a network file system server configuration directory (for example, named nfs):
$ mkdir $PXF_BASE/servers/nfs -
Copy the server template configuration file $PXF_HOME/templates/pxf-site.xml to $PXF_BASE/servers/nfs:
$ cd $PXF_BASE/servers/nfs $ cp $PXF_HOME/templates/pxf-site.xml . -
The template file includes two mandatory properties that need to be set:
-
pxf.fs.basePathidentifies the base network file system share path. The file path specified in theLOCATIONclause of theCREATE EXTERNAL TABLEcommand is considered to be relative to this share path. -
pxf.service.user.impersonationregulates user impersonation. PXF does not support user impersonation for NFS and accesses it as the operating system user that started the PXF process, usuallygpadmin. Therefore, user impersonation must be explicitly turned off.
Open the template server configuration file in the editor and provide the relevant property values. For example, if the file system share point is the directory named /mnt/extdata/pxf, set it as the
pxf.fs.basePathproperty value:<?xml version="1.0" encoding="UTF-8"?> <configuration> ... <property> <name>pxf.service.user.impersonation</name> <value>false</value> </property> <property> <name>pxf.fs.basePath</name> <value>/mnt/extdata/pxf</value> </property> ... </configuration> -
-
Synchronize the server configuration to the Greengage DB cluster hosts:
$ pxf cluster sync
Read a CSV file from NFS
-
In the NFS shared folder, create a CSV file named customers.csv having the following content:
1,John,Doe,john.doe@example.com,123 Elm Street 2,Jane,Smith,jane.smith@example.com,456 Oak Street 3,Bob,Brown,bob.brown@example.com,789 Pine Street 4,Rob,Stuart,rob.stuart@example.com,119 Willow Street
-
On the Greengage DB master host, create a readable external table that references the customers.csv file. In the
LOCATIONclause, specify the PXFfile:csvprofile and the server configuration. In theFORMATclause, setCSVas the data format.CREATE EXTERNAL TABLE customers_r ( id INTEGER, first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(100), address VARCHAR(255) ) LOCATION ('pxf://customers.csv?PROFILE=file:csv&SERVER=nfs') FORMAT 'CSV'; -
Query the created external table:
SELECT * FROM customers_r;The output should look as follows:
id | first_name | last_name | email | address ----+------------+-----------+------------------------+------------------- 1 | John | Doe | john.doe@example.com | 123 Elm Street 2 | Jane | Smith | jane.smith@example.com | 456 Oak Street 3 | Bob | Brown | bob.brown@example.com | 789 Pine Street 4 | Rob | Stuart | rob.stuart@example.com | 119 Willow Street (4 rows)
Write a CSV file to NFS
-
On the Greengage DB master host, create a writable external table that writes data to the customers subfolder of the NFS shared folder. In the
LOCATIONclause, specify the PXFfile:csvprofile and the server configuration. In theFORMATclause, setCSVas the data format:CREATE WRITABLE EXTERNAL TABLE customers_w ( id INTEGER, first_name TEXT, last_name TEXT, email TEXT, address TEXT ) LOCATION ('pxf://customers?PROFILE=file:csv&SERVER=nfs') FORMAT 'CSV'; -
Insert data into the created external table:
INSERT INTO customers_w ( id, first_name, last_name, email, address ) VALUES (5,'Alice','Johnson','alice.johnson@example.com','10 Oak Avenue'), (6,'Charlie','Williams','charlie.williams@example.com','42 Maple Drive'), (7,'Bob','Smith','bob.smith@example.com','7 Pine Court'), (8,'Eve','Brown','eve.brown@example.com','12 Birch Lane'); -
View the contents of the customers subfolder of the NFS shared folder. The file list should look similar to the following:
230-0000000016_0 230-0000000016_1 230-0000000016_2 230-0000000016_3
-
Verify the contents of the created files. The output should look as follows:
8,Eve,Brown,eve.brown@example.com,12 Birch Lane 6,Charlie,Williams,charlie.williams@example.com,42 Maple Drive 7,Bob,Smith,bob.smith@example.com,7 Pine Court 5,Alice,Johnson,alice.johnson@example.com,10 Oak Avenue
Custom options, data format, and formatting properties
The custom options, data format, and formatting properties that you specify when creating an external table that references a file on a network file system are file type-specific.
Single-line text values and CSV
| Keyword | Value |
|---|---|
IGNORE_MISSING_PATH=<boolean> |
The action to take when |
SKIP_HEADER_COUNT=<numlines> |
The number of header lines to skip in the beginning of each file before reading the data.
The default value is |
COMPRESSION_CODEC |
The compression codec to use when writing data: |
FORMAT <value> |
The data format, which can be Note that the |
delimiter |
The delimiter character in the data.
For the |
Multiline text
| Keyword | Value |
|---|---|
IGNORE_MISSING_PATH |
The action to take when |
FORMAT |
To read multiline text data in NFS, |
Fixed-width text
| Keyword | Value |
|---|---|
NEWLINE |
When the |
COMPRESSION_CODEC |
The compression codec to use when writing data: |
IGNORE_MISSING_PATH |
The action to take when |
FORMAT 'CUSTOM' |
The custom format with the built-in custom formatter functions for read ( |
<field_name>='<width>' |
The name and the width of the field in characters.
Fields must be listed in their physical order.
The field names must match the columns listed in the When reading data, if the field value is less than the |
line_delim |
The line delimiter character in the data, |
Avro
| Keyword | Value |
|---|---|
COLLECTION_DELIM |
The delimiter characters placed between entries in a top-level array, map, or record field when mapping an Avro complex data type to a text column during data reading.
The default is the comma character ( |
MAPKEY_DELIM |
The delimiter characters placed between the key and value of a map entry when mapping an Avro complex data type to a text column during data reading.
The default is the colon character ( |
RECORDKEY_DELIM |
The delimiter characters placed between the field name and value of a record entry when mapping an Avro complex data type to a text column during data reading.
The default is the colon character ( |
SCHEMA |
The path to the Avro schema file in NFS.
The path is considered relative to the base path, which is specified as the |
IGNORE_MISSING_PATH |
The action to take when |
COMPRESSION_CODEC |
The compression codec to use when writing data: |
CODEC_LEVEL |
The compression level (applicable to the |
FORMAT 'CUSTOM' |
The custom format with the built-in custom formatter functions for read ( |
JSON
| Keyword | Value |
|---|---|
IDENTIFIER=<value> |
Specified only when accessing JSON data comprised of multiline records.
When a nested object also includes a field with the same name as the one specified as |
SPLIT_BY_FILE=<boolean> |
Defines how to split the data specified in |
IGNORE_MISSING_PATH=<boolean> |
The action to take when |
ROOT=<value> |
When writing to a single JSON object, identifies the name of the root-level object attribute |
COMPRESSION_CODEC |
The compression codec to use when writing data: If a compression codec is specified, the following naming convention applies to written files: |
FORMAT 'CUSTOM' |
The custom format with the built-in custom formatter functions for read ( |
ORC
| Keyword | Value |
|---|---|
IGNORE_MISSING_PATH |
The action to take when |
MAP_BY_POSITION |
Specifies whether PXF should map an ORC column to a Greengage DB column by position.
The default value is |
COMPRESSION_CODEC |
The compression codec to use when writing data: You must explicitly specify |
FORMAT 'CUSTOM' |
The custom format with the built-in custom formatter functions for read ( |
Parquet
| Keyword | Value |
|---|---|
IGNORE_MISSING_PATH |
The action to take when |
COMPRESSION_CODEC |
The compression codec to use when writing data: You must explicitly specify |
ROWGROUP_SIZE |
The size (in bytes) of the row group, which provides a logical partitioning of the data into rows. The default row group size is 8 * 1024 * 1024 bytes |
PAGE_SIZE |
The size (in bytes) of a page, which divides row groups in a column into column chunks. The default page size is 1 * 1024 * 1024 bytes |
ENABLE_DICTIONARY |
Specifies whether to enable dictionary encoding.
The default value is |
DICTIONARY_PAGE_SIZE |
When dictionary encoding is enabled, defines a single dictionary page per column, per row group.
|
PARQUET_VERSION |
The Parquet version; the supported values are |
SCHEMA |
The path to the Parquet schema file in NFS.
The path is considered relative to the |
FORMAT 'CUSTOM' |
The custom format with the built-in custom formatter functions for read ( |