Search in sources :

Example 1 with QueryResult

use of com.thinkbiganalytics.discovery.schema.QueryResult 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 2 with QueryResult

use of com.thinkbiganalytics.discovery.schema.QueryResult in project kylo by Teradata.

the class FeedRestController method getPage.

private Response getPage(String processingdttm, int limit, String table, String filter) {
    if (limit > MAX_LIMIT) {
        limit = MAX_LIMIT;
    } else if (limit < 1) {
        limit = 1;
    }
    StringBuilder query = new StringBuilder("SELECT * from " + HiveUtils.quoteIdentifier(table) + " where processing_dttm = " + HiveUtils.quoteString(processingdttm) + " ");
    if (StringUtils.isNotBlank(filter)) {
        query.append(filter);
    }
    query.append(" limit ").append(limit);
    QueryResult rows = hiveService.query(query.toString());
    return Response.ok(rows.getRows()).build();
}
Also used : QueryResult(com.thinkbiganalytics.discovery.schema.QueryResult)

Example 3 with QueryResult

use of com.thinkbiganalytics.discovery.schema.QueryResult in project kylo by Teradata.

the class FeedRestController method profileSummary.

@GET
@Path("/{feedId}/profile-summary")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Gets a summary of the feed profiles.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the profile summaries.", response = Map.class, responseContainer = "List"), @ApiResponse(code = 500, message = "The profiles are unavailable.", response = RestResponseStatus.class) })
public Response profileSummary(@PathParam("feedId") String feedId) {
    FeedMetadata feedMetadata = getMetadataService().getFeedById(feedId);
    final String profileTable = HiveUtils.quoteIdentifier(feedMetadata.getProfileTableName());
    String query = "SELECT * from " + profileTable + " where columnname = '(ALL)'";
    List<Map<String, Object>> rows = new ArrayList<>();
    try {
        QueryResult results = hiveService.query(query);
        rows.addAll(results.getRows());
        // add in the archive date time fields if applicipable
        String ARCHIVE_PROCESSOR_TYPE = "com.thinkbiganalytics.nifi.GetTableData";
        if (feedMetadata.getInputProcessorType().equalsIgnoreCase(ARCHIVE_PROCESSOR_TYPE)) {
            NifiProperty property = NifiPropertyUtil.findPropertyByProcessorType(feedMetadata.getProperties(), ARCHIVE_PROCESSOR_TYPE, "Date Field");
            if (property != null && property.getValue() != null) {
                String field = property.getValue();
                if (field.contains(".")) {
                    field = StringUtils.substringAfterLast(field, ".");
                }
                query = "SELECT * from " + profileTable + " where metrictype IN('MIN_TIMESTAMP','MAX_TIMESTAMP') AND columnname = " + HiveUtils.quoteString(field);
                QueryResult dateRows = hiveService.query(query);
                if (dateRows != null && !dateRows.isEmpty()) {
                    rows.addAll(dateRows.getRows());
                }
            }
        }
    } catch (DataAccessException e) {
        if (e.getCause() instanceof org.apache.hive.service.cli.HiveSQLException && e.getCause().getMessage().contains("Table not found")) {
        // this exception is ok to swallow since it just means no profile data exists yet
        } else if (e.getCause().getMessage().contains("HiveAccessControlException Permission denied")) {
            throw new AccessControlException("You do not have permission to execute this hive query");
        } else {
            throw e;
        }
    }
    return Response.ok(rows).build();
}
Also used : QueryResult(com.thinkbiganalytics.discovery.schema.QueryResult) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) ArrayList(java.util.ArrayList) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) AccessControlException(java.security.AccessControlException) Map(java.util.Map) DataAccessException(org.springframework.dao.DataAccessException) 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 4 with QueryResult

use of com.thinkbiganalytics.discovery.schema.QueryResult in project kylo by Teradata.

the class DatasourceController method query.

/**
 * Executes a query on the specified datasource.
 *
 * @param idStr the datasource id
 * @param query the SQL query
 * @return the SQL result
 */
@GET
@Path("{id}/query")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Executes a query 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 query(@PathParam("id") final String idStr, @QueryParam("query") final String query) {
    // 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.executeQueryForDatasource(datasource, query)).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) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 5 with QueryResult

use of com.thinkbiganalytics.discovery.schema.QueryResult in project kylo by Teradata.

the class FeedRestController method profileStats.

@GET
@Path("/{feedId}/profile-stats")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Gets the profile statistics for the specified job.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the profile statistics.", response = Map.class, responseContainer = "List"), @ApiResponse(code = 500, message = "The profile is unavailable.", response = RestResponseStatus.class) })
public Response profileStats(@PathParam("feedId") String feedId, @QueryParam("processingdttm") String processingdttm) {
    FeedMetadata feedMetadata = getMetadataService().getFeedById(feedId);
    String profileTable = feedMetadata.getProfileTableName();
    String query = "SELECT * from " + HiveUtils.quoteIdentifier(profileTable) + " where processing_dttm = " + HiveUtils.quoteString(processingdttm);
    QueryResult rows = hiveService.query(query);
    return Response.ok(rows.getRows()).build();
}
Also used : QueryResult(com.thinkbiganalytics.discovery.schema.QueryResult) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) 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)

Aggregations

QueryResult (com.thinkbiganalytics.discovery.schema.QueryResult)5 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 AccessControlException (java.security.AccessControlException)3 Collections2 (com.google.common.collect.Collections2)2 Formatters (com.thinkbiganalytics.Formatters)2 DefaultDatabaseMetadata (com.thinkbiganalytics.discovery.model.DefaultDatabaseMetadata)2 DatabaseMetadata (com.thinkbiganalytics.discovery.schema.DatabaseMetadata)2 TableSchema (com.thinkbiganalytics.discovery.schema.TableSchema)2 DBCPConnectionPoolService (com.thinkbiganalytics.feedmgr.nifi.controllerservice.DBCPConnectionPoolService)2 Model (com.thinkbiganalytics.feedmgr.rest.Model)2 FeedMetadata (com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata)2 FeedServicesAccessControl (com.thinkbiganalytics.feedmgr.security.FeedServicesAccessControl)2 DatasourceModelTransform (com.thinkbiganalytics.feedmgr.service.datasource.DatasourceModelTransform)2 SecurityService (com.thinkbiganalytics.feedmgr.service.security.SecurityService)2 MetadataAccess (com.thinkbiganalytics.metadata.api.MetadataAccess)2 DatasourceDefinitionProvider (com.thinkbiganalytics.metadata.api.datasource.DatasourceDefinitionProvider)2