Search in sources :

Example 1 with Statement

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

the class DefaultLivyClient method getStatement.

@Override
public Statement getStatement(JerseyRestClient client, SparkLivyProcess sparkLivyProcess, Integer statementId) {
    Integer sessionId = sparkLivyProcess.getSessionId();
    try {
        Statement statement = client.get(STATEMENT_URL(sessionId, statementId), Statement.class);
        livyServer.setLivyServerStatus(LivyServerStatus.alive);
        logger.debug("statement={}", statement);
        return statement;
    } catch (WebApplicationException wae) {
        if (wae.getResponse().getStatus() != 404 || wae.getResponse().getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
            livyServer.setLivyServerStatus(LivyServerStatus.http_error);
        }
        throw wae;
    } catch (LivyException le) {
        livyServer.setLivyServerStatus(LivyServerStatus.http_error);
        throw le;
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) Statement(com.thinkbiganalytics.kylo.spark.model.Statement) LivyException(com.thinkbiganalytics.kylo.spark.exceptions.LivyException)

Example 2 with Statement

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

the class LivyRestModelTransformer method toTransformResponse.

public static TransformResponse toTransformResponse(Statement statement, String transformId) {
    TransformResponse transformResponse = prepTransformResponse(statement, transformId);
    if (transformResponse.getStatus() == TransformResponse.Status.SUCCESS) {
        String code = statement.getCode().trim();
        if (code.endsWith("dfResultsAsJson")) {
            transformResponse.setResults(toTransformQueryResultWithSchema(transformResponse, statement.getOutput()));
        } else if (code.endsWith("dfProf")) {
            List<OutputRow> rows = toTransformResponseProfileStats(statement.getOutput());
            transformResponse.setProfile(toTransformResponseProfileStats(statement.getOutput()));
            transformResponse.setActualCols(1);
            Integer actualRows = rows.stream().filter(metric -> metric.getMetricType().equals(MetricType.TOTAL_COUNT.toString())).map(metric -> Integer.valueOf(metric.getMetricValue())).findFirst().orElse(1);
            transformResponse.setActualRows(actualRows);
            transformResponse.setResults(emptyResult());
        } else if (code.endsWith("transformAsStr")) {
            /* expects that 'statement' contains a payload of TransformResponse in JSON format */
            TransformResponse tr = serializeStatementOutputResponse(checkCodeWasWellFormed(statement.getOutput()), TransformResponse.class);
            statementIdCache.put(tr.getTable(), statement.getId());
            return tr;
        } else {
            logger.error("Exception Processing Result: ", new LivyCodeException("Unsupported result type requested of Livy.  Results not recognized"));
            throw new LivyUserException("livy.unsupported_result_type");
        }
    // end if
    }
    return transformResponse;
}
Also used : XLogger(org.slf4j.ext.XLogger) LivyServerStatus(com.thinkbiganalytics.kylo.spark.client.model.enums.LivyServerStatus) XLoggerFactory(org.slf4j.ext.XLoggerFactory) StatementOutputResponse(com.thinkbiganalytics.kylo.spark.model.StatementOutputResponse) SparkJobResult(com.thinkbiganalytics.kylo.spark.rest.model.job.SparkJobResult) StringUtils(org.apache.commons.lang3.StringUtils) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SparkJobResponse(com.thinkbiganalytics.kylo.spark.rest.model.job.SparkJobResponse) ObjectMapperSerializer(com.thinkbiganalytics.json.ObjectMapperSerializer) TransformResponse(com.thinkbiganalytics.spark.rest.model.TransformResponse) Lists(com.google.common.collect.Lists) MetricType(com.thinkbiganalytics.spark.dataprofiler.model.MetricType) StatementState(com.thinkbiganalytics.kylo.spark.model.enums.StatementState) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) Nonnull(javax.annotation.Nonnull) SessionState(com.thinkbiganalytics.kylo.spark.model.enums.SessionState) LivySessionStatus(com.thinkbiganalytics.kylo.spark.client.model.enums.LivySessionStatus) Iterator(java.util.Iterator) DefaultQueryResultColumn(com.thinkbiganalytics.discovery.model.DefaultQueryResultColumn) Statement(com.thinkbiganalytics.kylo.spark.model.Statement) StatementOutputStatus(com.thinkbiganalytics.kylo.spark.model.enums.StatementOutputStatus) OutputRow(com.thinkbiganalytics.spark.dataprofiler.output.OutputRow) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SparkLivySaveException(com.thinkbiganalytics.kylo.spark.livy.SparkLivySaveException) IOException(java.io.IOException) DataSources(com.thinkbiganalytics.spark.rest.model.DataSources) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) LivyUserException(com.thinkbiganalytics.kylo.spark.exceptions.LivyUserException) SaveResponse(com.thinkbiganalytics.spark.rest.model.SaveResponse) LivyServer(com.thinkbiganalytics.kylo.spark.client.model.LivyServer) LivyCodeException(com.thinkbiganalytics.kylo.spark.exceptions.LivyCodeException) QueryResultColumn(com.thinkbiganalytics.discovery.schema.QueryResultColumn) ServerStatusResponse(com.thinkbiganalytics.spark.rest.model.ServerStatusResponse) WebApplicationException(javax.ws.rs.WebApplicationException) CacheBuilder(com.google.common.cache.CacheBuilder) LivyDeserializationException(com.thinkbiganalytics.kylo.spark.exceptions.LivyDeserializationException) Cache(com.google.common.cache.Cache) TransformQueryResult(com.thinkbiganalytics.spark.rest.model.TransformQueryResult) LivyCodeException(com.thinkbiganalytics.kylo.spark.exceptions.LivyCodeException) TransformResponse(com.thinkbiganalytics.spark.rest.model.TransformResponse) List(java.util.List) LivyUserException(com.thinkbiganalytics.kylo.spark.exceptions.LivyUserException)

Example 3 with Statement

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

the class SparkLivyRestClient method createJob.

@Override
public SparkJobResponse createJob(@Nonnull final SparkShellProcess process, @Nonnull final SparkJobRequest request) {
    logger.entry(process, request);
    // Execute job script
    final JerseyRestClient client = sparkLivyProcessManager.getClient(process);
    final String script = scalaScriptService.wrapScriptForLivy(request);
    final Statement statement = submitCode(client, script, process);
    final String jobId = ScalaScriptService.newTableName();
    sparkLivyProcessManager.setStatementId(jobId, statement.getId());
    // Generate response
    final SparkJobResponse response = new SparkJobResponse();
    response.setId(jobId);
    response.setStatus(StatementStateTranslator.translate(statement.getState()));
    return logger.exit(response);
}
Also used : SparkJobResponse(com.thinkbiganalytics.kylo.spark.rest.model.job.SparkJobResponse) Statement(com.thinkbiganalytics.kylo.spark.model.Statement) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient)

Example 4 with Statement

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

the class SparkLivyRestClient method getTransformResult.

@Nonnull
@Override
public Optional<TransformResponse> getTransformResult(@Nonnull SparkShellProcess process, @Nonnull String transformId) {
    logger.entry(process, transformId);
    Validate.isInstanceOf(SparkLivyProcess.class, process, "SparkLivyRestClient.getTransformResult called on non Livy Process");
    SparkLivyProcess sparkLivyProcess = (SparkLivyProcess) process;
    JerseyRestClient client = sparkLivyProcessManager.getClient(process);
    Integer stmtId = sparkLivyProcessManager.getStatementId(transformId);
    Statement statement = livyClient.getStatement(client, sparkLivyProcess, stmtId);
    sparkLivyProcessManager.setStatementId(transformId, statement.getId());
    TransformResponse response = LivyRestModelTransformer.toTransformResponse(statement, transformId);
    if (statement.getState() == StatementState.available && response.getStatus() == TransformResponse.Status.PENDING) {
        // TODO:: change this so that if transformId is a livyQueryID it knows what to do. similar to saveResponse.  A good first stab at this is at KYLO-2639-withLivyId
        // The result came back from Livy, but the transform result still needs to be fetched
        String script = scriptGenerator.script("getTransform", ScalaScriptUtils.scalaStr(transformId));
        statement = submitCode(client, script, process);
        // associate transformId to this new query and get results on subsequent call
        sparkLivyProcessManager.setStatementId(transformId, statement.getId());
    } else if (response.getStatus() == TransformResponse.Status.ERROR) {
        logger.error(String.format("Unexpected error found in transform response:\n%s", response.getMessage()));
        throw new LivyUserException("livy.transform_error");
    }
    // end if
    return logger.exit(Optional.of(response));
}
Also used : Statement(com.thinkbiganalytics.kylo.spark.model.Statement) TransformResponse(com.thinkbiganalytics.spark.rest.model.TransformResponse) LivyUserException(com.thinkbiganalytics.kylo.spark.exceptions.LivyUserException) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient) Nonnull(javax.annotation.Nonnull)

Example 5 with Statement

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

the class SparkLivyRestClient method getJobResult.

@Nonnull
@Override
public Optional<SparkJobResponse> getJobResult(@Nonnull final SparkShellProcess process, @Nonnull final String id) {
    logger.entry(process, id);
    Validate.isInstanceOf(SparkLivyProcess.class, process, "SparkLivyRestClient.getJobResult called on non Livy Process");
    SparkLivyProcess sparkLivyProcess = (SparkLivyProcess) process;
    // Request result from Livy
    final JerseyRestClient client = sparkLivyProcessManager.getClient(process);
    final Integer statementId = sparkLivyProcessManager.getStatementId(id);
    final Statement statement = livyClient.getStatement(client, sparkLivyProcess, statementId);
    sparkLivyProcessManager.setStatementId(id, statement.getId());
    // Generate response
    final SparkJobResponse response = LivyRestModelTransformer.toJobResponse(id, statement);
    if (response.getStatus() != TransformResponse.Status.ERROR) {
        return logger.exit(Optional.of(response));
    } else {
        throw logger.throwing(new SparkException(String.format("Unexpected error found in transform response:\n%s", response.getMessage())));
    }
}
Also used : SparkJobResponse(com.thinkbiganalytics.kylo.spark.rest.model.job.SparkJobResponse) SparkException(com.thinkbiganalytics.kylo.spark.SparkException) Statement(com.thinkbiganalytics.kylo.spark.model.Statement) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient) Nonnull(javax.annotation.Nonnull)

Aggregations

Statement (com.thinkbiganalytics.kylo.spark.model.Statement)16 JerseyRestClient (com.thinkbiganalytics.rest.JerseyRestClient)11 Nonnull (javax.annotation.Nonnull)9 LivyUserException (com.thinkbiganalytics.kylo.spark.exceptions.LivyUserException)6 SparkJobResponse (com.thinkbiganalytics.kylo.spark.rest.model.job.SparkJobResponse)4 SaveResponse (com.thinkbiganalytics.spark.rest.model.SaveResponse)4 TransformResponse (com.thinkbiganalytics.spark.rest.model.TransformResponse)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 QueryResultColumn (com.thinkbiganalytics.discovery.schema.QueryResultColumn)2 LivyCodeException (com.thinkbiganalytics.kylo.spark.exceptions.LivyCodeException)2 LivyException (com.thinkbiganalytics.kylo.spark.exceptions.LivyException)2 ServerStatusResponse (com.thinkbiganalytics.spark.rest.model.ServerStatusResponse)2 TransformQueryResult (com.thinkbiganalytics.spark.rest.model.TransformQueryResult)2 URI (java.net.URI)2 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Cache (com.google.common.cache.Cache)1