Configuring the JDBC Connector (Optional)
A newer version of this documentation is available. Use the version menu above to view the most up-to-date release of the Greenplum 6.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-42.2.5.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 Configuring PXF Servers. 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.
The PXF JDBC Connector uses JDBC connection pooling implemented by HikariCP. When a user queries or writes to an external table, the Connector establishes a connection pool for the associated server configuration the first time that it encounters a unique combination of
jdbc.password, connection property, and pool property settings. The Connector reuses connections in the pool subject to certain connection and timeout settings.
One or more connection pools may exist for a given server configuration, and user access to different external tables specifying the same server may share a connection pool.
Note: If you have enabled JDBC user impersonation in a server configuration, the JDBC Connector creates a separate connection pool for each Greenplum Database user that accesses any external table specifying that server configuration.
jdbc.pool.enabled property governs JDBC connection pooling for a server configuration. Connection pooling is enabled by default. To disable JDBC connection pooling for a server configuration, set the property to false:
<property> <name>jdbc.pool.enabled</name> <value>false</value> </property>
If you disable JDBC connection pooling for a server configuration, PXF does not reuse JDBC connections for that server. PXF creates a connection to the remote database for every partition of a query, and closes the connection when the query for that partition completes.
PXF exposes connection pooling properties that you can configure in a JDBC server definition. These properties are named with the
jdbc.pool.property. prefix and apply to each PXF JVM. The JDBC Connector automatically sets the following connection pool properties and default values:
|jdbc.pool.property.maximumPoolSize||The maximum number of connections to the database backend.||5|
|jdbc.pool.property.connectionTimeout||The maximum amount of time, in milliseconds, to wait for a connection from the pool.||30000|
|jdbc.pool.property.idleTimeout||The maximum amount of time, in milliseconds, after which an inactive connection is considered idle.||30000|
|jdbc.pool.property.minimumIdle||The minimum number of idle connections maintained in the connection pool.||0|
You can set other HikariCP-specific connection pooling properties for a server configuration by specifying
jdbc.pool.property.<HIKARICP_PROP_NAME> and the desired value in the
jdbc-site.xml configuration file for the server. Also note that the JDBC Connector passes along any property that you specify with a
jdbc.connection.property. prefix when it requests a connection from the JDBC
DriverManager. Refer to Connection-Level Properties above.
To not exceed the maximum number of connections allowed by the target database, and at the same time ensure that each PXF JVM services a fair share of the JDBC connections, determine the maximum value of
maxPoolSize based on the size of the Greenplum Database cluster as follows:
max_conns_allowed_by_remote_db / #_greenplum_segment_hosts
For example, if your Greenplum Database cluster has 16 segment hosts and the target database allows 160 concurrent connections, calculate
maxPoolSize as follows:
160 / 16 = 10
In practice, you may choose to set
maxPoolSize to a lower value, since the number of concurrent connections per JDBC query depends on the number of partitions used in the query. When a query uses no partitions, a single PXF JVM services the query. If a query uses 12 partitions, PXF establishes 12 concurrent JDBC connections to the remote database. Ideally, these connections are distributed equally among the PXF JVMs, but that is not guaranteed.
The PXF JDBC Connector uses the
jdbc.user setting or information in the
jdbc.url to determine the identity of the user to connect to the external data store. When PXF JDBC user impersonation is disabled (the default), the behavior of the JDBC Connector is further dependent upon the external data store. For example, if you are using the JDBC Connector to access Hive, the Connector uses the settings of certain Hive authentication and impersonation properties to determine the user. You may be required to provide a
jdbc.user setting, or add properties to the
jdbc.url setting in the server
jdbc-site.xml file. Refer to Configuring Hive Access via the JDBC Connector for more information on this procedure.
When you enable PXF JDBC user impersonation, the PXF JDBC Connector accesses the external data store on behalf of a Greenplum Database end user. The Connector uses the name of the Greenplum Database user that accesses the PXF external table to try to connect to the external data store.
When you enable JDBC user impersonation for a PXF server, PXF overrides the value of a
jdbc.user property setting defined in either
<greenplum_user_name>-user.xml, or specified in the external table DDL, with the Greenplum Database user name. For user impersonation to work effectively when the external data store requires passwords to authenticate connecting users, you must specify the
jdbc.password setting for each user that can be impersonated in that user’s
<greenplum_user_name>-user.xml property override file. Refer to Configuring a PXF User for more information about per-server, per-Greenplum-user configuration.
pxf.service.user.impersonation property in the
jdbc-site.xml configuration file governs JDBC user impersonation.
pxf.impersonation.jdbcproperty setting in the
By default, PXF JDBC user impersonation is disabled. Perform the following procedure to turn PXF user impersonation on or off for a JDBC server configuration.
Log in to your Greenplum Database master node as the administrative user:
$ ssh gpadmin@<gpmaster>
Identify the name of the PXF JDBC server configuration that you want to update.
Navigate to the server configuration directory. For example, if the server is named
gpadmin@gpmaster$ cd $PXF_CONF/servers/mysqldb
jdbc-site.xmlfile in the editor of your choice, and add or uncomment the user impersonation property and setting. For example, if you require user impersonation for this server configuration, set the
<property> <name>pxf.service.user.impersonation</name> <value>true</value> </propery>
jdbc-site.xmlfile and exit the editor.
pxf cluster synccommand to synchronize the PXF JDBC server configuration to your Greenplum Database cluster. For example:
gpadmin@gpmaster$ $GPHOME/pxf/bin/pxf cluster sync
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 construct 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
You may optionally 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 via DDL for additional information.
You can use the JDBC Connector to access Hive. Refer to Configuring the JDBC Connector for Hive Access for detailed information on this configuration procedure.
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_CONF/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