Search in sources :

Example 11 with TransformResponse

use of com.thinkbiganalytics.spark.rest.model.TransformResponse in project kylo by Teradata.

the class ResponseStage method apply.

@Nonnull
@Override
public TransformResponse apply(@Nullable final TransformResult result) {
    Preconditions.checkNotNull(result);
    // Transform data set into rows
    final QueryResultRowTransform rowTransform = new QueryResultRowTransform(result.getDataSet().schema(), table);
    final List<List<Object>> rows = Lists.transform(result.getDataSet().collectAsList(), new Function<Row, List<Object>>() {

        @Nullable
        @Override
        public List<Object> apply(@Nullable Row row) {
            return (row != null) ? rowTransform.convertRow(row) : null;
        }
    });
    // Build the query result
    final TransformQueryResult queryResult = new TransformQueryResult();
    queryResult.setColumns(result.getColumns());
    queryResult.setRows(rows);
    queryResult.setValidationResults(result.getValidationResults());
    // Build the response
    final TransformResponse response = new TransformResponse();
    response.setProfile(result.getProfile());
    response.setResults(queryResult);
    response.setStatus(TransformResponse.Status.SUCCESS);
    response.setTable(table);
    return response;
}
Also used : TransformQueryResult(com.thinkbiganalytics.spark.rest.model.TransformQueryResult) List(java.util.List) TransformResponse(com.thinkbiganalytics.spark.rest.model.TransformResponse) Row(org.apache.spark.sql.Row) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull)

Example 12 with TransformResponse

use of com.thinkbiganalytics.spark.rest.model.TransformResponse in project kylo by Teradata.

the class AbstractTransformController method createErrorResponse.

/**
 * Creates a response for the specified message.
 *
 * @param status  the response status
 * @param message the message text
 * @return the response
 */
private Response createErrorResponse(@Nonnull final Response.Status status, @Nonnull final String message) {
    final TransformResponse entity = new TransformResponse();
    entity.setMessage(message);
    entity.setStatus(TransformResponse.Status.ERROR);
    return Response.status(status).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).entity(entity).build();
}
Also used : TransformResponse(com.thinkbiganalytics.spark.rest.model.TransformResponse)

Example 13 with TransformResponse

use of com.thinkbiganalytics.spark.rest.model.TransformResponse in project kylo by Teradata.

the class AbstractTransformController method getTable.

/**
 * Requests the status of a transformation.
 *
 * @param id the destination table name
 * @return the transformation status
 */
@GET
@Path("{table}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Fetches the status of a transformation.")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the status of the transformation.", response = TransformResponse.class), @ApiResponse(code = 404, message = "The transformation does not exist.", response = TransformResponse.class), @ApiResponse(code = 500, message = "There was a problem accessing the data.", response = TransformResponse.class) })
@Nonnull
public Response getTable(@Nonnull @PathParam("table") final String id) {
    try {
        TransformJob job = transformService.getTransformJob(id);
        if (job.isDone()) {
            return Response.ok(job.get()).build();
        } else {
            TransformResponse response = new TransformResponse();
            response.setProgress(job.progress());
            response.setStatus(TransformResponse.Status.PENDING);
            response.setTable(job.getGroupId());
            return Response.ok(response).build();
        }
    } catch (final IllegalArgumentException e) {
        return error(Response.Status.NOT_FOUND, "getTable.notFound");
    } catch (final Exception e) {
        return error(Response.Status.INTERNAL_SERVER_ERROR, e);
    }
}
Also used : TransformJob(com.thinkbiganalytics.spark.metadata.TransformJob) TransformResponse(com.thinkbiganalytics.spark.rest.model.TransformResponse) MissingResourceException(java.util.MissingResourceException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Nonnull(javax.annotation.Nonnull) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 14 with TransformResponse

use of com.thinkbiganalytics.spark.rest.model.TransformResponse in project kylo by Teradata.

the class TransformService method execute.

/**
 * Executes the specified transformation and returns the name of the Hive table containing the results.
 *
 * @param request the transformation request
 * @return the Hive table containing the results
 * @throws IllegalStateException if this service is not running
 * @throws ScriptException       if the script cannot be executed
 */
@Nonnull
public TransformResponse execute(@Nonnull final TransformRequest request) throws ScriptException {
    log.entry(request);
    // Handle async request
    if (request.isAsync()) {
        return cacheTransform(request);
    }
    // Execute script
    final DataSet dataSet = createShellTask(request);
    final StructType schema = dataSet.schema();
    TransformResponse response = submitTransformJob(new ShellTransformStage(dataSet), getPolicies(request));
    // Build response
    if (response.getStatus() != TransformResponse.Status.SUCCESS) {
        final String table = response.getTable();
        final TransformQueryResult partialResult = new TransformQueryResult();
        partialResult.setColumns(Arrays.<QueryResultColumn>asList(new QueryResultRowTransform(schema, table).columns()));
        response = new TransformResponse();
        response.setProgress(0.0);
        response.setResults(partialResult);
        response.setStatus(TransformResponse.Status.PENDING);
        response.setTable(table);
    }
    return log.exit(response);
}
Also used : QueryResultRowTransform(com.thinkbiganalytics.spark.metadata.QueryResultRowTransform) StructType(org.apache.spark.sql.types.StructType) DataSet(com.thinkbiganalytics.spark.DataSet) ShellTransformStage(com.thinkbiganalytics.spark.metadata.ShellTransformStage) TransformQueryResult(com.thinkbiganalytics.spark.rest.model.TransformQueryResult) TransformResponse(com.thinkbiganalytics.spark.rest.model.TransformResponse) Nonnull(javax.annotation.Nonnull)

Example 15 with TransformResponse

use of com.thinkbiganalytics.spark.rest.model.TransformResponse in project kylo by Teradata.

the class TransformService method query.

/**
 * Executes the specified SQL query and returns the result.
 *
 * @param request the transformation request
 * @return the transformation results
 * @throws IllegalStateException if this service is not running
 * @throws ScriptException       if the script cannot be executed
 */
@Nonnull
public TransformResponse query(@Nonnull final TransformRequest request) throws ScriptException {
    log.entry(request);
    // Handle async request
    if (request.isAsync()) {
        return cacheTransform(request);
    }
    // Execute query
    final TransformResponse response = submitTransformJob(createSqlTask(request), getPolicies(request));
    return log.exit(response);
}
Also used : TransformResponse(com.thinkbiganalytics.spark.rest.model.TransformResponse) Nonnull(javax.annotation.Nonnull)

Aggregations

TransformResponse (com.thinkbiganalytics.spark.rest.model.TransformResponse)23 Test (org.junit.Test)12 TransformRequest (com.thinkbiganalytics.spark.rest.model.TransformRequest)8 Nonnull (javax.annotation.Nonnull)7 Response (javax.ws.rs.core.Response)7 SaveResponse (com.thinkbiganalytics.spark.rest.model.SaveResponse)4 DataSet (com.thinkbiganalytics.spark.DataSet)3 TransformJob (com.thinkbiganalytics.spark.metadata.TransformJob)3 TransformQueryResult (com.thinkbiganalytics.spark.rest.model.TransformQueryResult)3 TransformService (com.thinkbiganalytics.spark.service.TransformService)3 InputStream (java.io.InputStream)3 List (java.util.List)3 StructType (org.apache.spark.sql.types.StructType)3 SparkContextService (com.thinkbiganalytics.spark.SparkContextService)2 SparkScriptEngine (com.thinkbiganalytics.spark.repl.SparkScriptEngine)2 MissingResourceException (java.util.MissingResourceException)2 ScriptException (javax.script.ScriptException)2 SparkContext (org.apache.spark.SparkContext)2 StorageLevel (org.apache.spark.storage.StorageLevel)2 NamedParam (scala.tools.nsc.interpreter.NamedParam)2