Search in sources :

Example 11 with Datasource

use of com.thinkbiganalytics.metadata.rest.model.data.Datasource in project kylo by Teradata.

the class DatasourceController method getTablesAndColumns.

/**
 * Gets the tables and their columns from the specified data source for given schema
 *
 * @param idStr  the data source id
 * @param schema the schema name, or {@code null} for all schemas
 * @return the list of tables and their columns
 */
@GET
@Path("{id}/table-columns")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Gets the tables and their columns from the data source for given schema", notes = "Connects to the database specified by the data source.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns tables and columns", response = String.class, responseContainer = "List"), @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 getTablesAndColumns(@PathParam("id") final String idStr, @QueryParam("schema") final String schema) {
    // Verify user has access to data source
    final Optional<com.thinkbiganalytics.metadata.api.datasource.Datasource.ID> id = metadata.read(() -> {
        accessController.checkPermission(AccessController.SERVICES, FeedServicesAccessControl.ACCESS_DATASOURCES);
        final com.thinkbiganalytics.metadata.api.datasource.Datasource datasource = datasetProvider.getDatasource(datasetProvider.resolve(idStr));
        return Optional.ofNullable(datasource).map(com.thinkbiganalytics.metadata.api.datasource.Datasource::getId);
    });
    // Retrieve table names using system user
    return metadata.read(() -> {
        JdbcDatasource datasource = id.map(datasetProvider::getDatasource).map(ds -> datasourceTransform.toDatasource(ds, DatasourceModelTransform.Level.ADMIN)).filter(JdbcDatasource.class::isInstance).map(JdbcDatasource.class::cast).orElseThrow(() -> new NotFoundException("No JDBC datasource exists with the given ID: " + idStr));
        List<String> tableNamesForDatasource = dbcpConnectionPoolTableInfo.getTableNamesForDatasource(datasource, schema, null);
        List<DatabaseMetadata> tables = new ArrayList<>();
        if (tableNamesForDatasource != null) {
            tables = tableNamesForDatasource.stream().flatMap(schemaNameDotTableName -> {
                String tableName = schemaNameDotTableName.substring(schema.length() + 1);
                TableSchema tableSchema = dbcpConnectionPoolTableInfo.describeTableForDatasource(datasource, schema, tableName);
                return tableSchema.getFields().stream().map(field -> {
                    DefaultDatabaseMetadata meta = new DefaultDatabaseMetadata();
                    meta.setDatabaseName(schema);
                    meta.setColumnName(field.getName());
                    meta.setTableName(tableName);
                    return meta;
                });
            }).collect(Collectors.toList());
        }
        return Response.ok(tables).build();
    }, MetadataAccess.SERVICE);
}
Also used : JdbcDatasource(com.thinkbiganalytics.metadata.rest.model.data.JdbcDatasource) UserDatasource(com.thinkbiganalytics.metadata.rest.model.data.UserDatasource) Datasource(com.thinkbiganalytics.metadata.rest.model.data.Datasource) Arrays(java.util.Arrays) JdbcDatasourceDetails(com.thinkbiganalytics.metadata.api.datasource.JdbcDatasourceDetails) Produces(javax.ws.rs.Produces) DatabaseMetadata(com.thinkbiganalytics.discovery.schema.DatabaseMetadata) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) DatasourceModelTransform(com.thinkbiganalytics.feedmgr.service.datasource.DatasourceModelTransform) Collections2(com.google.common.collect.Collections2) DatasourceDefinition(com.thinkbiganalytics.metadata.rest.model.data.DatasourceDefinition) StringUtils(org.apache.commons.lang3.StringUtils) Model(com.thinkbiganalytics.feedmgr.rest.Model) DatasourceDefinitionProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceDefinitionProvider) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) AccessController(com.thinkbiganalytics.security.AccessController) DefaultValue(javax.ws.rs.DefaultValue) NiFiRestClient(com.thinkbiganalytics.nifi.rest.client.NiFiRestClient) Formatters(com.thinkbiganalytics.Formatters) FeedServicesAccessControl(com.thinkbiganalytics.feedmgr.security.FeedServicesAccessControl) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) DELETE(javax.ws.rs.DELETE) PermissionsChange(com.thinkbiganalytics.security.rest.model.PermissionsChange) ActionGroup(com.thinkbiganalytics.security.rest.model.ActionGroup) Set(java.util.Set) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) List(java.util.List) Principal(java.security.Principal) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) AccessControlException(java.security.AccessControlException) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) RoleMembershipChange(com.thinkbiganalytics.security.rest.model.RoleMembershipChange) PathParam(javax.ws.rs.PathParam) SecurityModelTransform(com.thinkbiganalytics.security.rest.controller.SecurityModelTransform) GET(javax.ws.rs.GET) ApiResponses(io.swagger.annotations.ApiResponses) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) RestResponseStatus(com.thinkbiganalytics.rest.model.RestResponseStatus) DefaultDatabaseMetadata(com.thinkbiganalytics.discovery.model.DefaultDatabaseMetadata) JdbcDatasource(com.thinkbiganalytics.metadata.rest.model.data.JdbcDatasource) UserDatasource(com.thinkbiganalytics.metadata.rest.model.data.UserDatasource) Api(io.swagger.annotations.Api) Tag(io.swagger.annotations.Tag) Nonnull(javax.annotation.Nonnull) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) SecurityService(com.thinkbiganalytics.feedmgr.service.security.SecurityService) AccessControlled(com.thinkbiganalytics.metadata.api.security.AccessControlled) Datasource(com.thinkbiganalytics.metadata.rest.model.data.Datasource) DBCPConnectionPoolService(com.thinkbiganalytics.feedmgr.nifi.controllerservice.DBCPConnectionPoolService) Component(org.springframework.stereotype.Component) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition) ApiResponse(io.swagger.annotations.ApiResponse) TableSchema(com.thinkbiganalytics.discovery.schema.TableSchema) DatasourceProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceProvider) DatasourceCriteria(com.thinkbiganalytics.metadata.rest.model.data.DatasourceCriteria) QueryResult(com.thinkbiganalytics.discovery.schema.QueryResult) DefaultDatabaseMetadata(com.thinkbiganalytics.discovery.model.DefaultDatabaseMetadata) JdbcDatasource(com.thinkbiganalytics.metadata.rest.model.data.JdbcDatasource) TableSchema(com.thinkbiganalytics.discovery.schema.TableSchema) ArrayList(java.util.ArrayList) NotFoundException(javax.ws.rs.NotFoundException) DatabaseMetadata(com.thinkbiganalytics.discovery.schema.DatabaseMetadata) DefaultDatabaseMetadata(com.thinkbiganalytics.discovery.model.DefaultDatabaseMetadata) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 12 with Datasource

use of com.thinkbiganalytics.metadata.rest.model.data.Datasource in project kylo by Teradata.

the class DatasourceController method previewQuery.

/**
 * Executes a query on the specified datasource.
 *
 * @param idStr the datasource id
 * @param query the SQL query
 * @return the SQL result
 */
@POST
@Path("{id}/preview/{schema}/{table}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Executes a preview query appropriate for the type of datasource and returns the result.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the result.", response = QueryResult.class), @ApiResponse(code = 403, message = "Access denied.", response = RestResponseStatus.class), @ApiResponse(code = 400, 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 previewQuery(@PathParam("id") final String idStr, @PathParam("schema") final String schema, @PathParam("table") final String tableName, @QueryParam("limit") @DefaultValue("10") final int limit) {
    // Verify user has access to data source
    final Optional<com.thinkbiganalytics.metadata.api.datasource.Datasource.ID> id = metadata.read(() -> {
        accessController.checkPermission(AccessController.SERVICES, FeedServicesAccessControl.ACCESS_DATASOURCES);
        final com.thinkbiganalytics.metadata.api.datasource.Datasource datasource = datasetProvider.getDatasource(datasetProvider.resolve(idStr));
        return Optional.ofNullable(datasource).map(com.thinkbiganalytics.metadata.api.datasource.Datasource::getId);
    });
    // Execute query
    return metadata.read(() -> {
        final QueryResult result = id.map(datasetProvider::getDatasource).map(ds -> datasourceTransform.toDatasource(ds, DatasourceModelTransform.Level.ADMIN)).filter(JdbcDatasource.class::isInstance).map(JdbcDatasource.class::cast).map(datasource -> dbcpConnectionPoolTableInfo.executePreviewQueryForDatasource(datasource, schema, tableName, limit)).orElseThrow(() -> new NotFoundException("No JDBC datasource exists with the given ID: " + idStr));
        return Response.ok(result).build();
    }, MetadataAccess.SERVICE);
}
Also used : JdbcDatasource(com.thinkbiganalytics.metadata.rest.model.data.JdbcDatasource) UserDatasource(com.thinkbiganalytics.metadata.rest.model.data.UserDatasource) Datasource(com.thinkbiganalytics.metadata.rest.model.data.Datasource) Arrays(java.util.Arrays) JdbcDatasourceDetails(com.thinkbiganalytics.metadata.api.datasource.JdbcDatasourceDetails) Produces(javax.ws.rs.Produces) DatabaseMetadata(com.thinkbiganalytics.discovery.schema.DatabaseMetadata) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) DatasourceModelTransform(com.thinkbiganalytics.feedmgr.service.datasource.DatasourceModelTransform) Collections2(com.google.common.collect.Collections2) DatasourceDefinition(com.thinkbiganalytics.metadata.rest.model.data.DatasourceDefinition) StringUtils(org.apache.commons.lang3.StringUtils) Model(com.thinkbiganalytics.feedmgr.rest.Model) DatasourceDefinitionProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceDefinitionProvider) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) AccessController(com.thinkbiganalytics.security.AccessController) DefaultValue(javax.ws.rs.DefaultValue) NiFiRestClient(com.thinkbiganalytics.nifi.rest.client.NiFiRestClient) Formatters(com.thinkbiganalytics.Formatters) FeedServicesAccessControl(com.thinkbiganalytics.feedmgr.security.FeedServicesAccessControl) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) DELETE(javax.ws.rs.DELETE) PermissionsChange(com.thinkbiganalytics.security.rest.model.PermissionsChange) ActionGroup(com.thinkbiganalytics.security.rest.model.ActionGroup) Set(java.util.Set) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) List(java.util.List) Principal(java.security.Principal) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) AccessControlException(java.security.AccessControlException) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) RoleMembershipChange(com.thinkbiganalytics.security.rest.model.RoleMembershipChange) PathParam(javax.ws.rs.PathParam) SecurityModelTransform(com.thinkbiganalytics.security.rest.controller.SecurityModelTransform) GET(javax.ws.rs.GET) ApiResponses(io.swagger.annotations.ApiResponses) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) RestResponseStatus(com.thinkbiganalytics.rest.model.RestResponseStatus) DefaultDatabaseMetadata(com.thinkbiganalytics.discovery.model.DefaultDatabaseMetadata) JdbcDatasource(com.thinkbiganalytics.metadata.rest.model.data.JdbcDatasource) UserDatasource(com.thinkbiganalytics.metadata.rest.model.data.UserDatasource) Api(io.swagger.annotations.Api) Tag(io.swagger.annotations.Tag) Nonnull(javax.annotation.Nonnull) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) SecurityService(com.thinkbiganalytics.feedmgr.service.security.SecurityService) AccessControlled(com.thinkbiganalytics.metadata.api.security.AccessControlled) Datasource(com.thinkbiganalytics.metadata.rest.model.data.Datasource) DBCPConnectionPoolService(com.thinkbiganalytics.feedmgr.nifi.controllerservice.DBCPConnectionPoolService) Component(org.springframework.stereotype.Component) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition) ApiResponse(io.swagger.annotations.ApiResponse) TableSchema(com.thinkbiganalytics.discovery.schema.TableSchema) DatasourceProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceProvider) DatasourceCriteria(com.thinkbiganalytics.metadata.rest.model.data.DatasourceCriteria) QueryResult(com.thinkbiganalytics.discovery.schema.QueryResult) JdbcDatasource(com.thinkbiganalytics.metadata.rest.model.data.JdbcDatasource) NotFoundException(javax.ws.rs.NotFoundException) QueryResult(com.thinkbiganalytics.discovery.schema.QueryResult) 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 13 with Datasource

use of com.thinkbiganalytics.metadata.rest.model.data.Datasource in project kylo by Teradata.

the class FeedLineage method serialize.

@JsonIgnore
private void serialize(Feed feed) {
    if (feed.getDependentFeeds() != null) {
        Set<String> ids = new HashSet<>();
        Set<Feed> dependentFeeds = new HashSet<>(feed.getDependentFeeds());
        feed.setDependentFeeds(null);
        dependentFeeds.stream().forEach(depFeed -> {
            feedMap.put(depFeed.getId(), depFeed);
            ids.add(depFeed.getId());
            if (depFeed.getDependentFeeds() != null) {
                serialize(depFeed);
            }
        });
        feed.setDependentFeedIds(ids);
    }
    if (feed.getUsedByFeeds() != null) {
        Set<String> ids = new HashSet<>();
        Set<Feed> usedByFeeds = new HashSet<>(feed.getUsedByFeeds());
        feed.getUsedByFeeds().clear();
        usedByFeeds.stream().forEach(depFeed -> {
            feedMap.put(depFeed.getId(), depFeed);
            ids.add(depFeed.getId());
            if (depFeed.getUsedByFeeds() != null) {
                serialize(depFeed);
            }
        });
        feed.setUsedByFeedIds(ids);
    }
    if (feed.getSources() != null) {
        feed.getSources().forEach(feedSource -> {
            Datasource ds = serializeDatasource(feedSource.getDatasource());
            feedSource.setDatasource(null);
            if (StringUtils.isBlank(feedSource.getDatasourceId())) {
                feedSource.setDatasourceId(ds != null ? ds.getId() : null);
            }
        });
    }
    if (feed.getDestinations() != null) {
        feed.getDestinations().forEach(feedDestination -> {
            Datasource ds = serializeDatasource(feedDestination.getDatasource());
            feedDestination.setDatasource(null);
            if (StringUtils.isBlank(feedDestination.getDatasourceId())) {
                feedDestination.setDatasourceId(ds != null ? ds.getId() : null);
            }
        });
    }
    feedMap.put(feed.getId(), feed);
}
Also used : Datasource(com.thinkbiganalytics.metadata.rest.model.data.Datasource) HashSet(java.util.HashSet) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore)

Example 14 with Datasource

use of com.thinkbiganalytics.metadata.rest.model.data.Datasource in project kylo by Teradata.

the class MetadataClientProviderTest method testGetDatasourceByName.

@Test
public void testGetDatasourceByName() {
    this.provider.ensureDirectoryDatasource("test2", "", Paths.get("aaa", "bbb"));
    Datasource ds = this.provider.getDatasourceByName("test2");
    assertThat(ds).isNotNull();
}
Also used : DirectoryDatasource(com.thinkbiganalytics.metadata.rest.model.data.DirectoryDatasource) Datasource(com.thinkbiganalytics.metadata.rest.model.data.Datasource) HiveTableDatasource(com.thinkbiganalytics.metadata.rest.model.data.HiveTableDatasource) Test(org.junit.Test)

Example 15 with Datasource

use of com.thinkbiganalytics.metadata.rest.model.data.Datasource in project kylo by Teradata.

the class MetadataClientProviderTest method testEnsureFeedSource.

@Test
public void testEnsureFeedSource() {
    Feed feed = this.provider.ensureFeed("category", "test3", "");
    Datasource ds = this.provider.ensureDirectoryDatasource("test3", "", Paths.get("aaa", "bbb"));
    feed = this.provider.ensureFeedSource(feed.getId(), ds.getId());
    assertThat(feed.getSources()).hasSize(1);
    feed = this.provider.ensureFeedSource(feed.getId(), ds.getId());
    assertThat(feed.getSources()).hasSize(1);
}
Also used : DirectoryDatasource(com.thinkbiganalytics.metadata.rest.model.data.DirectoryDatasource) Datasource(com.thinkbiganalytics.metadata.rest.model.data.Datasource) HiveTableDatasource(com.thinkbiganalytics.metadata.rest.model.data.HiveTableDatasource) Feed(com.thinkbiganalytics.metadata.rest.model.feed.Feed) FeedExecutedSinceFeed(com.thinkbiganalytics.metadata.api.sla.FeedExecutedSinceFeed) Test(org.junit.Test)

Aggregations

Datasource (com.thinkbiganalytics.metadata.rest.model.data.Datasource)25 DatasourceModelTransform (com.thinkbiganalytics.feedmgr.service.datasource.DatasourceModelTransform)10 MetadataAccess (com.thinkbiganalytics.metadata.api.MetadataAccess)10 DatasourceProvider (com.thinkbiganalytics.metadata.api.datasource.DatasourceProvider)10 JdbcDatasource (com.thinkbiganalytics.metadata.rest.model.data.JdbcDatasource)10 List (java.util.List)10 Optional (java.util.Optional)10 Collectors (java.util.stream.Collectors)10 Inject (javax.inject.Inject)10 FeedServicesAccessControl (com.thinkbiganalytics.feedmgr.security.FeedServicesAccessControl)9 AccessController (com.thinkbiganalytics.security.AccessController)9 HashSet (java.util.HashSet)9 NotFoundException (javax.ws.rs.NotFoundException)9 StringUtils (org.apache.commons.lang3.StringUtils)9 Logger (org.slf4j.Logger)9 LoggerFactory (org.slf4j.LoggerFactory)9 QueryResult (com.thinkbiganalytics.discovery.schema.QueryResult)8 TableSchema (com.thinkbiganalytics.discovery.schema.TableSchema)8 AccessControlled (com.thinkbiganalytics.metadata.api.security.AccessControlled)8 DatasourceCriteria (com.thinkbiganalytics.metadata.rest.model.data.DatasourceCriteria)8