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);
}
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();
}
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();
}
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);
}
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();
}
Aggregations