001package bluemix;
002
003import java.io.File;
004import java.math.BigInteger;
005import java.util.HashMap;
006import java.util.Map;
007
008import com.ibm.streamsx.topology.Topology;
009import com.ibm.streamsx.topology.context.AnalyticsServiceProperties;
010import com.ibm.streamsx.topology.context.JobProperties;
011import com.ibm.streamsx.topology.context.StreamsContext;
012import com.ibm.streamsx.topology.context.StreamsContext.Type;
013import com.ibm.streamsx.topology.context.StreamsContextFactory;
014import com.ibm.streamsx.topology.jobconfig.JobConfig;
015
016/**
017 * Sample demonstrating submission of a topology
018 * to Streaming Analytic service on IBM Cloud.. 
019 *
020 */
021public class Submit2StreamingAnalyticService {
022    
023    public static void main(String[] args) throws Exception {
024        
025        String vcapFile = args[0];
026        String serviceName = args[1];
027
028        /*
029         * Create a simple topology, focus of the
030         * sample is the submission of the job
031         * to the service.
032         * 
033         * This topology is just like simple.HelloWorld
034         * with different values printed to the console log.
035         */
036        Topology topology = new Topology("Submit2StreamingAnalyticService");
037        topology.strings(
038                "Hello", "Streaming Analytic Service",
039                serviceName, "running on IBM Cloud").print();       
040        
041        // Require a configuration object.
042        Map<String,Object> config = new HashMap<>();
043        
044        // Here the VCAP_SERVICES information is in a local file
045        // (as serialized JSON)
046        config.put(AnalyticsServiceProperties.VCAP_SERVICES, new File(vcapFile));
047        
048        // Explicitly state which service is the job will be submitted to
049        // The service must be in the Streaming Analytic Service section
050        // of the VCAP services.
051        config.put(AnalyticsServiceProperties.SERVICE_NAME, serviceName);
052        
053        // Optionally we can specify a job name
054        // (note job names must be unique within the instance).
055        JobConfig jco = new JobConfig();
056        jco.setJobName("StreamingAnalyticsSubmitSample");
057        
058        config.put(JobProperties.CONFIG, jco);
059        
060        // Submit to the ANALYICS_SERVICE context
061        @SuppressWarnings("unchecked")
062        StreamsContext<BigInteger> context =
063             (StreamsContext<BigInteger>) StreamsContextFactory.getStreamsContext(Type.STREAMING_ANALYTICS_SERVICE);
064        
065        BigInteger jobId = context.submit(topology, config).get();
066
067        System.out.println("Submitted job with jobId=" + jobId);
068        
069    }
070}