Search in sources :

Example 1 with SPLStream

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

the class JsonSubscriber method subscribe.

public static TStream<String> subscribe(Topology topo, String topic) {
    SPLStream splStream = SPLStreams.subscribe(topo, topic, JSONSchemas.JSON);
    TStream<String> stringStream = splStream.transform(new SplToTStream());
    return stringStream;
}
Also used : SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Example 2 with SPLStream

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

the class StreamImpl method publish.

@Override
public void publish(String topic, boolean allowFilter) {
    checkTopicName(topic);
    Type tupleType = getTupleType();
    if (JSONObject.class.equals(tupleType)) {
        filtersNotAllowed(allowFilter);
        @SuppressWarnings("unchecked") TStream<JSONObject> json = (TStream<JSONObject>) this;
        JSONStreams.toSPL(json).publish(topic, allowFilter);
        return;
    }
    BOperatorInvocation op;
    if (Schemas.usesDirectSchema(tupleType) || ((TStream<T>) this) instanceof SPLStream) {
        // would not allow a filter against.
        if (String.class != tupleType && !(((TStream<T>) this) instanceof SPLStream))
            filtersNotAllowed(allowFilter);
        // Publish as a stream consumable by SPL & Java/Scala
        Map<String, Object> publishParms = new HashMap<>();
        publishParms.put("topic", topic);
        publishParms.put("allowFilter", allowFilter);
        op = builder().addSPLOperator("Publish", "com.ibm.streamsx.topology.topic::Publish", publishParms);
    } else if (getTupleClass() != null) {
        filtersNotAllowed(allowFilter);
        // Publish as a stream consumable only by Java/Scala
        Map<String, Object> params = new HashMap<>();
        params.put("topic", topic);
        params.put("class", getTupleClass().getName());
        op = builder().addSPLOperator("Publish", "com.ibm.streamsx.topology.topic::PublishJava", params);
    } else {
        throw new IllegalStateException("A TStream with a tuple type that contains a generic or unknown type cannot be published");
    }
    SourceInfo.setSourceInfo(op, SPL.class);
    this.connectTo(op, false, null);
}
Also used : HashMap(java.util.HashMap) BOperatorInvocation(com.ibm.streamsx.topology.builder.BOperatorInvocation) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) TStream(com.ibm.streamsx.topology.TStream) Type(java.lang.reflect.Type) JSONObject(com.ibm.json.java.JSONObject) JSONObject(com.ibm.json.java.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 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 4 with SPLStream

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

the class JsonPublisher method publish.

public static void publish(TStream<String> jsonInputStream, String topic) {
    SPLStream splStream = SPLStreams.convertStream(jsonInputStream, new JsonToSpl(), JSONSchemas.JSON);
    splStream.publish(topic);
}
Also used : SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Example 5 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)

Aggregations

SPLStream (com.ibm.streamsx.topology.spl.SPLStream)86 Topology (com.ibm.streamsx.topology.Topology)66 Test (org.junit.Test)56 TestTopology (com.ibm.streamsx.topology.test.TestTopology)49 Tester (com.ibm.streamsx.topology.tester.Tester)40 List (java.util.List)38 HashMap (java.util.HashMap)33 StreamSchema (com.ibm.streams.operator.StreamSchema)25 OutputTuple (com.ibm.streams.operator.OutputTuple)20 Tuple (com.ibm.streams.operator.Tuple)19 Map (java.util.Map)17 TStream (com.ibm.streamsx.topology.TStream)15 SPL (com.ibm.streamsx.topology.spl.SPL)14 Condition (com.ibm.streamsx.topology.tester.Condition)14 TimeUnit (java.util.concurrent.TimeUnit)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