Search in sources :

Example 6 with SPLStream

use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.

the class MqttStreams method subscribe.

/**
     * Subscribe to a MQTT topic and create a stream of messages.
     * <p>
     * The quality of service for handling each topic is
     * the value of configuration property {@code defaultQOS}.
     * <p> 
     * N.B., A topology that includes this will not support
     * {@code StreamsContext.Type.EMBEDDED}.
     * <p>
     * N.B. due to com.ibm.streamsx.messaging 
     * <a href="https://github.com/IBMStreams/streamsx.messaging/issues/124">issue#124</a>,
     * terminating a {@code StreamsContext.Type.STANDALONE} topology may result
     * in ERROR messages and a stranded standalone process.
     *
     * @param topic the MQTT topic.  May be a submission parameter.
     * @return TStream&lt;Message>
     *      The generated {@code Message} tuples have a non-null {@code topic}.
     *      The tuple's {@code key} will be null. 
     * @throws IllegalArgumentException if topic is null.
     * @see Value
     * @see Topology#createSubmissionParameter(String, Class)
    */
public TStream<Message> subscribe(Supplier<String> topic) {
    if (topic == null)
        throw new IllegalArgumentException("topic");
    Map<String, Object> params = new HashMap<>();
    params.put("reconnectionBound", -1);
    params.put("qos", 0);
    params.putAll(Util.configToSplParams(config));
    params.remove("retain");
    params.put("topics", topic);
    params.put("topicOutAttrName", "topic");
    params.put("dataAttributeName", "message");
    if (++opCnt > 1) {
        // each op requires its own clientID
        String clientId = (String) params.get("clientID");
        if (clientId != null && clientId.length() > 0)
            params.put("clientID", opCnt + "-" + clientId);
    }
    // Use SPL.invoke to avoid adding a compile time dependency
    // to com.ibm.streamsx.messaging since JavaPrimitive.invoke*()
    // lack "kind" based variants.
    String kind = "com.ibm.streamsx.messaging.mqtt::MQTTSource";
    String className = "com.ibm.streamsx.messaging.mqtt.MqttSourceOperator";
    SPLStream rawMqtt = SPL.invokeSource(te, kind, params, MqttSchemas.MQTT);
    SPL.tagOpAsJavaPrimitive(toOp(rawMqtt), kind, className);
    TStream<Message> rcvdMsgs = toMessageStream(rawMqtt);
    rcvdMsgs.colocate(rawMqtt);
    return rcvdMsgs;
}
Also used : SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) HashMap(java.util.HashMap) SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Example 7 with SPLStream

use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.

the class ParallelTest method testParallelSubmissionParamInner.

@Test
public void testParallelSubmissionParamInner() throws Exception {
    checkUdpSupported();
    Topology topology = newTopology("testParallelSubmissionParamInner");
    final int count = new Random().nextInt(1000) + 37;
    String submissionWidthName = "width";
    Integer submissionWidth = 5;
    String submissionAppendName = "append";
    boolean submissionAppend = true;
    String submissionFlushName = "flush";
    Integer submissionFlush = 1;
    String submissionThresholdName = "threshold";
    String submissionDefaultedThresholdName = "defaultedTreshold";
    Integer submissionThreshold = -1;
    // getConfig().put(ContextProperties.KEEP_ARTIFACTS, true);
    Supplier<Integer> threshold = topology.createSubmissionParameter(submissionThresholdName, Integer.class);
    Supplier<Integer> defaultedThreshold = topology.createSubmissionParameter(submissionDefaultedThresholdName, submissionThreshold);
    TStream<BeaconTuple> fb = BeaconStreams.beacon(topology, count);
    TStream<BeaconTuple> pb = fb.parallel(topology.createSubmissionParameter(submissionWidthName, Integer.class));
    TStream<Integer> is = pb.transform(randomHashProducer());
    // submission param use within a parallel region
    StreamSchema schema = getStreamSchema("tuple<int32 i>");
    SPLStream splStream = SPLStreams.convertStream(is, cvtMsgFunc(), schema);
    File tmpFile = File.createTempFile("parallelTest", null);
    tmpFile.deleteOnExit();
    Map<String, Object> splParams = new HashMap<>();
    splParams.put("file", tmpFile.getAbsolutePath());
    splParams.put("append", topology.createSubmissionParameter(submissionAppendName, submissionAppend));
    splParams.put("flush", SPL.createSubmissionParameter(topology, submissionFlushName, SPL.createValue(0, Type.MetaType.UINT32), false));
    SPL.invokeSink("spl.adapter::FileSink", splStream, splParams);
    // use a submission parameter in "inner" functional logic
    is = is.filter(thresholdFilter(threshold));
    is = is.filter(thresholdFilter(defaultedThreshold));
    // avoid another parallel impl limitation noted in issue#173
    is = is.filter(passthru());
    TStream<Integer> joined = is.endParallel();
    TStream<String> numRegions = joined.transform(uniqueIdentifierMap(count));
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(numRegions, 1);
    Condition<List<String>> regionCount = tester.stringContents(numRegions, submissionWidth.toString());
    Map<String, Object> params = new HashMap<>();
    params.put(submissionWidthName, submissionWidth);
    params.put(submissionFlushName, submissionFlush);
    params.put(submissionThresholdName, submissionThreshold);
    getConfig().put(ContextProperties.SUBMISSION_PARAMS, params);
    complete(tester, allConditions(expectedCount, regionCount), 10, TimeUnit.SECONDS);
    assertTrue(expectedCount.valid());
    assertTrue(regionCount.valid());
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) HashMap(java.util.HashMap) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) StreamSchema(com.ibm.streams.operator.StreamSchema) Factory.getStreamSchema(com.ibm.streams.operator.Type.Factory.getStreamSchema) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Random(java.util.Random) BeaconTuple(com.ibm.streamsx.topology.tuple.BeaconTuple) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) Test(org.junit.Test)

Example 8 with SPLStream

use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.

the class JobConfigSubmissionTest method testItDirect.

private List<String> testItDirect(String topologyName, JobConfig config) throws Exception {
    // JobConfig only apply to DISTRIBUTED submit
    assumeTrue(getTesterType() == StreamsContext.Type.DISTRIBUTED_TESTER);
    assumeTrue(SC_OK);
    config.addToConfig(getConfig());
    Topology topology = newTopology(topologyName);
    topology.addClassDependency(JobPropertiesTestOp.class);
    SPLStream sourceSPL = JavaPrimitive.invokeJavaPrimitiveSource(topology, JobPropertiesTestOp.class, Schemas.STRING, null);
    TStream<String> source = sourceSPL.toStringStream();
    Condition<Long> end = topology.getTester().tupleCount(source, 4);
    Condition<List<String>> result = topology.getTester().stringContents(source);
    complete(topology.getTester(), end, 10, TimeUnit.SECONDS);
    return result.getResult();
}
Also used : List(java.util.List) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Example 9 with SPLStream

use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.

the class JSONStreamsTest method checkJsonOutput.

private JSONArtifact checkJsonOutput(JSONArtifact expected, TStream<String> jsonString) throws Exception, IOException {
    assertEquals(String.class, jsonString.getTupleClass());
    assertEquals(String.class, jsonString.getTupleType());
    SPLStream splS = SPLStreams.stringToSPLStream(jsonString);
    MostRecent<Tuple> mr = jsonString.topology().getTester().splHandler(splS, new MostRecent<Tuple>());
    Condition<Long> singleTuple = jsonString.topology().getTester().tupleCount(splS, 1);
    complete(jsonString.topology().getTester(), singleTuple, 10, TimeUnit.SECONDS);
    JSONArtifact rv = JSON.parse(mr.getMostRecentTuple().getString(0));
    assertEquals(expected, rv);
    return rv;
}
Also used : JSONArtifact(com.ibm.json.java.JSONArtifact) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Tuple(com.ibm.streams.operator.Tuple)

Example 10 with SPLStream

use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.

the class SubscribeSPLDictTest method testSubscribeSPLDictMap.

@Test
public void testSubscribeSPLDictMap() throws Exception {
    Topology topology = new Topology("testSubscribeMap");
    // Publish a stream with all the SPL types supported by Python including sets
    SPLStream tuples = PythonFunctionalOperatorsTest.testTupleStream(topology, true);
    tuples = addStartupDelay(tuples);
    tuples.publish("pytest/spl/map");
    SPLStream viaSPL = SPL.invokeOperator("spl.relational::Functor", tuples, tuples.getSchema(), null);
    // Python that subscribes to the SPL tuple stream and then republishes as Json.
    includePythonApp(topology, "spl_map_json.py", "spl_map_json::spl_map_json");
    TStream<JSONObject> viaPythonJson = topology.subscribe("pytest/spl/map/result", JSONObject.class);
    SPLStream viaPythonJsonSpl = JSONStreams.toSPL(viaPythonJson.isolate());
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(viaPythonJsonSpl, TUPLE_COUNT);
    Condition<Long> expectedCountSpl = tester.tupleCount(viaSPL, TUPLE_COUNT);
    Condition<List<Tuple>> viaSPLResult = tester.tupleContents(viaSPL);
    Condition<List<Tuple>> viaPythonResult = tester.tupleContents(viaPythonJsonSpl);
    complete(tester, allConditions(expectedCount, expectedCountSpl), 60, TimeUnit.SECONDS);
    System.out.println(expectedCount.getResult());
    System.out.println(expectedCountSpl.getResult());
    assertTrue(expectedCount.valid());
    assertTrue(expectedCountSpl.valid());
    List<Tuple> splResults = viaSPLResult.getResult();
    List<Tuple> pyJsonResults = viaPythonResult.getResult();
    assertEquals(TUPLE_COUNT, splResults.size());
    assertEquals(TUPLE_COUNT, pyJsonResults.size());
    for (int i = 0; i < TUPLE_COUNT; i++) {
        Tuple spl = splResults.get(i);
        JSONObject json = (JSONObject) JSON.parse(pyJsonResults.get(i).getString("jsonString"));
        System.out.println(spl);
        System.out.println(pyJsonResults.get(i).getString("jsonString"));
        assertEquals(spl.getBoolean("b"), ((Boolean) json.get("b")).booleanValue());
        assertEquals(spl.getInt("i8"), ((Number) json.get("i8")).intValue());
        assertEquals(spl.getInt("i16"), ((Number) json.get("i16")).intValue());
        assertEquals(spl.getInt("i32"), ((Number) json.get("i32")).intValue());
        assertEquals(spl.getLong("i64"), ((Number) json.get("i64")).longValue());
        assertEquals(spl.getString("r"), json.get("r").toString());
        assertEquals(spl.getDouble("f32"), ((Number) json.get("f32")).doubleValue(), 0.1);
        assertEquals(spl.getDouble("f64"), ((Number) json.get("f64")).doubleValue(), 0.1);
        {
            List<?> ex = spl.getList("li32");
            JSONArray pya = (JSONArray) json.get("li32");
            assertEquals(ex.size(), pya.size());
            for (int j = 0; j < ex.size(); j++) {
                assertEquals(ex.get(j), ((Number) pya.get(j)).intValue());
            }
        }
        {
            Set<?> ex = spl.getSet("si32");
            JSONArray pya = (JSONArray) json.get("si32");
            assertEquals(ex.size(), pya.size());
            setAssertHelper(ex, pya);
        }
    }
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) Set(java.util.Set) TreeSet(java.util.TreeSet) JSONArray(com.ibm.json.java.JSONArray) Topology(com.ibm.streamsx.topology.Topology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) JSONObject(com.ibm.json.java.JSONObject) List(java.util.List) Tuple(com.ibm.streams.operator.Tuple) PythonFunctionalOperatorsTest(com.ibm.streamsx.topology.test.splpy.PythonFunctionalOperatorsTest) Test(org.junit.Test)

Aggregations

SPLStream (com.ibm.streamsx.topology.spl.SPLStream)72 Topology (com.ibm.streamsx.topology.Topology)53 Test (org.junit.Test)46 TestTopology (com.ibm.streamsx.topology.test.TestTopology)36 Tester (com.ibm.streamsx.topology.tester.Tester)34 List (java.util.List)31 HashMap (java.util.HashMap)27 OutputTuple (com.ibm.streams.operator.OutputTuple)17 Tuple (com.ibm.streams.operator.Tuple)16 StreamSchema (com.ibm.streams.operator.StreamSchema)15 TStream (com.ibm.streamsx.topology.TStream)15 Map (java.util.Map)14 Constants (com.ibm.streamsx.kafka.test.utils.Constants)12 KafkaSPLStreamsUtils (com.ibm.streamsx.kafka.test.utils.KafkaSPLStreamsUtils)12 StreamsContext (com.ibm.streamsx.topology.context.StreamsContext)12 Type (com.ibm.streamsx.topology.context.StreamsContext.Type)12 StreamsContextFactory (com.ibm.streamsx.topology.context.StreamsContextFactory)12 SPL (com.ibm.streamsx.topology.spl.SPL)12 SPLStreams (com.ibm.streamsx.topology.spl.SPLStreams)12 Condition (com.ibm.streamsx.topology.tester.Condition)12