Search in sources :

Example 1 with JerseyRestClient

use of com.thinkbiganalytics.rest.JerseyRestClient in project kylo by Teradata.

the class RemoteClientRunner method register.

/**
 * Registers this Spark Shell client with the remote Kylo services server.
 */
public void register() {
    Preconditions.checkState(serverUrl != null, "Registration server is not available.");
    // Parse server address
    final URL url;
    try {
        url = new URL(serverUrl);
    } catch (MalformedURLException e) {
        throw new IllegalStateException("Not a valid registration URL: " + serverUrl);
    }
    // Verify client id and secret
    Preconditions.checkNotNull(clientId, "Environment variable is not defined: KYLO_CLIENT_ID");
    Preconditions.checkNotNull(clientSecret, "Environment variable is not defined: KYLO_CLIENT_SECRET");
    // Register with server
    final char[] keystorePasswordChars = (keystorePassword != null) ? keystorePassword.toCharArray() : null;
    final JerseyClientConfig config = new JerseyClientConfig(url.getHost(), clientId, clientSecret.toCharArray(), url.getProtocol().equalsIgnoreCase("https"), false, keystorePath, keystorePasswordChars);
    config.setPort(url.getPort() > 0 ? url.getPort() : url.getDefaultPort());
    final JerseyRestClient client = getRestClient(config);
    final String hostName = getHostName();
    log.info("Registering client {} at {}:{} with server {}.", clientId, hostName, localPort, serverUrl);
    final Response response = client.post(url.getPath(), ImmutableMap.of("host", hostName, "port", localPort));
    if (response != null && response.getStatus() >= 200 && response.getStatus() < 300) {
        log.info("Successfully registered client.");
    } else {
        log.info("Registration failed with response: {}", response);
        throw new IllegalStateException("Failed to register with server");
    }
}
Also used : Response(javax.ws.rs.core.Response) MalformedURLException(java.net.MalformedURLException) JerseyClientConfig(com.thinkbiganalytics.rest.JerseyClientConfig) URL(java.net.URL) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient)

Example 2 with JerseyRestClient

use of com.thinkbiganalytics.rest.JerseyRestClient 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 3 with JerseyRestClient

use of com.thinkbiganalytics.rest.JerseyRestClient 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 4 with JerseyRestClient

use of com.thinkbiganalytics.rest.JerseyRestClient in project kylo by Teradata.

the class SparkLivyProcessManager method initSession.

private void initSession(SparkLivyProcess sparkLivyProcess) {
    JerseyRestClient jerseyClient = getClient(sparkLivyProcess);
    String script = scalaScriptService.getInitSessionScript();
    StatementsPost sp = new StatementsPost.Builder().kind(StatementKind.spark.toString()).code(script).build();
    Statement statement = livyClient.postStatement(jerseyClient, sparkLivyProcess, sp);
    // NOTE:  why pollStatement now?  so we block on result.
    livyClient.pollStatement(jerseyClient, sparkLivyProcess, statement.getId());
}
Also used : StatementsPost(com.thinkbiganalytics.kylo.spark.model.StatementsPost) Statement(com.thinkbiganalytics.kylo.spark.model.Statement) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient)

Example 5 with JerseyRestClient

use of com.thinkbiganalytics.rest.JerseyRestClient 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

JerseyRestClient (com.thinkbiganalytics.rest.JerseyRestClient)23 Statement (com.thinkbiganalytics.kylo.spark.model.Statement)11 Nonnull (javax.annotation.Nonnull)10 LivyUserException (com.thinkbiganalytics.kylo.spark.exceptions.LivyUserException)7 JerseyClientConfig (com.thinkbiganalytics.rest.JerseyClientConfig)5 Session (com.thinkbiganalytics.kylo.spark.model.Session)4 SparkJobResponse (com.thinkbiganalytics.kylo.spark.rest.model.job.SparkJobResponse)3 SaveResponse (com.thinkbiganalytics.spark.rest.model.SaveResponse)3 LivyServerNotReachableException (com.thinkbiganalytics.kylo.spark.exceptions.LivyServerNotReachableException)2 TransformResponse (com.thinkbiganalytics.spark.rest.model.TransformResponse)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Invocation (javax.ws.rs.client.Invocation)2 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 SPRING_PROFILES_INCLUDE (com.thinkbiganalytics.install.inspector.inspection.Configuration.SPRING_PROFILES_INCLUDE)1