001/* 002# Licensed Materials - Property of IBM 003# Copyright IBM Corp. 2017 004 */ 005package rest; 006 007import java.io.IOException; 008import java.util.List; 009 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.ProcessingElement; 017import com.ibm.streamsx.rest.RESTException; 018import com.ibm.streamsx.rest.StreamsConnection; 019 020/** 021 * This is the main class to show how to use the StreamsConnectionInterface. 022 * 023 * <p> 024 * The example connects to the streams instance and gets a list of resources The 025 * objects printed out include all the fields known in the JSON object per the 026 * REST api however, the classes may not have accessors for all objects 027 * </p> 028 * 029 * <p> 030 * The following arguments are required for the sample: 031 * <ul> 032 * <li>userName</li> 033 * <li>password</li> 034 * <li>rest url for the IBM Streams Instance (streamstool geturl --api)</li> 035 * <li>instance name</li> 036 * </ul> 037 * </p> 038 */ 039public class StreamsConnectionSample { 040 041 public static void main(String[] args) throws IOException { 042 String userName = args[0]; 043 String authToken = args[1]; 044 String url = args[2]; 045 String instanceName = args[3]; 046 047 /* 048 * Create the connection to the instance indicated 049 */ 050 StreamsConnection sClient = StreamsConnection.createInstance(userName, authToken, 051 url); 052 /* 053 * This option is only used to by-pass the certificate certification 054 */ 055 if (args.length == 5 && "true".equals(args[4])) { 056 sClient.allowInsecureHosts(true); 057 } 058 059 try { 060 System.out.println("Instance: "); 061 Instance instance = sClient.getInstance(instanceName); 062 063 /* 064 * From the Instance, get a list of jobs 065 */ 066 List<Job> jobs = instance.getJobs(); 067 for (Job job : jobs) { 068 System.out.println("Job: " + job.toString()); 069 /* 070 * For each job, get a list of operators 071 */ 072 List<Operator> operators = job.getOperators(); 073 for (Operator op : operators) { 074 System.out.println("Operator: " + op.toString()); 075 List<Metric> metrics = op.getMetrics(); 076 /* 077 * For each operator, you can get a list of metrics, output 078 * ports and input ports 079 */ 080 for (Metric m : metrics) { 081 System.out.println("Metric: " + m.toString()); 082 } 083 List<OutputPort> outP = op.getOutputPorts(); 084 for (OutputPort oport : outP) { 085 System.out.println("Output Port: " + oport.toString()); 086 /* 087 * For each output port, you can get a list of metrics 088 */ 089 for (Metric om : oport.getMetrics()) { 090 System.out.println("Output Port Metric: " + om.toString()); 091 } 092 } 093 List<InputPort> inP = op.getInputPorts(); 094 /* 095 * For each input port, get a list of metrics 096 */ 097 for (InputPort ip : inP) { 098 System.out.println("Input Port: " + ip.toString()); 099 for (Metric im : ip.getMetrics()) { 100 System.out.println("Input Port Metric: " + im.toString()); 101 } 102 } 103 } 104 /* 105 * For each job, get a list of processing elements 106 */ 107 for (ProcessingElement pe : job.getPes()) { 108 System.out.println("ProcessingElement:" + pe.toString()); 109 } 110 } 111 112 try { 113 /* 114 * Get a specific job in the instance 115 */ 116 instance.getJob("99999"); 117 } catch (RESTException e) { 118 /* 119 * This shows what is available in the RESTException should 120 * something fail 121 */ 122 System.out.println("Status Code: " + e.getStatusCode()); 123 System.out.println("Message Id: " + e.getStreamsErrorMessageId()); 124 System.out.println("MessageAsJson: " + e.getStreamsErrorMessageAsJson().toString()); 125 System.out.println("Message: " + e.getMessage()); 126 } 127 } catch (Exception e) { 128 e.printStackTrace(); 129 } 130 } 131}