Search in sources :

Example 1 with CatalogTableSchema

use of com.thinkbiganalytics.discovery.model.CatalogTableSchema in project kylo by Teradata.

the class DataSourceController method createJdbcTableDataSet.

/**
 * Gets the schema of the specified table using the specified data source.
 *
 * @param dataSourceId the data source id
 * @param tableName    the table name
 * @param schema       the schema name, or {@code null} to search all schemas
 * @return the table and field details
 */
@POST
@Path("{id}/tables/{tableName}/dataset")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Gets the schema of the specified table.", notes = "Connects to the database specified by the data source.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the table schema.", response = DataSetWithTableSchema.class), @ApiResponse(code = 403, message = "Access denied.", response = RestResponseStatus.class), @ApiResponse(code = 404, message = "A JDBC data source with that id does not exist.", response = RestResponseStatus.class), @ApiResponse(code = 500, message = "NiFi or the database are unavailable.", response = RestResponseStatus.class) })
public Response createJdbcTableDataSet(@PathParam("id") final String dataSourceId, @PathParam("tableName") final String tableName, @QueryParam("schema") final String schema) {
    // TODO Verify user has access to data source
    // Require admin permission if the results should include unencrypted credentials.
    final boolean encryptCredentials = true;
    accessController.checkPermission(AccessController.SERVICES, encryptCredentials ? FeedServicesAccessControl.ACCESS_DATASOURCES : FeedServicesAccessControl.ADMIN_DATASOURCES);
    DataSetWithTableSchema dataSetWithTableSchema = null;
    boolean hasAccess = false;
    try {
        // ensure the user can read teh datasource
        DataSource encryptedSource = metadataService.read(() -> {
            return findDataSource(dataSourceId, true);
        });
        hasAccess = encryptedSource != null;
        if (hasAccess) {
            // fetch the datasource as a service account to get the creds.
            DataSource dataSource = metadataService.read(() -> {
                return findDataSource(dataSourceId, false);
            }, MetadataAccess.SERVICE);
            // describe the datasource and table
            CatalogTableSchema tableSchema = tableManager.describeTable(dataSource, schema, tableName);
            if (tableSchema != null) {
                DataSet dataSet = new DataSet();
                // assign the datasource to this dataset with encrypted credentials
                dataSet.setDataSource(encryptedSource);
                String fullTableName = (tableSchema.getTable() != null) ? tableSchema.getTable().getQualifiedIdentifier() : HiveUtils.quoteIdentifier(tableSchema.getSchemaName(), tableSchema.getName());
                dataSet.setTitle(tableSchema.getSchemaName() + "." + tableSchema.getName());
                DefaultDataSetTemplate defaultDataSetTemplate = DataSetUtil.mergeTemplates(dataSet);
                List<String> paths = defaultDataSetTemplate.getPaths();
                String format = defaultDataSetTemplate.getFormat();
                Map<String, String> options = defaultDataSetTemplate.getOptions();
                if (options == null) {
                    options = new HashMap<>();
                }
                if ("hive".equalsIgnoreCase(format.toLowerCase())) {
                    if (paths == null) {
                        paths = new ArrayList<>();
                    }
                    paths.add(fullTableName);
                }
                options.put("dbtable", fullTableName);
                dataSet.setFormat(format);
                dataSet.setPaths(paths);
                dataSet.setOptions(options);
                DataSet dataSet1 = dataSetService.findOrCreateDataSet(dataSet, encryptCredentials);
                dataSetWithTableSchema = new DataSetWithTableSchema(dataSet1, tableSchema);
            } else {
                if (log.isErrorEnabled()) {
                    log.error("Failed to describe tables for schema [" + schema + "], table [" + tableName + "], dataSource [" + dataSourceId + "] ");
                }
                final RestResponseStatus status = new RestResponseStatus.ResponseStatusBuilder().message(getMessage("catalog.datasource.describeTable.error", tableName, schema)).url(request.getRequestURI()).buildError();
                throw new InternalServerErrorException(Response.serverError().entity(status).build());
            }
        // });
        } else {
            // no acceess
            final RestResponseStatus status = new RestResponseStatus.ResponseStatusBuilder().message(getMessage("catalog.datasource.forbidden")).url(request.getRequestURI()).buildError();
            throw new InternalServerErrorException(Response.serverError().entity(status).build());
        }
    } catch (Exception e) {
        if (exceptionTransformer.causesInChain(e)) {
            throw new ThriftConnectionException(e);
        }
        final RestResponseStatus status = new RestResponseStatus.ResponseStatusBuilder().message(getMessage("catalog.datasource.describeTable.error", tableName, schema)).url(request.getRequestURI()).buildError();
        throw new InternalServerErrorException(Response.serverError().entity(status).build());
    }
    return Response.ok(dataSetWithTableSchema).build();
}
Also used : DataSet(com.thinkbiganalytics.kylo.catalog.rest.model.DataSet) TTransportException(org.apache.thrift.transport.TTransportException) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) AccessDeniedException(java.nio.file.AccessDeniedException) SQLException(java.sql.SQLException) ThriftConnectionException(com.thinkbiganalytics.hive.exceptions.ThriftConnectionException) CatalogException(com.thinkbiganalytics.kylo.catalog.CatalogException) PotentialControllerServiceConflictException(com.thinkbiganalytics.kylo.catalog.datasource.PotentialControllerServiceConflictException) ForbiddenException(javax.ws.rs.ForbiddenException) DataSourceAlreadyExistsException(com.thinkbiganalytics.metadata.api.catalog.DataSourceAlreadyExistsException) DataSource(com.thinkbiganalytics.kylo.catalog.rest.model.DataSource) DataSetWithTableSchema(com.thinkbiganalytics.kylo.catalog.rest.model.DataSetWithTableSchema) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) ThriftConnectionException(com.thinkbiganalytics.hive.exceptions.ThriftConnectionException) CatalogTableSchema(com.thinkbiganalytics.discovery.model.CatalogTableSchema) DefaultDataSetTemplate(com.thinkbiganalytics.kylo.catalog.rest.model.DefaultDataSetTemplate) RestResponseStatus(com.thinkbiganalytics.rest.model.RestResponseStatus) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with CatalogTableSchema

use of com.thinkbiganalytics.discovery.model.CatalogTableSchema in project kylo by Teradata.

the class DefaultCatalogTableManager method describeTable.

@Nonnull
@Override
public CatalogTableSchema describeTable(@Nonnull final DataSource dataSource, @Nullable final String schemaName, @Nullable final String tableName) throws SQLException {
    final DataSetTemplate template = DataSourceUtil.mergeTemplates(dataSource);
    if (Objects.equals("hive", template.getFormat())) {
        final TableSchema tableSchema = hiveMetastoreService.getTable(schemaName, tableName);
        final CatalogTableSchema catalogTableSchema = new CatalogTableSchema(tableSchema);
        // Get table metadata
        if (StringUtils.isNotEmpty(tableSchema.getName())) {
            final DefaultJdbcTable jdbcTable = new DefaultJdbcTable(tableSchema.getName(), "TABLE");
            jdbcTable.setCatalog(tableSchema.getDatabaseName());
            jdbcTable.setCatalog(tableSchema.getDatabaseName());
            jdbcTable.setRemarks(tableSchema.getDescription());
            jdbcTable.setSchema(tableSchema.getSchemaName());
            jdbcTable.setCatalogSeparator(".");
            jdbcTable.setIdentifierQuoteString("`");
            catalogTableSchema.setTable(createTable(jdbcTable));
        }
        return catalogTableSchema;
    } else if (Objects.equals("jdbc", template.getFormat())) {
        return isolatedFunction(template, schemaName, (connection, schemaParser) -> {
            final javax.sql.DataSource ds = new SingleConnectionDataSource(connection, true);
            final DBSchemaParser tableSchemaParser = new DBSchemaParser(ds, new KerberosTicketConfiguration());
            final TableSchema tableSchema = tableSchemaParser.describeTable(schemaName, tableName);
            if (tableSchema != null) {
                // Get table metadata
                final DefaultJdbcTable jdbcTable = new DefaultJdbcTable(tableSchema.getName(), "TABLE");
                jdbcTable.setCatalog(tableSchema.getDatabaseName());
                jdbcTable.setCatalog(tableSchema.getDatabaseName());
                jdbcTable.setRemarks(tableSchema.getDescription());
                jdbcTable.setSchema(tableSchema.getSchemaName());
                jdbcTable.setMetaData(connection.getMetaData());
                // Return table schema
                final CatalogTableSchema catalogTableSchema = new CatalogTableSchema(tableSchema);
                catalogTableSchema.setTable(createTable(jdbcTable));
                return catalogTableSchema;
            } else {
                return null;
            }
        });
    } else {
        throw new IllegalArgumentException("Unsupported format: " + template.getFormat());
    }
}
Also used : PoolingDataSourceService(com.thinkbiganalytics.db.PoolingDataSourceService) DatabaseType(com.thinkbiganalytics.jdbc.util.DatabaseType) LoadingCache(com.google.common.cache.LoadingCache) Connection(java.sql.Connection) DataSourceUtil(com.thinkbiganalytics.kylo.catalog.datasource.DataSourceUtil) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) DataSourceProperties(com.thinkbiganalytics.db.DataSourceProperties) DataSetUtil(com.thinkbiganalytics.kylo.catalog.dataset.DataSetUtil) StringUtils(org.apache.commons.lang3.StringUtils) DataSetTemplate(com.thinkbiganalytics.kylo.catalog.rest.model.DataSetTemplate) Value(org.springframework.beans.factory.annotation.Value) SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) DBSchemaParser(com.thinkbiganalytics.schema.DBSchemaParser) DataSource(com.thinkbiganalytics.kylo.catalog.rest.model.DataSource) Configuration(org.apache.hadoop.conf.Configuration) Qualifier(org.springframework.beans.factory.annotation.Qualifier) CatalogTableSchema(com.thinkbiganalytics.discovery.model.CatalogTableSchema) HiveMetastoreService(com.thinkbiganalytics.hive.service.HiveMetastoreService) DefaultJdbcTable(com.thinkbiganalytics.schema.DefaultJdbcTable) Nonnull(javax.annotation.Nonnull) JdbcSchemaParser(com.thinkbiganalytics.discovery.schema.JdbcSchemaParser) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) Properties(java.util.Properties) Set(java.util.Set) JdbcCatalog(com.thinkbiganalytics.discovery.schema.JdbcCatalog) HadoopClassLoader(com.thinkbiganalytics.kylo.util.HadoopClassLoader) Collectors(java.util.stream.Collectors) JdbcSchemaParserProvider(com.thinkbiganalytics.schema.JdbcSchemaParserProvider) KerberosUtil(com.thinkbiganalytics.kerberos.KerberosUtil) CacheLoader(com.google.common.cache.CacheLoader) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Component(org.springframework.stereotype.Component) List(java.util.List) JdbcSchema(com.thinkbiganalytics.discovery.schema.JdbcSchema) TableSchema(com.thinkbiganalytics.discovery.schema.TableSchema) KerberosTicketConfiguration(com.thinkbiganalytics.kerberos.KerberosTicketConfiguration) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CacheBuilder(com.google.common.cache.CacheBuilder) JdbcTable(com.thinkbiganalytics.discovery.schema.JdbcTable) DataSetTable(com.thinkbiganalytics.kylo.catalog.rest.model.DataSetTable) SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) DBSchemaParser(com.thinkbiganalytics.schema.DBSchemaParser) CatalogTableSchema(com.thinkbiganalytics.discovery.model.CatalogTableSchema) TableSchema(com.thinkbiganalytics.discovery.schema.TableSchema) DataSetTemplate(com.thinkbiganalytics.kylo.catalog.rest.model.DataSetTemplate) DefaultJdbcTable(com.thinkbiganalytics.schema.DefaultJdbcTable) KerberosTicketConfiguration(com.thinkbiganalytics.kerberos.KerberosTicketConfiguration) CatalogTableSchema(com.thinkbiganalytics.discovery.model.CatalogTableSchema) SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) DataSource(com.thinkbiganalytics.kylo.catalog.rest.model.DataSource) Nonnull(javax.annotation.Nonnull)

Aggregations

CatalogTableSchema (com.thinkbiganalytics.discovery.model.CatalogTableSchema)2 DataSource (com.thinkbiganalytics.kylo.catalog.rest.model.DataSource)2 SQLException (java.sql.SQLException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 DataSourceProperties (com.thinkbiganalytics.db.DataSourceProperties)1 PoolingDataSourceService (com.thinkbiganalytics.db.PoolingDataSourceService)1 JdbcCatalog (com.thinkbiganalytics.discovery.schema.JdbcCatalog)1 JdbcSchema (com.thinkbiganalytics.discovery.schema.JdbcSchema)1 JdbcSchemaParser (com.thinkbiganalytics.discovery.schema.JdbcSchemaParser)1 JdbcTable (com.thinkbiganalytics.discovery.schema.JdbcTable)1 TableSchema (com.thinkbiganalytics.discovery.schema.TableSchema)1 ThriftConnectionException (com.thinkbiganalytics.hive.exceptions.ThriftConnectionException)1 HiveMetastoreService (com.thinkbiganalytics.hive.service.HiveMetastoreService)1 DatabaseType (com.thinkbiganalytics.jdbc.util.DatabaseType)1 KerberosTicketConfiguration (com.thinkbiganalytics.kerberos.KerberosTicketConfiguration)1 KerberosUtil (com.thinkbiganalytics.kerberos.KerberosUtil)1 CatalogException (com.thinkbiganalytics.kylo.catalog.CatalogException)1