Search in sources :

Example 1 with SaveResult

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

the class AbstractTransformController method getSave.

/**
 * Requests the status of a save.
 *
 * @param id the save id
 * @return the save status
 */
@GET
@Path("{table}/save/{save}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation("Fetches the status of a save")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the status of the save.", response = SaveResponse.class), @ApiResponse(code = 404, message = "The transformation or save does not exist.", response = SaveResponse.class), @ApiResponse(code = 500, message = "There was a problem accessing the data.", response = SaveResponse.class) })
@Nonnull
public Response getSave(@Nonnull @PathParam("save") final String id) {
    try {
        final SaveJob job = transformService.getSaveJob(id, false);
        final SaveResponse response = new SaveResponse();
        if (job.isDone()) {
            response.setId(job.getGroupId());
            response.setStatus(SaveResponse.Status.SUCCESS);
            final SaveResult result = job.get();
            if (result.getPath() != null) {
                response.setLocation("./zip");
            } else {
                transformService.getSaveJob(id, true);
            }
        } else {
            response.setId(job.getGroupId());
            response.setProgress(job.progress());
            response.setStatus(SaveResponse.Status.PENDING);
        }
        return Response.ok(response).build();
    } catch (final IllegalArgumentException e) {
        return error(Response.Status.NOT_FOUND, "getSave.notFound");
    } catch (final Exception e) {
        final SaveResponse save = new SaveResponse();
        save.setId(id);
        save.setMessage(e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName());
        save.setStatus(SaveResponse.Status.ERROR);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(save).build();
    }
}
Also used : SaveResponse(com.thinkbiganalytics.spark.rest.model.SaveResponse) SaveJob(com.thinkbiganalytics.spark.metadata.SaveJob) SaveResult(com.thinkbiganalytics.spark.model.SaveResult) MissingResourceException(java.util.MissingResourceException) Path(javax.ws.rs.Path) 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 2 with SaveResult

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

the class SaveDataSetStage method apply.

@Nonnull
@Override
public SaveResult apply(@Nullable final TransformResult transform) {
    Preconditions.checkNotNull(transform);
    // Configure writer
    final DataFrameWriter writer = getDataSet(transform).write();
    if (request.getFormat() != null) {
        writer.format(request.getFormat());
    }
    if (request.getMode() != null) {
        writer.mode(request.getMode());
    }
    if (request.getOptions() != null) {
        writer.options(request.getOptions());
    }
    // Save transformation
    final SaveResult result = new SaveResult();
    if (request.getJdbc() != null) {
        final Properties properties = new Properties();
        properties.setProperty("driver", request.getJdbc().getDatabaseDriverClassName());
        properties.setProperty("user", request.getJdbc().getDatabaseUser());
        properties.setProperty("password", request.getJdbc().getPassword());
        writer.jdbc(request.getJdbc().getDatabaseConnectionUrl(), request.getTableName(), properties);
    } else if (request.getTableName() != null) {
        writer.saveAsTable(request.getTableName());
    } else {
        final String hadoopTmpDir = fs.getConf().get("hadoop.tmp.dir", "/tmp");
        final Path absolutePath = new Path(hadoopTmpDir, UUID.randomUUID().toString());
        final Path qualifiedPath = fs.makeQualified(absolutePath);
        result.setPath(qualifiedPath);
        writer.save(qualifiedPath.toString());
    }
    return result;
}
Also used : Path(org.apache.hadoop.fs.Path) DataFrameWriter(org.apache.spark.sql.DataFrameWriter) SaveResult(com.thinkbiganalytics.spark.model.SaveResult) Properties(java.util.Properties) Nonnull(javax.annotation.Nonnull)

Example 3 with SaveResult

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

the class AbstractTransformController method download.

/**
 * Downloads the saved results as a ZIP file.
 *
 * @param id the save id
 * @return the zip file response
 */
@GET
@Path("{table}/save/{save}/zip")
@ApiOperation("Downloads the saved results in a ZIP file")
@ApiResponses({ @ApiResponse(code = 200, message = "Returns the saved file."), @ApiResponse(code = 404, message = "The save does not exist."), @ApiResponse(code = 500, message = "There was a problem accessing the data.") })
@Nonnull
public Response download(@Nonnull @PathParam("save") final String id) {
    // Find job
    SaveJob job;
    try {
        job = transformService.getSaveJob(id, true);
    } catch (final IllegalArgumentException e) {
        job = null;
    }
    // Get result
    final SaveResult result;
    if (job != null && job.isDone()) {
        try {
            result = job.get();
        } catch (final Exception e) {
            return error(Response.Status.INTERNAL_SERVER_ERROR, e);
        }
    } else {
        result = null;
    }
    // Return response
    if (result != null && result.getPath() != null) {
        return Response.ok(new ZipStreamingOutput(result.getPath(), fileSystem)).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + id + ".zip\"").build();
    } else {
        return error(Response.Status.NOT_FOUND, "download.notFound");
    }
}
Also used : ZipStreamingOutput(com.thinkbiganalytics.spark.io.ZipStreamingOutput) SaveJob(com.thinkbiganalytics.spark.metadata.SaveJob) SaveResult(com.thinkbiganalytics.spark.model.SaveResult) MissingResourceException(java.util.MissingResourceException) Path(javax.ws.rs.Path) Nonnull(javax.annotation.Nonnull) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with SaveResult

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

the class TransformService method saveSql.

/**
 * Executes and saves a Spark SQL request.
 */
@Nonnull
public SaveResponse saveSql(@Nonnull final String id, @Nonnull final SaveRequest save) {
    log.entry(id, save);
    // Create task
    final Supplier<SaveResult> task;
    final TransformRequest transform = getTransformRequest(id);
    final JdbcDatasource transformDatasource = (transform.getDatasources() != null && transform.getDatasources().size() == 1 && transform.getDatasources().get(0) instanceof JdbcDatasource) ? (JdbcDatasource) transform.getDatasources().get(0) : null;
    if (transformDatasource != null && save.getJdbc() != null && Objects.equal(transformDatasource.getId(), save.getJdbc().getId())) {
        Preconditions.checkArgument(save.getTableName() != null, "Missing target table name.");
        task = new SaveSqlStage(save.getTableName(), transform.getScript(), save.getJdbc());
    } else {
        task = createSaveTask(save, createSqlTask(transform));
    }
    // Submit job
    final SaveResponse response = submitSaveJob(task);
    return log.exit(response);
}
Also used : JdbcDatasource(com.thinkbiganalytics.spark.rest.model.JdbcDatasource) SaveSqlStage(com.thinkbiganalytics.spark.metadata.SaveSqlStage) SaveResponse(com.thinkbiganalytics.spark.rest.model.SaveResponse) SaveResult(com.thinkbiganalytics.spark.model.SaveResult) TransformRequest(com.thinkbiganalytics.spark.rest.model.TransformRequest) Nonnull(javax.annotation.Nonnull)

Aggregations

SaveResult (com.thinkbiganalytics.spark.model.SaveResult)4 Nonnull (javax.annotation.Nonnull)4 SaveJob (com.thinkbiganalytics.spark.metadata.SaveJob)2 SaveResponse (com.thinkbiganalytics.spark.rest.model.SaveResponse)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 MissingResourceException (java.util.MissingResourceException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 ZipStreamingOutput (com.thinkbiganalytics.spark.io.ZipStreamingOutput)1 SaveSqlStage (com.thinkbiganalytics.spark.metadata.SaveSqlStage)1 JdbcDatasource (com.thinkbiganalytics.spark.rest.model.JdbcDatasource)1 TransformRequest (com.thinkbiganalytics.spark.rest.model.TransformRequest)1 Properties (java.util.Properties)1 Produces (javax.ws.rs.Produces)1 Path (org.apache.hadoop.fs.Path)1 DataFrameWriter (org.apache.spark.sql.DataFrameWriter)1