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