Search in sources :

Example 6 with Job

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());
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) BigInteger(java.math.BigInteger) Job(com.ibm.streamsx.rest.Job)

Example 7 with Job

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);
}
Also used : StreamingAnalyticsService(com.ibm.streamsx.rest.StreamingAnalyticsService) JsonObject(com.google.gson.JsonObject) BigInteger(java.math.BigInteger) Job(com.ibm.streamsx.rest.Job)

Example 8 with Job

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);
        }
    }
}
Also used : JsonObject(com.google.gson.JsonObject) StreamsConnection(com.ibm.streamsx.rest.StreamsConnection) URL(java.net.URL) JsonArray(com.google.gson.JsonArray) GsonUtilities.jboolean(com.ibm.streamsx.topology.internal.gson.GsonUtilities.jboolean) Executor(org.apache.http.client.fluent.Executor) ApplicationBundle(com.ibm.streamsx.rest.ApplicationBundle) JsonElement(com.google.gson.JsonElement) Job(com.ibm.streamsx.rest.Job) File(java.io.File)

Example 9 with Job

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();
}
Also used : Operator(com.ibm.streamsx.rest.Operator) ProcessingElement(com.ibm.streamsx.rest.ProcessingElement) Job(com.ibm.streamsx.rest.Job) Test(org.junit.Test)

Example 10 with Job

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();
    }
}
Also used : Operator(com.ibm.streamsx.rest.Operator) OutputPort(com.ibm.streamsx.rest.OutputPort) ProcessingElement(com.ibm.streamsx.rest.ProcessingElement) RESTException(com.ibm.streamsx.rest.RESTException) Instance(com.ibm.streamsx.rest.Instance) InputPort(com.ibm.streamsx.rest.InputPort) StreamsConnection(com.ibm.streamsx.rest.StreamsConnection) IOException(java.io.IOException) RESTException(com.ibm.streamsx.rest.RESTException) Metric(com.ibm.streamsx.rest.Metric) Job(com.ibm.streamsx.rest.Job)

Aggregations

Job (com.ibm.streamsx.rest.Job)11 JsonObject (com.google.gson.JsonObject)5 Instance (com.ibm.streamsx.rest.Instance)4 JsonElement (com.google.gson.JsonElement)3 Metric (com.ibm.streamsx.rest.Metric)3 Operator (com.ibm.streamsx.rest.Operator)3 ProcessingElement (com.ibm.streamsx.rest.ProcessingElement)3 StreamingAnalyticsService (com.ibm.streamsx.rest.StreamingAnalyticsService)3 JsonArray (com.google.gson.JsonArray)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 ApplicationBundle (com.ibm.streamsx.rest.ApplicationBundle)2 InputPort (com.ibm.streamsx.rest.InputPort)2 OutputPort (com.ibm.streamsx.rest.OutputPort)2 StreamsConnection (com.ibm.streamsx.rest.StreamsConnection)2 File (java.io.File)2 BigInteger (java.math.BigInteger)2 Test (org.junit.Test)2 Gson (com.google.gson.Gson)1 JsonParser (com.google.gson.JsonParser)1 RESTException (com.ibm.streamsx.rest.RESTException)1