Search in sources :

Example 1 with DatabaseType

use of com.thinkbiganalytics.jdbc.util.DatabaseType in project kylo by Teradata.

the class DBCPConnectionPoolService method generatePreviewQueryForDatasource.

/**
 * Generates a preview query on a table that is appropriate for the type of specified data source.
 *
 * @param datasource the JDBC datasource
 * @param schema the schema determining the SQL dialect, or null/empty for the default dialect
 * @param tableName the name of the table
 * @param limit the result size
 * @return the query string
 * @throws IllegalArgumentException if the datasource is invalid
 */
@Nonnull
public String generatePreviewQueryForDatasource(@Nonnull final JdbcDatasource datasource, @Nonnull final String schema, @Nonnull final String tableName, final int limit) {
    final Optional<ControllerServiceDTO> controllerService = Optional.ofNullable(datasource.getControllerServiceId()).map(id -> getControllerService(id, null));
    if (controllerService.isPresent()) {
        final DatabaseType dbType = DatabaseType.fromJdbcConnectionString(datasource.getDatabaseConnectionUrl());
        final ExecuteQueryControllerServiceRequest serviceProperties = new ExecuteQueryControllerServiceRequestBuilder(controllerService.get()).previewQuery(dbType, schema, tableName, limit).build();
        return serviceProperties.getQuery();
    } else {
        log.error("Cannot execute query for datasource: {}", datasource);
        throw new IllegalArgumentException("Missing controller service for datasource: " + datasource);
    }
}
Also used : ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) DatabaseType(com.thinkbiganalytics.jdbc.util.DatabaseType) ExecuteQueryControllerServiceRequestBuilder(com.thinkbiganalytics.feedmgr.nifi.controllerservice.ExecuteQueryControllerServiceRequest.ExecuteQueryControllerServiceRequestBuilder) Nonnull(javax.annotation.Nonnull)

Example 2 with DatabaseType

use of com.thinkbiganalytics.jdbc.util.DatabaseType in project kylo by Teradata.

the class DBCPConnectionPoolService method executePreviewQueryForControllerService.

/**
 * Executes preview query on a table, in the context of the specified controller service, based
 * on the supplied schema (if provided) and limit.
 *
 * @param serviceId   a NiFi controller service id
 * @param serviceName a NiFi controller service name
 * @param tableName the name of the table
 * @param schema the schema determining the SQL dialect, or null/empty for the default dialect
 * @param limit the result size
 * @return the query results
 * @throws DataAccessException      if the query cannot be executed
 * @throws IllegalArgumentException if the controller service cannot be found, the schema/tableName is empty, or the limit is less than zero
 */
public QueryResult executePreviewQueryForControllerService(final String serviceId, final String serviceName, @Nonnull final String schema, @Nonnull final String tableName, final int limit) {
    Validate.notEmpty(schema, "No schema provided");
    Validate.notEmpty(tableName, "No table name provided");
    Validate.isTrue(limit >= 0, "The query result size must be greater than or equal to 0");
    final ControllerServiceDTO controllerService = getControllerService(serviceId, serviceName);
    if (controllerService != null) {
        final ExecuteQueryControllerServiceRequest serviceProperties = new ExecuteQueryControllerServiceRequestBuilder(controllerService).build();
        final PoolingDataSourceService.DataSourceProperties dataSourceProperties = getDataSourceProperties(serviceProperties);
        final DatabaseType dbType = DatabaseType.fromJdbcConnectionString(dataSourceProperties.getUrl());
        final ExecuteQueryControllerServiceRequest previewRequest = new ExecuteQueryControllerServiceRequestBuilder(controllerService).using(serviceProperties).previewQuery(dbType, schema, tableName, limit).build();
        return executeQueryForControllerService(dataSourceProperties, previewRequest);
    } else {
        log.error("Cannot execute query for controller service. Unable to obtain controller service: {}, {}", serviceId, serviceName);
        throw new IllegalArgumentException("Not a valid controller service: " + serviceId + ", " + serviceName);
    }
}
Also used : ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) PoolingDataSourceService(com.thinkbiganalytics.db.PoolingDataSourceService) DatabaseType(com.thinkbiganalytics.jdbc.util.DatabaseType) ExecuteQueryControllerServiceRequestBuilder(com.thinkbiganalytics.feedmgr.nifi.controllerservice.ExecuteQueryControllerServiceRequest.ExecuteQueryControllerServiceRequestBuilder)

Example 3 with DatabaseType

use of com.thinkbiganalytics.jdbc.util.DatabaseType in project kylo by Teradata.

the class DBCPConnectionPoolService method parseValidationQueryFromConnectionString.

/**
 * get the validation query from the db name that is parsed from the
 */
private String parseValidationQueryFromConnectionString(String connectionString) {
    String validationQuery = null;
    try {
        if (StringUtils.isNotBlank(connectionString)) {
            DatabaseType databaseType = DatabaseType.fromJdbcConnectionString(connectionString);
            validationQuery = databaseType.getValidationQuery();
        }
    } catch (IllegalArgumentException e) {
    // if we cant find it in the map its ok.
    }
    return validationQuery;
}
Also used : DatabaseType(com.thinkbiganalytics.jdbc.util.DatabaseType)

Example 4 with DatabaseType

use of com.thinkbiganalytics.jdbc.util.DatabaseType in project kylo by Teradata.

the class DataSourceSupplier method get.

@Override
public DataSource get() {
    if (cache == null) {
        // Get validation query
        final DatabaseType databaseType = DatabaseType.fromJdbcConnectionString(config.getDatabaseConnectionUrl());
        final String validationQuery = databaseType.getValidationQuery();
        // Build data source
        final PoolingDataSourceService.DataSourceProperties dataSourceProperties = new PoolingDataSourceService.DataSourceProperties(config.getDatabaseUser(), config.getPassword(), config.getDatabaseConnectionUrl(), config.getDatabaseDriverClassName(), (validationQuery != null), validationQuery);
        dataSourceProperties.setDriverLocation(config.getDatabaseDriverLocation());
        cache = PoolingDataSourceService.getDataSource(dataSourceProperties);
    }
    return cache;
}
Also used : PoolingDataSourceService(com.thinkbiganalytics.db.PoolingDataSourceService) DatabaseType(com.thinkbiganalytics.jdbc.util.DatabaseType)

Example 5 with DatabaseType

use of com.thinkbiganalytics.jdbc.util.DatabaseType in project kylo by Teradata.

the class DatasourceController method testConnection.

@POST
@Path("test")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Tests datasource connection")
@ApiResponses({ @ApiResponse(code = 200, message = "Connection was tested, test failure will appear in message, successful connections return no message", response = RestResponseStatus.class), @ApiResponse(code = 400, message = "UserDatasource is not a JdbcDatasource", response = RestResponseStatus.class) })
public Response testConnection(@Nonnull final UserDatasource datasource) {
    return metadata.commit(() -> {
        accessController.checkPermission(AccessController.SERVICES, FeedServicesAccessControl.EDIT_DATASOURCES);
        if (datasource instanceof JdbcDatasource) {
            JdbcDatasource jdbcDatasource = (JdbcDatasource) datasource;
            DatabaseType databaseType = DatabaseType.fromJdbcConnectionString(jdbcDatasource.getDatabaseConnectionUrl());
            String query = databaseType.getValidationQuery();
            try {
                dbcpConnectionPoolTableInfo.testConnectionForDatasource(jdbcDatasource, query);
                return Response.ok().build();
            } catch (CannotGetJdbcConnectionException e) {
                Map<String, String> message = new HashMap<>(1);
                message.put("message", e.getRootCause().toString());
                return Response.ok(message).build();
            }
        } else {
            return Response.status(Response.Status.BAD_REQUEST).build();
        }
    });
}
Also used : JdbcDatasource(com.thinkbiganalytics.metadata.rest.model.data.JdbcDatasource) DatabaseType(com.thinkbiganalytics.jdbc.util.DatabaseType) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) Map(java.util.Map) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

DatabaseType (com.thinkbiganalytics.jdbc.util.DatabaseType)8 PoolingDataSourceService (com.thinkbiganalytics.db.PoolingDataSourceService)5 ExecuteQueryControllerServiceRequestBuilder (com.thinkbiganalytics.feedmgr.nifi.controllerservice.ExecuteQueryControllerServiceRequest.ExecuteQueryControllerServiceRequestBuilder)5 ControllerServiceDTO (org.apache.nifi.web.api.dto.ControllerServiceDTO)5 Nonnull (javax.annotation.Nonnull)3 JdbcDatasource (com.thinkbiganalytics.metadata.rest.model.data.JdbcDatasource)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 QueryResult (com.thinkbiganalytics.discovery.schema.QueryResult)1 TableSchema (com.thinkbiganalytics.discovery.schema.TableSchema)1 NifiControllerServiceProperties (com.thinkbiganalytics.feedmgr.nifi.NifiControllerServiceProperties)1 DescribeTableControllerServiceRequestBuilder (com.thinkbiganalytics.feedmgr.nifi.controllerservice.DescribeTableControllerServiceRequest.DescribeTableControllerServiceRequestBuilder)1 DatasourceModelTransform (com.thinkbiganalytics.feedmgr.service.datasource.DatasourceModelTransform)1 KerberosTicketConfiguration (com.thinkbiganalytics.kerberos.KerberosTicketConfiguration)1 MetadataAccess (com.thinkbiganalytics.metadata.api.MetadataAccess)1 DatasourceProvider (com.thinkbiganalytics.metadata.api.datasource.DatasourceProvider)1 Datasource (com.thinkbiganalytics.metadata.rest.model.data.Datasource)1 DBSchemaParser (com.thinkbiganalytics.schema.DBSchemaParser)1 QueryRunner (com.thinkbiganalytics.schema.QueryRunner)1 ApiOperation (io.swagger.annotations.ApiOperation)1