Search in sources :

Example 26 with SPLStream

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

the class Topology method subscribe.

/**
     * Declare a stream that is a subscription to {@code topic}.
     * A topic is published using {@link TStream#publish(String)}.
     * Subscribers are matched to published streams when the {@code topic}
     * is an exact match and the type of the stream ({@code T},
     * {@code tupleTypeClass}) is an exact match.
     * <BR>
     * Publish-subscribe is a many to many relationship,
     * multiple streams from multiple applications may
     * be published on the same topic and type. Multiple
     * subscribers may subscribe to a topic and type.
     * <BR>
     * A subscription will match all publishers using the
     * same topic and tuple type. Tuples on the published
     * streams will appear on the returned stream, as
     * a single stream.
     * <BR>
     * The subscription is dynamic, the returned stream
     * will subscribe to a matching stream published by
     * a newly submitted application (a job), and stops a
     * subscription when an running job is cancelled.
     * <P>
     * Publish-subscribe only works when the topology is
     * submitted to a {@link com.ibm.streamsx.topology.context.StreamsContext.Type#DISTRIBUTED}
     * context. This allows different applications (or
     * even within the same application) to communicate
     * using published streams.
     * </P>
     * <P>
     * If {@code tupleTypeClass} is {@code JSONObject.class} then the
     * subscription is the generic IBM Streams schema for JSON
     * ({@link JSONSchemas#JSON}). Streams of type {@code JSONObject}
     * are always published and subscribed using the generic schema
     * to allow interchange between applications implemented in
     * different languages.
     * </P>
     * @param topic Topic to subscribe to.
     * @param tupleTypeClass Type to subscribe to.
     * @return Stream the will contain tuples from matching publishers.
     * 
     * @see TStream#publish(String)
     * @see SPLStreams#subscribe(TopologyElement, String, com.ibm.streams.operator.StreamSchema)
     */
public <T> TStream<T> subscribe(String topic, Class<T> tupleTypeClass) {
    checkTopicFilter(topic);
    if (JSONObject.class.equals(tupleTypeClass)) {
        @SuppressWarnings("unchecked") TStream<T> json = (TStream<T>) SPLStreams.subscribe(this, topic, JSONSchemas.JSON).toJSON();
        return json;
    }
    StreamSchema mappingSchema = Schemas.getSPLMappingSchema(tupleTypeClass);
    SPLStream splImport;
    // Subscribed as an SPL Stream.
    if (Schemas.usesDirectSchema(tupleTypeClass)) {
        splImport = SPLStreams.subscribe(this, topic, mappingSchema);
    } else {
        Map<String, Object> params = new HashMap<>();
        params.put("topic", topic);
        params.put("class", tupleTypeClass.getName());
        params.put("streamType", mappingSchema);
        splImport = SPL.invokeSource(this, "com.ibm.streamsx.topology.topic::SubscribeJava", params, mappingSchema);
    }
    return new StreamImpl<T>(this, splImport.output(), tupleTypeClass);
}
Also used : HashMap(java.util.HashMap) StreamImpl(com.ibm.streamsx.topology.internal.core.StreamImpl) JSONObject(com.ibm.json.java.JSONObject) StreamSchema(com.ibm.streams.operator.StreamSchema) SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Example 27 with SPLStream

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

the class PublishSubscribeSPLTest method _testSPLPublishFilteredSubscribe.

private void _testSPLPublishFilteredSubscribe(String topic, boolean allowFilters) throws Exception {
    final Topology t = new Topology();
    SPL.addToolkit(t, new File(getTestRoot(), "spl/testtk"));
    SPLStream source = SPLStreamsTest.testTupleStream(t);
    source = addStartupDelay(source);
    source.publish(topic, allowFilters);
    // Filter on the vi attribute, passing the value 321.
    Map<String, Object> params = new HashMap<>();
    params.put("topic", topic);
    params.put("value", 43675232L);
    SPLStream sub = SPL.invokeSource(t, "testspl::Int64SubscribeFilter", params, source.getSchema());
    TStream<String> subscribe = sub.transform(new GetTupleId());
    completeAndValidate(subscribe, 20, "SPL:1");
}
Also used : HashMap(java.util.HashMap) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) File(java.io.File) SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Example 28 with SPLStream

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

the class SimpleEmbeddedTest method testSimple.

@Test
public void testSimple() throws Exception {
    Topology topology = new Topology("testSimple");
    TStream<String> hw = topology.strings("Hello", "World!", "Test!!");
    SPLStream hws = SPLStreams.stringToSPLStream(hw);
    Tester tester = topology.getTester();
    StreamCounter<Tuple> counter = tester.splHandler(hws, new StreamCounter<Tuple>());
    StreamCollector<LinkedList<Tuple>, Tuple> collector = tester.splHandler(hws, StreamCollector.newLinkedListCollector());
    StreamsContextFactory.getStreamsContext(StreamsContext.Type.EMBEDDED_TESTER).submit(topology).get();
    assertEquals(3, counter.getTupleCount());
    assertEquals("Hello", collector.getTuples().get(0).getString(0));
    assertEquals("World!", collector.getTuples().get(1).getString(0));
    assertEquals("Test!!", collector.getTuples().get(2).getString(0));
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Tuple(com.ibm.streams.operator.Tuple) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 29 with SPLStream

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

the class PublishSubscribeSPLTest method testSPLPublishNoFilterWithSubscribe.

/**
     * Test that with publish allow filter set to false
     * a subscriber without a filter gets the full set of data.
     */
@Test
public void testSPLPublishNoFilterWithSubscribe() throws Exception {
    final Topology t = new Topology();
    SPLStream source = SPLStreamsTest.testTupleStream(t);
    source = addStartupDelay(source);
    source.publish("testSPLPublishNoFilterSFilteredSubscribe", false);
    SPLStream sub = SPLStreams.subscribe(t, "testSPLPublishNoFilterSFilteredSubscribe", source.getSchema());
    TStream<String> subscribe = sub.transform(new GetTupleId());
    completeAndValidate(subscribe, 20, "SPL:0", "SPL:1", "SPL:2", "SPL:3");
}
Also used : Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Test(org.junit.Test) SPLStreamsTest(com.ibm.streamsx.topology.test.spl.SPLStreamsTest)

Example 30 with SPLStream

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

the class PublishSubscribeSPLTest method testSPLPublishAllowFilterWithSubscribe.

/**
     * Test that with publish allow filter set to false
     * a subscriber without a filter gets the full set of data.
     */
@Test
public void testSPLPublishAllowFilterWithSubscribe() throws Exception {
    final Topology t = new Topology();
    SPLStream source = SPLStreamsTest.testTupleStream(t);
    source = addStartupDelay(source);
    source.publish("testSPLPublishAllowFilterWithSubscribe", true);
    SPLStream sub = SPLStreams.subscribe(t, "testSPLPublishAllowFilterWithSubscribe", source.getSchema());
    TStream<String> subscribe = sub.transform(new GetTupleId());
    completeAndValidate(subscribe, 20, "SPL:0", "SPL:1", "SPL:2", "SPL:3");
}
Also used : Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Test(org.junit.Test) SPLStreamsTest(com.ibm.streamsx.topology.test.spl.SPLStreamsTest)

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