Search in sources :

Example 6 with StreamingAnalyticsService

use of com.ibm.streamsx.rest.StreamingAnalyticsService in project streamsx.topology by IBMStreams.

the class RemoteBuildAndSubmitRemoteContext method streamingAnalyticServiceFromDeploy.

/**
 * Get the StreamingAnalyticsService from deploy section of graph.
 * Used consistently when we need one for topology.
 */
public static StreamingAnalyticsService streamingAnalyticServiceFromDeploy(JsonObject deploy) throws IOException {
    final StreamingAnalyticsService sas;
    if (deploy.has(SERVICE_DEFINITION)) {
        sas = StreamingAnalyticsService.of(object(deploy, SERVICE_DEFINITION));
    } else {
        JsonObject vcapServices = VcapServices.getVCAPServices(deploy.get(VCAP_SERVICES));
        sas = StreamingAnalyticsService.of(vcapServices, jstring(deploy, SERVICE_NAME));
    }
    return sas;
}
Also used : StreamingAnalyticsService(com.ibm.streamsx.rest.StreamingAnalyticsService) JsonObject(com.google.gson.JsonObject)

Example 7 with StreamingAnalyticsService

use of com.ibm.streamsx.rest.StreamingAnalyticsService in project streamsx.topology by IBMStreams.

the class StreamingAnalyticsConnectionTest method getTestService.

static StreamingAnalyticsService getTestService() throws IOException {
    String serviceName = System.getenv("STREAMING_ANALYTICS_SERVICE_NAME");
    String vcapServices = System.getenv("VCAP_SERVICES");
    // if we don't have serviceName or vcapServices, skip the test
    assumeNotNull(serviceName, vcapServices);
    StreamingAnalyticsService service = StreamingAnalyticsService.of(null, null);
    Instance instance = service.getInstance();
    // bail if streaming analytics instance isn't up & running
    assumeTrue(instance.getStatus().equals("running"));
    return service;
}
Also used : StreamingAnalyticsService(com.ibm.streamsx.rest.StreamingAnalyticsService) Instance(com.ibm.streamsx.rest.Instance)

Example 8 with StreamingAnalyticsService

use of com.ibm.streamsx.rest.StreamingAnalyticsService in project streamsx.topology by IBMStreams.

the class StreamingAnalyticsConnectionTest method testVCAPOptions.

@Test
public void testVCAPOptions() throws Exception {
    String serviceName = System.getenv("STREAMING_ANALYTICS_SERVICE_NAME");
    String existingVCAP = System.getenv("VCAP_SERVICES");
    assumeNotNull(serviceName, existingVCAP);
    String vcapString;
    // VCAP_SERVICES can be either a string representing a json object, a
    // string representing a file or a JSON object
    // Let's check what we have and manipulate it to the other to confirm
    // the underlying functionality to decode VCAP_SERVICES is correct.
    // let's make sure we have something to work with
    System.out.println("Checking VCAP_SERVICES exists ...");
    assumeTrue(!existingVCAP.isEmpty());
    // is this a file?
    if (Files.isRegularFile(Paths.get(existingVCAP))) {
        System.out.println("convert file to json string ...");
        // we have a file, let's convert to a String and re-try
        vcapString = new String(Files.readAllBytes(Paths.get(existingVCAP)), StandardCharsets.UTF_8);
        StreamingAnalyticsService stringService = StreamingAnalyticsService.of(new JsonPrimitive(vcapString), serviceName);
        stringService.getInstance().refresh();
    } else {
        // let's convert to a file and try that
        System.out.println("convert json string to file ...");
        Path tempPath = Files.createTempFile("vCapTest", ".json");
        File tempFile = tempPath.toFile();
        Files.write(tempPath, existingVCAP.getBytes(StandardCharsets.UTF_8));
        tempFile.deleteOnExit();
        StreamingAnalyticsService fileService = StreamingAnalyticsService.of(new JsonPrimitive(tempPath.toString()), serviceName);
        fileService.getInstance().refresh();
        // need this setup later
        vcapString = existingVCAP;
    }
    // let's try a fake service name
    System.out.println("Try a non-existant service name ...");
    try {
        StreamingAnalyticsService.of(new JsonPrimitive(vcapString), "FakeServiceName");
        fail("Worked with non-existant service name!");
    } catch (IllegalStateException e) {
    // should trigger an IllegalStateException exception here
    // System.err.println("Expecting an exception here - FakeServiceName");
    // e.printStackTrace();
    }
    // let's try a non-existant file
    System.out.println("Try a non-existant file ...");
    Path tempPath = Paths.get("nonExistantFileName");
    File tempFile = tempPath.toFile();
    tempFile.deleteOnExit();
    if (Files.isRegularFile(Paths.get(tempPath.toString()))) {
        fail("Non-existant file exists!!");
    }
    try {
        StreamingAnalyticsService.of(new JsonPrimitive(tempPath.toString()), serviceName);
        fail("Worked with non-existant file!");
    } catch (IllegalStateException e) {
    // should trigger an IllegalStateException exception here
    // as the file isn't a file, and we'll throw when trying to act on a json string
    }
    // let's try bad json
    String badJson = "{ extraCharacters, " + vcapString;
    // write junk to the file
    Files.createFile(tempPath);
    Files.write(tempPath, badJson.getBytes(StandardCharsets.UTF_8));
    if (!Files.isRegularFile(Paths.get(tempPath.toString()))) {
        fail("Could not write to tempFile!!");
    }
    try {
        StreamingAnalyticsService.of(new JsonPrimitive(tempPath.toString()), serviceName);
        fail("Worked with bad json!");
    } catch (com.google.gson.JsonSyntaxException e) {
    // should trigger an exception here
    }
    // use bad json as a string
    try {
        StreamingAnalyticsService.of(new JsonPrimitive(badJson), serviceName);
        fail("Worked with bad json as string!");
    } catch (com.google.gson.JsonSyntaxException e) {
    // should trigger an exception here
    }
}
Also used : Path(java.nio.file.Path) StreamingAnalyticsService(com.ibm.streamsx.rest.StreamingAnalyticsService) JsonPrimitive(com.google.gson.JsonPrimitive) File(java.io.File) Test(org.junit.Test)

Example 9 with StreamingAnalyticsService

use of com.ibm.streamsx.rest.StreamingAnalyticsService in project streamsx.topology by IBMStreams.

the class StreamingAnalyticsConnectionSample method main.

public static void main(String[] args) {
    String credentials = args[0];
    String serviceName = args[1];
    System.out.println(credentials);
    System.out.println(serviceName);
    try {
        StreamingAnalyticsService sClient = StreamingAnalyticsService.of(new JsonPrimitive(credentials), serviceName);
        Instance instance = sClient.getInstance();
        System.out.println("Instance:" + instance.toString());
        if (!instance.getStatus().equals("running")) {
            System.out.println("Instance is not started, please start the instance to retrieve more information");
            System.exit(0);
        }
        /* Retrieve a list of jobs in the instance */
        List<Job> jobs = instance.getJobs();
        for (Job job : jobs) {
            System.out.println("Job: " + job.toString());
            /* Retrieve a list of operators for the current job */
            List<Operator> operators = job.getOperators();
            for (Operator op : operators) {
                System.out.println("Operator: " + op.toString());
                /* Retrieve a list of metrics for the current operator */
                List<Metric> metrics = op.getMetrics();
                for (Metric m : metrics) {
                    System.out.println("Metric: " + m.toString());
                }
                /*
                     * Retrieve a list of output ports for the current operator
                     */
                List<OutputPort> outP = op.getOutputPorts();
                for (OutputPort oport : outP) {
                    System.out.println("Output Port: " + oport.toString());
                    /* Retrieve the metrics for this output port */
                    for (Metric om : oport.getMetrics()) {
                        System.out.println("Output Port Metric: " + om.toString());
                    }
                }
                /*
                     * Retrieve a list of input ports for the current operator
                     */
                List<InputPort> inP = op.getInputPorts();
                for (InputPort ip : inP) {
                    System.out.println("Input Port: " + ip.toString());
                    /* Retrieve the metrics for this input port */
                    for (Metric im : ip.getMetrics()) {
                        System.out.println("Input Port Metric: " + im.toString());
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Operator(com.ibm.streamsx.rest.Operator) OutputPort(com.ibm.streamsx.rest.OutputPort) StreamingAnalyticsService(com.ibm.streamsx.rest.StreamingAnalyticsService) JsonPrimitive(com.google.gson.JsonPrimitive) Instance(com.ibm.streamsx.rest.Instance) InputPort(com.ibm.streamsx.rest.InputPort) Metric(com.ibm.streamsx.rest.Metric) Job(com.ibm.streamsx.rest.Job)

Aggregations

StreamingAnalyticsService (com.ibm.streamsx.rest.StreamingAnalyticsService)9 JsonObject (com.google.gson.JsonObject)4 Instance (com.ibm.streamsx.rest.Instance)4 JsonPrimitive (com.google.gson.JsonPrimitive)3 Job (com.ibm.streamsx.rest.Job)3 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 ActiveVersion (com.ibm.streamsx.rest.ActiveVersion)1 InputPort (com.ibm.streamsx.rest.InputPort)1 Metric (com.ibm.streamsx.rest.Metric)1 Operator (com.ibm.streamsx.rest.Operator)1 OutputPort (com.ibm.streamsx.rest.OutputPort)1 File (java.io.File)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 Path (java.nio.file.Path)1 Test (org.junit.Test)1