Source code

    001/*
    002# Licensed Materials - Property of IBM
    003# Copyright IBM Corp. 2017
    004 */
    005package rest;
    006
    007import java.util.List;
    008
    009import com.google.gson.JsonPrimitive;
    010import com.ibm.streamsx.rest.InputPort;
    011import com.ibm.streamsx.rest.Instance;
    012import com.ibm.streamsx.rest.Job;
    013import com.ibm.streamsx.rest.Metric;
    014import com.ibm.streamsx.rest.Operator;
    015import com.ibm.streamsx.rest.OutputPort;
    016import com.ibm.streamsx.rest.StreamingAnalyticsService;
    017
    018/**
    019 * Sample code to show how to access a Streaming Analytics Instance through the
    020 * REST Api
    021 *
    022 * <p>
    023 * The example connects to the streams instance and gets a list of resources.
    024 * The objects printed out include all the fields known in the JSON object per
    025 * the REST api however, the classes may not have accessors for all objects.
    026 * </p>
    027 *
    028 * <p>
    029 * The following arguments are required for the sample:
    030 * <ul>
    031 * <li>credentials (as an absolute file path, or JSON string)</li>
    032 * <li>serviceName identifying the credentials to use in the file</li>
    033 * </ul>
    034 * </p>
    035 *
    036 */
    037public class StreamingAnalyticsConnectionSample {
    038
    039    public static void main(String[] args) {
    040        String credentials = args[0];
    041        String serviceName = args[1];
    042
    043        System.out.println(credentials);
    044        System.out.println(serviceName);
    045
    046        try {
    047            StreamingAnalyticsService sClient = StreamingAnalyticsService.of(
    048                    new JsonPrimitive(credentials),
    049                    serviceName);
    050
    051            Instance instance = sClient.getInstance();
    052            System.out.println("Instance:" + instance.toString());
    053
    054            if (!instance.getStatus().equals("running")) {
    055                System.out.println("Instance is not started, please start the instance to retrieve more information");
    056                System.exit(0);
    057            }
    058            /* Retrieve a list of jobs in the instance */
    059            List<Job> jobs = instance.getJobs();
    060            for (Job job : jobs) {
    061                System.out.println("Job: " + job.toString());
    062                /* Retrieve a list of operators for the current job */
    063                List<Operator> operators = job.getOperators();
    064                for (Operator op : operators) {
    065                    System.out.println("Operator: " + op.toString());
    066                    /* Retrieve a list of metrics for the current operator */
    067                    List<Metric> metrics = op.getMetrics();
    068                    for (Metric m : metrics) {
    069                        System.out.println("Metric: " + m.toString());
    070                    }
    071                    /*
    072                     * Retrieve a list of output ports for the current operator
    073                     */
    074                    List<OutputPort> outP = op.getOutputPorts();
    075                    for (OutputPort oport : outP) {
    076                        System.out.println("Output Port: " + oport.toString());
    077                        /* Retrieve the metrics for this output port */
    078                        for (Metric om : oport.getMetrics()) {
    079                            System.out.println("Output Port Metric: " + om.toString());
    080                        }
    081                    }
    082                    /*
    083                     * Retrieve a list of input ports for the current operator
    084                     */
    085                    List<InputPort> inP = op.getInputPorts();
    086                    for (InputPort ip : inP) {
    087                        System.out.println("Input Port: " + ip.toString());
    088                        /* Retrieve the metrics for this input port */
    089                        for (Metric im : ip.getMetrics()) {
    090                            System.out.println("Input Port Metric: " + im.toString());
    091                        }
    092                    }
    093                }
    094            }
    095
    096        } catch (Exception e) {
    097            e.printStackTrace();
    098        }
    099    }
    100}