Search in sources :

Example 1 with DriverWrapper

use of org.apache.spark.sql.execution.datasources.jdbc.DriverWrapper in project hudi by apache.

the class UtilHelpers method createConnectionFactory.

/**
 * Returns a factory for creating connections to the given JDBC URL.
 *
 * @param options - JDBC options that contains url, table and other information.
 * @return
 * @throws SQLException if the driver could not open a JDBC connection.
 */
private static Connection createConnectionFactory(Map<String, String> options) throws SQLException {
    String driverClass = options.get(JDBCOptions.JDBC_DRIVER_CLASS());
    DriverRegistry.register(driverClass);
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    Driver driver = null;
    while (drivers.hasMoreElements()) {
        Driver d = drivers.nextElement();
        if (d instanceof DriverWrapper) {
            if (((DriverWrapper) d).wrapped().getClass().getCanonicalName().equals(driverClass)) {
                driver = d;
            }
        } else if (d.getClass().getCanonicalName().equals(driverClass)) {
            driver = d;
        }
        if (driver != null) {
            break;
        }
    }
    Objects.requireNonNull(driver, String.format("Did not find registered driver with class %s", driverClass));
    Properties properties = new Properties();
    properties.putAll(options);
    Connection connect;
    String url = options.get(JDBCOptions.JDBC_URL());
    connect = driver.connect(url, properties);
    Objects.requireNonNull(connect, String.format("The driver could not open a JDBC connection. Check the URL: %s", url));
    return connect;
}
Also used : DriverWrapper(org.apache.spark.sql.execution.datasources.jdbc.DriverWrapper) Connection(java.sql.Connection) Driver(java.sql.Driver) Properties(java.util.Properties) TypedProperties(org.apache.hudi.common.config.TypedProperties)

Aggregations

Connection (java.sql.Connection)1 Driver (java.sql.Driver)1 Properties (java.util.Properties)1 TypedProperties (org.apache.hudi.common.config.TypedProperties)1 DriverWrapper (org.apache.spark.sql.execution.datasources.jdbc.DriverWrapper)1