use of com.ibm.streamsx.rest.Job in project streamsx.topology by IBMStreams.
the class DistributedStreamsContext method invokeUsingRest.
/**
* V5 path when local build occurred,
* (V5 is always job submission using REST).
*/
protected BigInteger invokeUsingRest(AppEntity entity, File bundle) throws Exception {
instance = createInstance(entity);
Result<Job, JsonObject> result = instance.submitJob(bundle, deploy(entity.submission));
result.getRawResult().addProperty(SubmissionResultsKeys.INSTANCE_ID, instance.getId());
final JsonObject submissionResult = GsonUtilities.objectCreate(entity.submission, RemoteContext.SUBMISSION_RESULTS);
for (Entry<String, JsonElement> kv : result.getRawResult().entrySet()) submissionResult.add(kv.getKey(), kv.getValue());
return new BigInteger(result.getId());
}
use of com.ibm.streamsx.rest.Job in project streamsx.topology by IBMStreams.
the class AnalyticsServiceStreamsContext method submitJobToService.
private BigInteger submitJobToService(File bundle, AppEntity entity) throws IOException {
final JsonObject submission = entity.submission;
JsonObject deploy = deploy(submission);
JsonObject jco = DeployKeys.copyJobConfigOverlays(deploy);
final StreamingAnalyticsService sas = sas(entity);
Result<Job, JsonObject> submitResult = sas.submitJob(bundle, jco);
final JsonObject submissionResult = GsonUtilities.objectCreate(submission, RemoteContext.SUBMISSION_RESULTS);
GsonUtilities.addAll(submissionResult, submitResult.getRawResult());
// Ensure job id is in a known place regardless of version
final String jobId = submitResult.getId();
GsonUtilities.addToObject(submissionResult, SubmissionResultsKeys.JOB_ID, jobId);
return new BigInteger(jobId);
}
use of com.ibm.streamsx.rest.Job in project streamsx.topology by IBMStreams.
the class DistributedStreamsRestContext method postBuildAction.
@Override
protected void postBuildAction(JsonObject deploy, JsonObject jco, JsonObject result) throws Exception {
if (instance == null) {
URL instanceUrl = new URL(StreamsKeys.getStreamsInstanceURL(deploy));
String path = instanceUrl.getPath();
URL restUrl;
if (path.startsWith("/streams/rest/instances/")) {
restUrl = new URL(instanceUrl.getProtocol(), instanceUrl.getHost(), instanceUrl.getPort(), "/streams/rest/resources");
} else {
restUrl = new URL(instanceUrl.getProtocol(), instanceUrl.getHost(), instanceUrl.getPort(), path.replaceFirst("/streams-rest/", "/streams-resource/"));
}
JsonObject serviceDefinition = object(deploy, StreamsKeys.SERVICE_DEFINITION);
String name = jstring(serviceDefinition, "service_name");
final boolean verify = sslVerify(deploy);
Function<Executor, String> authenticator = (name == null || name.isEmpty()) ? StandaloneAuthenticator.of(serviceDefinition) : ICP4DAuthenticator.of(serviceDefinition, verify);
StreamsConnection conn = StreamsConnection.ofAuthenticator(restUrl.toExternalForm(), authenticator);
if (!verify)
conn.allowInsecureHosts(true);
// Create the instance directly from the URL
instance = conn.getInstance(instanceUrl.toExternalForm());
}
JsonArray artifacts = GsonUtilities.array(GsonUtilities.object(result, "build"), "artifacts");
try {
if (artifacts == null || artifacts.size() == 0)
throw new IllegalStateException("No build artifacts produced.");
if (artifacts.size() != 1)
throw new IllegalStateException("Multiple build artifacts produced.");
String location = GsonUtilities.jstring(artifacts.get(0).getAsJsonObject(), "location");
report("Uploading bundle");
ApplicationBundle bundle = instance.uploadBundle(new File(location));
report("Submitting job");
Result<Job, JsonObject> submissionResult = bundle.submitJob(jco);
for (Entry<String, JsonElement> entry : submissionResult.getRawResult().entrySet()) {
result.add(entry.getKey(), entry.getValue());
}
report("Job id:" + submissionResult.getId());
} finally {
if (!jboolean(deploy, KEEP_ARTIFACTS)) {
for (JsonElement e : artifacts) {
JsonObject artifact = e.getAsJsonObject();
if (artifact.has("location")) {
new File(GsonUtilities.jstring(artifact, "location")).delete();
}
}
if (result.has(SubmissionResultsKeys.BUNDLE_PATH))
result.remove(SubmissionResultsKeys.BUNDLE_PATH);
}
}
}
use of com.ibm.streamsx.rest.Job in project streamsx.topology by IBMStreams.
the class StreamsConnectionTest method testJobObject.
@Test
public void testJobObject() throws Exception {
setupJob();
List<Job> jobs = instance.getJobs();
// we should have at least one job
assertTrue(jobs.size() > 0);
boolean foundJob = false;
for (Job j : jobs) {
if (j.getId().equals(job.getId())) {
foundJob = true;
break;
}
}
assertTrue(foundJob);
assertSame(job.getStreamsConnection(), instance.getStreamsConnection());
// get a specific job
final Job job2 = instance.getJob(jobId);
for (int i = 0; i < 3; i++) {
// check a subset of info returned matches
assertEquals(job.getId(), job2.getId());
assertEquals(job.getName(), job2.getName());
assertEquals(job.getHealth(), job2.getHealth());
assertEquals(job.getApplicationName(), job2.getApplicationName());
assertEquals(job.getJobGroup(), job2.getJobGroup());
assertEquals(job.getStartedBy(), job2.getStartedBy());
assertEquals(job.getStatus(), job2.getStatus());
assertEquals("job", job2.getResourceType());
assertEquals("job", job.getResourceType());
Thread.sleep(400);
job2.refresh();
}
// job is setup with 3 operators
List<Operator> operators = job.getOperators();
assertEquals(3, operators.size());
// job is setup with 2 PEs
List<ProcessingElement> pes = job.getPes();
assertEquals(2, pes.size());
checkResourceAllocations(job.getResourceAllocations(), true);
validateOperators();
validateProcessingElements();
}
use of com.ibm.streamsx.rest.Job in project streamsx.topology by IBMStreams.
the class StreamsConnectionSample method main.
public static void main(String[] args) throws IOException {
String userName = args[0];
String authToken = args[1];
String url = args[2];
String instanceName = args[3];
/*
* Create the connection to the instance indicated
*/
StreamsConnection sClient = StreamsConnection.createInstance(userName, authToken, url);
/*
* This option is only used to by-pass the certificate certification
*/
if (args.length == 5 && "true".equals(args[4])) {
sClient.allowInsecureHosts(true);
}
try {
System.out.println("Instance: ");
Instance instance = sClient.getInstance(instanceName);
/*
* From the Instance, get a list of jobs
*/
List<Job> jobs = instance.getJobs();
for (Job job : jobs) {
System.out.println("Job: " + job.toString());
/*
* For each job, get a list of operators
*/
List<Operator> operators = job.getOperators();
for (Operator op : operators) {
System.out.println("Operator: " + op.toString());
List<Metric> metrics = op.getMetrics();
/*
* For each operator, you can get a list of metrics, output
* ports and input ports
*/
for (Metric m : metrics) {
System.out.println("Metric: " + m.toString());
}
List<OutputPort> outP = op.getOutputPorts();
for (OutputPort oport : outP) {
System.out.println("Output Port: " + oport.toString());
/*
* For each output port, you can get a list of metrics
*/
for (Metric om : oport.getMetrics()) {
System.out.println("Output Port Metric: " + om.toString());
}
}
List<InputPort> inP = op.getInputPorts();
/*
* For each input port, get a list of metrics
*/
for (InputPort ip : inP) {
System.out.println("Input Port: " + ip.toString());
for (Metric im : ip.getMetrics()) {
System.out.println("Input Port Metric: " + im.toString());
}
}
}
/*
* For each job, get a list of processing elements
*/
for (ProcessingElement pe : job.getPes()) {
System.out.println("ProcessingElement:" + pe.toString());
}
}
try {
/*
* Get a specific job in the instance
*/
instance.getJob("99999");
} catch (RESTException e) {
/*
* This shows what is available in the RESTException should
* something fail
*/
System.out.println("Status Code: " + e.getStatusCode());
System.out.println("Message Id: " + e.getStreamsErrorMessageId());
System.out.println("MessageAsJson: " + e.getStreamsErrorMessageAsJson().toString());
System.out.println("Message: " + e.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations