Search in sources :

Example 1 with DataSourceProperties

use of com.thinkbiganalytics.db.DataSourceProperties in project kylo by Teradata.

the class PostgreSqlSchemaParserTest method prepareDataSourceWithDatabaseUrl.

/**
 * Verify ignoring catalog name if already specified by the data source URL.
 */
@Test
public void prepareDataSourceWithDatabaseUrl() throws SQLException {
    // Test preparing data source with existing catalog
    DataSourceProperties dataSource = new DataSourceProperties("user", "password", "jdbc:postgresql://localhost:5432/kylo");
    final JdbcSchemaParser parser = new PostgreSqlSchemaParser();
    DataSourceProperties prepared = parser.prepareDataSource(dataSource, null);
    Assert.assertEquals(dataSource, prepared);
    Assert.assertEquals(dataSource.getUrl(), prepared.getUrl());
}
Also used : JdbcSchemaParser(com.thinkbiganalytics.discovery.schema.JdbcSchemaParser) DataSourceProperties(com.thinkbiganalytics.db.DataSourceProperties) Test(org.junit.Test)

Example 2 with DataSourceProperties

use of com.thinkbiganalytics.db.DataSourceProperties in project kylo by Teradata.

the class PostgreSqlSchemaParserTest method prepareDataSourceWithDatabaseInfo.

/**
 * Verify ignoring catalog name if already specified by the data source info.
 */
@Test
public void prepareDataSourceWithDatabaseInfo() throws SQLException {
    // Mock data source properties
    final Properties properties = new Properties();
    properties.put("PGDBNAME", "kylo");
    properties.put("PGHOST", "server");
    properties.put("PGPORT", "5000");
    final DataSourceProperties dataSource = new DataSourceProperties("user", "password", "jdbc:postgresql:");
    dataSource.setProperties(properties);
    // Test preparing data source with existing catalog
    final JdbcSchemaParser parser = new PostgreSqlSchemaParser();
    final DataSourceProperties prepared = parser.prepareDataSource(dataSource, null);
    Assert.assertEquals(dataSource, prepared);
    Assert.assertEquals("kylo", prepared.getProperties().getProperty("PGDBNAME"));
    Assert.assertEquals(dataSource.getUrl(), prepared.getUrl());
}
Also used : JdbcSchemaParser(com.thinkbiganalytics.discovery.schema.JdbcSchemaParser) Properties(java.util.Properties) DataSourceProperties(com.thinkbiganalytics.db.DataSourceProperties) DataSourceProperties(com.thinkbiganalytics.db.DataSourceProperties) Test(org.junit.Test)

Example 3 with DataSourceProperties

use of com.thinkbiganalytics.db.DataSourceProperties in project kylo by Teradata.

the class PostgreSqlSchemaParserTest method prepareDataSourceWithBasicInfo.

/**
 * Verify updating data source info with a catalog name.
 */
@Test
public void prepareDataSourceWithBasicInfo() throws SQLException {
    // Mock data source properties
    final Properties properties = new Properties();
    properties.put("PGHOST", "server");
    properties.put("PGPORT", "5000");
    final DataSourceProperties dataSource = new DataSourceProperties("user", "password", "jdbc:postgresql:");
    dataSource.setProperties(properties);
    // Test preparing data source with no catalog
    final JdbcSchemaParser parser = new PostgreSqlSchemaParser();
    DataSourceProperties prepared = parser.prepareDataSource(dataSource, null);
    Assert.assertNotEquals(dataSource, prepared);
    Assert.assertEquals("postgres", prepared.getProperties().getProperty("PGDBNAME"));
    Assert.assertEquals(dataSource.getUrl(), prepared.getUrl());
    // Test preparing data source with user-defined catalog
    prepared = parser.prepareDataSource(dataSource, "kylo");
    Assert.assertNotEquals(dataSource, prepared);
    Assert.assertEquals("kylo", prepared.getProperties().getProperty("PGDBNAME"));
    Assert.assertEquals(dataSource.getUrl(), prepared.getUrl());
}
Also used : JdbcSchemaParser(com.thinkbiganalytics.discovery.schema.JdbcSchemaParser) Properties(java.util.Properties) DataSourceProperties(com.thinkbiganalytics.db.DataSourceProperties) DataSourceProperties(com.thinkbiganalytics.db.DataSourceProperties) Test(org.junit.Test)

Example 4 with DataSourceProperties

use of com.thinkbiganalytics.db.DataSourceProperties in project kylo by Teradata.

the class DefaultCatalogTableManager method getDataSourceProperties.

/**
 * Creates the properties for a data source from the specified template.
 */
@Nonnull
private DataSourceProperties getDataSourceProperties(@Nonnull final DataSetTemplate template, @Nonnull final ClassLoader classLoader) {
    // Extract options from template
    final String user = template.getOptions().get("user");
    final String password = template.getOptions().get("password");
    final String url = template.getOptions().get("url");
    final String driverClassName = template.getOptions().get("driver");
    final String validationQuery = DatabaseType.fromJdbcConnectionString(url).getValidationQuery();
    final Properties dbProps = new Properties();
    dbProps.putAll(template.getOptions());
    // Create data source properties
    final DataSourceProperties dsProps = new DataSourceProperties(user, password, url, driverClassName, StringUtils.isNotEmpty(validationQuery), validationQuery);
    dsProps.setDriverClassLoader(classLoader);
    dsProps.setProperties(dbProps);
    return dsProps;
}
Also used : DataSourceProperties(com.thinkbiganalytics.db.DataSourceProperties) Properties(java.util.Properties) DataSourceProperties(com.thinkbiganalytics.db.DataSourceProperties) Nonnull(javax.annotation.Nonnull)

Example 5 with DataSourceProperties

use of com.thinkbiganalytics.db.DataSourceProperties in project kylo by Teradata.

the class PostgreSqlSchemaParser method prepareDataSource.

@Nonnull
@Override
public DataSourceProperties prepareDataSource(@Nonnull final DataSourceProperties properties, @Nullable final String catalog) throws SQLException {
    // Verify parameters
    final Properties info = (properties.getProperties() != null) ? properties.getProperties() : new Properties();
    final String url = properties.getUrl();
    if (!acceptsURL(url)) {
        throw new SQLException("URL not supported: " + url);
    }
    // Add catalog to properties
    if (url.startsWith(PROTOCOL + "//")) {
        final String[] urlSplit = url.split("\\?", 2);
        final String[] pathSplit = urlSplit[0].substring(PROTOCOL.length() + 2).split("/", 2);
        if (pathSplit.length == 1 || StringUtils.equalsAny(pathSplit[1], "", "/")) {
            // Append catalog to URL
            String catalogUrl = PROTOCOL + "//" + pathSplit[0] + "/" + (catalog != null ? urlEncode(catalog) : POSTGRES);
            if (urlSplit.length == 2) {
                catalogUrl += "?" + urlSplit[1];
            }
            // Clone data source with new URL
            final DataSourceProperties catalogProperties = new DataSourceProperties(properties);
            catalogProperties.setUrl(catalogUrl);
            return catalogProperties;
        } else {
            // Use existing database name
            return properties;
        }
    } else if (StringUtils.isEmpty(info.getProperty("PGDBNAME"))) {
        // Clone data source with new database name
        final Properties catalogInfo = new Properties(info);
        catalogInfo.setProperty("PGDBNAME", (catalog != null ? catalog : POSTGRES));
        final DataSourceProperties catalogProperties = new DataSourceProperties(properties);
        catalogProperties.setProperties(catalogInfo);
        return catalogProperties;
    } else {
        // Use existing database name
        return properties;
    }
}
Also used : SQLException(java.sql.SQLException) Properties(java.util.Properties) DataSourceProperties(com.thinkbiganalytics.db.DataSourceProperties) DataSourceProperties(com.thinkbiganalytics.db.DataSourceProperties) Nonnull(javax.annotation.Nonnull)

Aggregations

DataSourceProperties (com.thinkbiganalytics.db.DataSourceProperties)7 JdbcSchemaParser (com.thinkbiganalytics.discovery.schema.JdbcSchemaParser)5 Properties (java.util.Properties)4 Test (org.junit.Test)4 Nonnull (javax.annotation.Nonnull)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 KerberosTicketConfiguration (com.thinkbiganalytics.kerberos.KerberosTicketConfiguration)1 HadoopClassLoader (com.thinkbiganalytics.kylo.util.HadoopClassLoader)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Configuration (org.apache.hadoop.conf.Configuration)1