Accessing Files on a Network File System
You can use PXF to read data that resides on a network file system mounted on your Greenplum Database hosts. PXF supports reading and writing the following file types from a network file system:
| File Type | Profile Name | Operations Supported |
|---|---|---|
| delimited single line text | file:text | read, write |
| delimited single line comma-separated values of text | 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 |
PXF does not support user impersonation when you access a network file system. PXF accesses a file as the operating system user that started the PXF process, usually gpadmin.
Reading from, and writing to (where supported), a file of these types on a network file system is similar to reading/writing the file type on Hadoop.
Prerequisites
Before you use PXF to access files on a network file system, ensure that:
- You can identify the PXF runtime configuration directory (
$PXF_BASE). - You have configured PXF, and PXF is running on each Greenplum Database host. See Configuring PXF for additional information.
- 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 Greenplum Database host.
- You can identify the mount or share point of the network file system.
- You have created one or more named PXF server configurations as described in Configuring a PXF Network File System Server.
Configuring a PXF Network File System Server
Before you use PXF to access a file on a network file system, you must create a server configuration and then synchronize the PXF configuration to all Greenplum hosts. This procedure will typically be performed by the Greenplum Database administrator.
Use the server template configuration file <PXF_INSTALL_DIR>/templates/pxf-site.xml when you configure a network file system server for PXF. This template file includes the mandatory property pxf.fs.basePath that you configure to identify the network file system share path. PXF considers the file path that you specify in a CREATE EXTERNAL TABLE LOCATION clause that uses this server to be relative to this share path.
PXF does not support user impersonation when you access a network file system; you must explicitly turn off user impersonation in a network file system server configuration.
-
Log in to the Greenplum Database coordinator host:
$ ssh gpadmin@<coordinator> -
Choose a name for the file system server. You will provide the name to Greenplum users that you choose to allow to read from or write to files on the network file system.
Note: The server name
defaultis reserved. -
Create the
$PXF_BASE/servers/<server_name>directory. For example, use the following command to create a file system server configuration namednfssrvcfg:gpadmin@coordinator$ mkdir $PXF_BASE/servers/nfssrvcfg -
Copy the PXF
pxf-site.xmltemplate file to thenfssrvcfgserver configuration directory. For example:gpadmin@coordinator$ cp <PXF_INSTALL_DIR>/templates/pxf-site.xml $PXF_BASE/servers/nfssrvcfg/ -
Open the template server configuration file in the editor of your choice, and uncomment and provide property values appropriate for your environment. For example, if the file system share point is the directory named
/mnt/extdata/pxffs, uncomment and set these server properties:<?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/pxffs</value>
</property>
...
</configuration> -
Save your changes and exit the editor.
-
Synchronize the PXF server configuration to the Greenplum Database cluster:
gpadmin@coordinator$ pxf cluster sync
Creating the External Table
The following syntax creates a Greenplum Database external table that references a file on a network file system. Use the appropriate file:* profile for the file type that you want to access.
CREATE [READABLE | WRITABLE] EXTERNAL TABLE <table_name>
( <column_name> <data_type> [, ...] | LIKE <other_table> )
LOCATION ('pxf://<file-path>?PROFILE=file:<file-type>[&SERVER=<server_name>][&<custom-option>=<value>[...]]')
FORMAT '[TEXT|CSV|CUSTOM]' (<formatting-properties>);
The specific keywords and values used in the Greenplum Database CREATE EXTERNAL TABLE command are described in the table below.
| Keyword | Value |
|---|---|
| <file‑path> | The path to a directory or file on the network file system. PXF considers this file or path as being relative to the pxf.fs.basePath property value specified in <server_name>'s server configuration. <file‑path> must not specify a relative path nor include the dollar sign ($) character. |
| PROFILE | The PROFILE keyword value must specify a file:<file-type> identified in the table above. |
| SERVER=<server_name> | The named server configuration that PXF uses to access the network file system. PXF uses the default server if not specified. |
| <custom‑option>=<value> | <custom-option> is profile-specific. |
| FORMAT <value> | PXF profiles support the TEXT, CSV, and CUSTOM formats. |
| <formatting‑properties> | Formatting properties supported by the profile; for example, the FORMATTER or delimiter. |
The <custom-option>s, FORMAT, and <formatting‑properties> that you specify when accessing a file on a network file system are dependent on the <file-type>. Refer to the Hadoop documentation for the <file-type> of interest for these settings.
Example: Reading From and Writing to a CSV File on a Network File System
This example assumes that you have configured and mounted a network file system with the share point /mnt/extdata/pxffs on the Greenplum Database coordinator host, the standby coordinator host, and on each segment host.
In this example, you:
- Create a CSV file on the network file system and add data to the file.
- Configure a network file system server for the share point.
- Create a PXF readable external table that references the directory containing the CSV file, and read the data.
- Create a PXF writable external table that references the directory containing the CSV file, and write some data.
- Read from the original readable external table again.
Create a CSV File
-
Create a directory (relative to the network file system share point) named
/mnt/extdata/pxffs/ex1:gpadmin@coordinator$ mkdir -p /mnt/extdata/pxffs/ex1 -
Create a CSV file named
somedata.csvin the directory:$ echo 'Prague,Jan,101,4875.33
Rome,Mar,87,1557.39
Bangalore,May,317,8936.99
Beijing,Jul,411,11600.67' > /mnt/extdata/pxffs/ex1/somedata.csv
Create the Network File System Server
Create a server configuration named nfssrvcfg with share point /mnt/extdata/pxffs as described in Configuring a PXF Network File System Server.