Configuring the JDBC Connector (Optional)
A newer version of this documentation is available. Click here to view the most up-to-date release of the Greenplum 5.x documentation.
You can use PXF to access an external SQL database including MySQL, ORACLE, PostgreSQL, Hive, and Apache Ignite. This topic describes how to configure the PXF JDBC Connector to access these external data sources.
If you do not plan to use the PXF JDBC Connector, then you do not need to perform this procedure.
To access data in an external SQL database with the PXF JDBC Connector, you must:
- Register a compatible JDBC driver JAR file
- Specify the JDBC driver class name, database URL, and client credentials
In previous releases of Greenplum Database, you may have specified the JDBC driver class name, database URL, and client credentials via options in the
CREATE EXTERNAL TABLE command. PXF now supports file-based server configuration for the JDBC Connector. This configuration, described below, allows you to specify these options and credentials in a file.
Note: PXF external tables that you previously created that directly specified the JDBC connection options will continue to work. If you want to move these tables to use JDBC file-based server configuration, you must create a server configuration, drop the external tables, and then recreate the tables specifying an appropriate
The PXF JDBC Connector is installed with the
postgresql-8.4-702.jdbc4.jar JAR file. If you require a different JDBC driver, ensure that you install the JDBC driver JAR file for the external SQL database in the
$PXF_CONF/lib directory on each segment host. Be sure to install JDBC driver JAR files that are compatible with your JRE version. See Registering PXF JAR Dependencies for additional information.
When you configure the PXF JDBC Connector, you add at least one named PXF server configuration for the connector as described in About PXF Server Configuration. You can also configure one or more statically-defined queries to run against the remote SQL database.
PXF provides a template configuration file for the JDBC Connector. This server template configuration file, located in
$PXF_CONF/templates/jdbc-site.xml, identifies properties that you can configure to establish a connection to the external SQL database. The template also includes optional properties that you can set before executing query or insert commands in the external database session.
The required properties in the
jdbc-site.xml server template file follow:
|jdbc.driver||Class name of the JDBC driver.||The JDBC driver Java class name; for example
|jdbc.url||The URL that the JDBC driver uses to connect to the database.||The database connection URL (database-specific); for example
|jdbc.user||The database user name.||The user name for connecting to the database.|
|jdbc.password||The password for
||The password for connecting to the database.|
To set additional JDBC connection-level properties, add
jdbc.connection.property.<CPROP_NAME> properties to
jdbc-site.xml. PXF passes these properties to the JDBC driver when it establishes the connection to the external SQL database (
<CPROP_NAME> with the connection property name and specify its value:
|jdbc.connection.property.<CPROP_NAME>||The name of a property (<CPROP_NAME>) to pass to the JDBC driver when PXF establishes the connection to the external SQL database.||The value of the <CPROP_NAME> property.|
Example: To set the
createDatabaseIfNotExist connection property on a JDBC connection to a PostgreSQL database, include the following property block in
<property> <name>jdbc.connection.property.createDatabaseIfNotExist</name> <value>true</value> </property>
Ensure that the JDBC driver for the external SQL database supports any connection-level property that you specify.
The SQL standard defines four transaction isolation levels. The level that you specify for a given connection to an external SQL database determines how and when the changes made by one transaction executed on the connection are visible to another.
The PXF JDBC Connector exposes an optional server configuration property named
jdbc.connection.transactionIsolation that enables you to specify the transaction isolation level. PXF sets the level (
setTransactionIsolation()) just after establishing the connection to the external SQL database.
The JDBC Connector supports the following
jdbc.connection.transactionIsolation property values:
|SQL Level||PXF Property Value|
For example, to set the transaction isolation level to Read uncommitted, add the following property block to the
<property> <name>jdbc.connection.transactionIsolation</name> <value>READ_UNCOMMITTED</value> </property>
Different SQL databases support different transaction isolation levels. Ensure that the external database supports the level that you specify.
The PXF JDBC Connector executes a query or insert command on an external SQL database table in a statement. The Connector exposes properties that enable you to configure certain aspects of the statement before the command is executed in the external database. The Connector supports the following statement-level properties:
|jdbc.statement.batchSize||The number of rows to write to the external database table in a batch.||The number of rows. The default write batch size is 100.|
|jdbc.statement.fetchSize||The number of rows to fetch/buffer when reading from the external database table.||The number of rows. The default read fetch size is 1000.|
|jdbc.statement.queryTimeout||The amount of time (in seconds) the JDBC driver waits for a statement to execute. This timeout applies to statements created for both read and write operations.||The timeout duration in seconds. The default wait time is unlimited.|
PXF uses the default value for any statement-level property that you do not explicitly configure.
Example: To set the read fetch size to 5000, add the following property block to
<property> <name>jdbc.statement.fetchSize</name> <value>5000</value> </property>
Ensure that the JDBC driver for the external SQL database supports any statement-level property that you specify.
To set session-level properties, add the
jdbc.session.property.<SPROP_NAME> property to
jdbc-site.xml. PXF will
SET these properties in the external database before executing a query.
<SPROP_NAME> with the session property name and specify its value:
|jdbc.session.property.<SPROP_NAME>||The name of a session property (<SPROP_NAME>) to set before query execution.||The value of the <SPROP_NAME> property.|
Note: The PXF JDBC Connector passes both the session property name and property value to the external SQL database exactly as specified in the
jdbc-site.xml server configuration file. To limit the potential threat of SQL injection, the Connector rejects any property name or value that contains the
The PXF JDBC Connector handles the session property
SET syntax for all supported external SQL databases.
Example: To set the
search_path parameter before running a query in a PostgreSQL database, add the following property block to
<property> <name>jdbc.session.property.search_path</name> <value>public</value> </property>
Ensure that the JDBC driver for the external SQL database supports any property that you specify.
A PXF named query is a static query that you configure, and that PXF runs in the remote SQL database.
To configure and use a PXF JDBC named query:
- You define the query in a text file.
- You provide the query name to Greenplum Database users.
- The Greenplum Database user references the query in a Greenplum Database external table definition.
PXF runs the query each time the user invokes a
SELECT command on the Greenplum Database external table.
You create a named query by adding the query statement to a text file that has the following naming format:
<query_name>.sql. You can define one or more named queries for a JDBC server configuration. Each query must reside in a separate text file.
You must place a query text file in the PXF JDBC server configuration directory from which it will be accessed. If you want to make the query available to more than one JDBC server configuration, you must copy the query text file to the configuration directory for each JDBC server.
The query text file must contain a single query that you want to run in the remote SQL database. You must contruct the query in accordance with the syntax supported by the database.
For example, if a MySQL database has a
customers table and an
orders table, you could include the following SQL statement in a query text file:
SELECT c.name, c.city, sum(o.amount) AS total, o.month FROM customers c JOIN orders o ON c.id = o.customer_id WHERE c.state = 'CO' GROUP BY c.name, c.city, o.month
Do not provide the ending semicolon (
;) for the SQL statement.
The Greenplum Database user references a named query by specifying the query file name without the extension. For example, if you define a query in a file named
report.sql, the name of that query is
Named queries are associated with a specific JDBC server configuration. You will provide the available query names to the Greenplum Database users that you allow to create external tables using the server configuration.
The Greenplum Database user specifies
query:<query_name> rather than the name of a remote SQL database table when they create the external table. For example, if the query is defined in the file
CREATE EXTERNAL TABLE
LOCATION clause would include the following components:
LOCATION ('pxf://query:report?PROFILE=JDBC&SERVER=mydb ...')
Refer to About Using Named Queries for information about using PXF JDBC named queries.
You can override the JDBC server configuration by directly specifying certain JDBC properties via custom options in the
CREATE EXTERNAL TABLE command
LOCATION clause. Refer to Overriding the JDBC Server Configuration for additional information.
Ensure that you have initialized PXF before you configure a JDBC Connector server.
In this procedure, you name and add a PXF JDBC server configuration for a PostgreSQL database and synchronize the server configuration(s) to the Greenplum Database cluster.
Log in to your Greenplum Database master node:
$ ssh gpadmin@<gpmaster>
Choose a name for the JDBC server. You will provide the name to Greenplum users that you choose to allow to reference tables in the external SQL database as the configured user.
Note: The server name
$PXF_HOME/servers/<server_name>directory. For example, use the following command to create a JDBC server configuration named
gpadmin@gpmaster$ mkdir $PXF_CONF/servers/pg_user1_testdb
Copy the PXF JDBC server template file to the server configuration directory. For example:
gpadmin@gpmaster$ cp $PXF_CONF/templates/jdbc-site.xml $PXF_CONF/servers/pg_user1_testdb/
Open the template server configuration file in the editor of your choice, and provide appropriate property values for your environment. For example, if you are configuring access to a PostgreSQL database named
testdbon a PostgreSQL instance running on the host named
pgserverhostfor the user named
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property> <name>jdbc.driver</name> <value>org.postgresql.Driver</value> </property> <property> <name>jdbc.url</name> <value>jdbc:postgresql://pgserverhost:5432/testdb</value> </property> <property> <name>jdbc.user</name> <value>user1</value> </property> <property> <name>jdbc.password</name> <value>changeme</value> </property> </configuration>
Save your changes and exit the editor.
pxf cluster synccommand to copy the new server configuration to the Greenplum Database cluster. For example:
gpadmin@gpmaster$ $GPHOME/pxf/bin/pxf cluster sync