Search in sources :

Example 1 with RdbmsMetastoreException

use of org.apache.drill.metastore.rdbms.exception.RdbmsMetastoreException in project drill by apache.

the class RdbmsMetastore method dataSource.

/**
 * Prepares database before initializing data source based on its type,
 * initializes {@link HikariDataSource} instance and configures it based on given
 * Metastore configuration.
 * Basic parameters such as driver, url, user name and password are set using setters.
 * Other source parameters are set dynamically through the properties. See the list
 * of available Hikari properties: <a href="https://github.com/brettwooldridge/HikariCP">.
 *
 * @param config Metastore config
 * @return Hikari data source instance
 * @throws RdbmsMetastoreException if unable to configure Hikari data source
 */
private HikariDataSource dataSource(DrillConfig config) {
    DbHelper.init(config).prepareDatabase();
    try {
        Properties properties = new Properties();
        if (config.hasPath(RdbmsConfigConstants.DATA_SOURCE_PROPERTIES)) {
            Config propertiesConfig = config.getConfig(RdbmsConfigConstants.DATA_SOURCE_PROPERTIES);
            propertiesConfig.entrySet().forEach(e -> properties.put(e.getKey(), e.getValue().unwrapped()));
        }
        HikariConfig hikariConfig = new HikariConfig(properties);
        hikariConfig.setDriverClassName(config.getString(RdbmsConfigConstants.DATA_SOURCE_DRIVER));
        hikariConfig.setJdbcUrl(config.getString(RdbmsConfigConstants.DATA_SOURCE_URL));
        if (config.hasPath(RdbmsConfigConstants.DATA_SOURCE_USER_NAME)) {
            hikariConfig.setUsername(config.getString(RdbmsConfigConstants.DATA_SOURCE_USER_NAME));
        }
        if (config.hasPath(RdbmsConfigConstants.DATA_SOURCE_PASSWORD)) {
            hikariConfig.setPassword(config.getString(RdbmsConfigConstants.DATA_SOURCE_PASSWORD));
        }
        return new HikariDataSource(hikariConfig);
    } catch (RuntimeException e) {
        throw new RdbmsMetastoreException("Unable to init RDBMS Metastore data source: " + e.getMessage(), e);
    }
}
Also used : RdbmsMetastoreException(org.apache.drill.metastore.rdbms.exception.RdbmsMetastoreException) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Config(com.typesafe.config.Config) HikariConfig(com.zaxxer.hikari.HikariConfig) DrillConfig(org.apache.drill.common.config.DrillConfig) Properties(java.util.Properties) HikariConfig(com.zaxxer.hikari.HikariConfig)

Example 2 with RdbmsMetastoreException

use of org.apache.drill.metastore.rdbms.exception.RdbmsMetastoreException in project drill by apache.

the class RdbmsRead method internalExecute.

@Override
protected List<T> internalExecute() {
    Set<MetadataMapper<T, ? extends Record>> mappers = context.transformer().toMappers(metadataTypes);
    if (mappers.isEmpty()) {
        return Collections.emptyList();
    }
    List<T> units = new ArrayList<>();
    try (DSLContext executor = context.executorProvider().executor()) {
        for (MetadataMapper<T, ? extends Record> mapper : mappers) {
            Condition condition = mapper.toCondition(filter);
            logger.debug("Query data from RDBMS Metastore table {} using condition: {}", mapper.table(), condition);
            List<Field<?>> fields = columns.isEmpty() ? Arrays.asList(mapper.table().fields()) : mapper.toFields(columns);
            try {
                if (fields.isEmpty()) {
                    units.addAll(countRecords(executor, mapper, condition));
                } else {
                    units.addAll(queryRecords(executor, mapper, fields, condition));
                }
            } catch (DataAccessException e) {
                throw new RdbmsMetastoreException("Error when reading data from Metastore: " + e.getMessage(), e);
            }
        }
        return units;
    }
}
Also used : Condition(org.jooq.Condition) ArrayList(java.util.ArrayList) DSLContext(org.jooq.DSLContext) MetadataMapper(org.apache.drill.metastore.rdbms.transform.MetadataMapper) Field(org.jooq.Field) RdbmsMetastoreException(org.apache.drill.metastore.rdbms.exception.RdbmsMetastoreException) Record(org.jooq.Record) DataAccessException(org.jooq.exception.DataAccessException)

Example 3 with RdbmsMetastoreException

use of org.apache.drill.metastore.rdbms.exception.RdbmsMetastoreException in project drill by apache.

the class AbstractTransformer method toOverwrite.

protected RdbmsOperation.Overwrite toOverwrite(String metadataTypeString, List<T> units) {
    MetadataType metadataType = MetadataType.fromValue(metadataTypeString);
    if (metadataType == null) {
        throw new RdbmsMetastoreException("Metadata type must be specified during insert / update");
    } else {
        MetadataMapper<T, ? extends Record> mapper = toMapper(metadataType);
        List<Condition> deleteConditions = mapper.toDeleteConditions(units);
        List<? extends Record> records = units.stream().map(mapper::toRecord).collect(Collectors.toList());
        return new RdbmsOperation.Overwrite(mapper.table(), deleteConditions, records);
    }
}
Also used : Condition(org.jooq.Condition) RdbmsMetastoreException(org.apache.drill.metastore.rdbms.exception.RdbmsMetastoreException) MetadataType(org.apache.drill.metastore.metadata.MetadataType)

Example 4 with RdbmsMetastoreException

use of org.apache.drill.metastore.rdbms.exception.RdbmsMetastoreException in project drill by apache.

the class TestTablesTransformer method testToOverwriteAbsentMetadataType.

@Test
public void testToOverwriteAbsentMetadataType() {
    TableMetadataUnit basicUnit = TestData.basicTableMetadataUnit();
    List<TableMetadataUnit> units = Arrays.asList(basicUnit.toBuilder().metadataType(MetadataType.TABLE.name()).build(), basicUnit.toBuilder().metadataType(MetadataType.VIEW.name()).build());
    try {
        TRANSFORMER.toOverwrite(units);
        fail();
    } catch (RdbmsMetastoreException e) {
        assertThat(e.getMessage(), startsWith("Metadata mapper is absent for type"));
    }
}
Also used : RdbmsMetastoreException(org.apache.drill.metastore.rdbms.exception.RdbmsMetastoreException) TableMetadataUnit(org.apache.drill.metastore.components.tables.TableMetadataUnit) Test(org.junit.Test) RdbmsBaseTest(org.apache.drill.metastore.rdbms.RdbmsBaseTest)

Example 5 with RdbmsMetastoreException

use of org.apache.drill.metastore.rdbms.exception.RdbmsMetastoreException in project drill by apache.

the class RdbmsMetastore method initTables.

/**
 * Initializes RDBMS Metastore tables structure based on {@link #LIQUIBASE_CHANGELOG_FILE} file.
 * See <a href="https://www.liquibase.org/documentation/core-concepts/index.html"> for more details.
 *
 * @param dataSource data source
 */
private void initTables(DataSource dataSource) {
    try (Connection connection = dataSource.getConnection()) {
        JdbcConnection jdbcConnection = new JdbcConnection(connection);
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);
        ClassLoaderResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor();
        try (Liquibase liquibase = new Liquibase(LIQUIBASE_CHANGELOG_FILE, resourceAccessor, database)) {
            liquibase.update("");
        }
    } catch (Exception e) {
        throw new RdbmsMetastoreException("Unable to init Metastore tables using Liquibase: " + e.getMessage(), e);
    }
}
Also used : Liquibase(liquibase.Liquibase) RdbmsMetastoreException(org.apache.drill.metastore.rdbms.exception.RdbmsMetastoreException) Connection(java.sql.Connection) JdbcConnection(liquibase.database.jvm.JdbcConnection) Database(liquibase.database.Database) JdbcConnection(liquibase.database.jvm.JdbcConnection) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) RdbmsMetastoreException(org.apache.drill.metastore.rdbms.exception.RdbmsMetastoreException)

Aggregations

RdbmsMetastoreException (org.apache.drill.metastore.rdbms.exception.RdbmsMetastoreException)6 Condition (org.jooq.Condition)2 DSLContext (org.jooq.DSLContext)2 Config (com.typesafe.config.Config)1 HikariConfig (com.zaxxer.hikari.HikariConfig)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 Liquibase (liquibase.Liquibase)1 Database (liquibase.database.Database)1 JdbcConnection (liquibase.database.jvm.JdbcConnection)1 ClassLoaderResourceAccessor (liquibase.resource.ClassLoaderResourceAccessor)1 DrillConfig (org.apache.drill.common.config.DrillConfig)1 TableMetadataUnit (org.apache.drill.metastore.components.tables.TableMetadataUnit)1 MetadataType (org.apache.drill.metastore.metadata.MetadataType)1 RdbmsBaseTest (org.apache.drill.metastore.rdbms.RdbmsBaseTest)1 MetadataMapper (org.apache.drill.metastore.rdbms.transform.MetadataMapper)1 Field (org.jooq.Field)1 Record (org.jooq.Record)1