Search in sources :

Example 36 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 37 with SPLStream

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

the class HTTPStreams method getJSON.

/**
     * Periodically poll a web service using HTTP {@code GET} for
     * {@code application/json} data. Declares a source stream that will contain
     * a single tuple for each successful {@code GET}. The tuple is the complete
     * JSON ({@code application/json} content) returned by the request.
     * 
     * @param te
     *            Topology the source stream will be contained in.
     * @param url
     *            URL to poll.
     * @param period
     *            Poling period.
     * @param unit
     *            Unit for {@code period}.
     * @return Stream that will contain the JSON tuples from periodic HTTP
     *         {@code GET} requests.
     */
public static TStream<JSONObject> getJSON(TopologyElement te, String url, long period, TimeUnit unit) {
    Map<String, Object> params = new HashMap<>();
    params.put("url", url);
    double dperiod = (unit.toMillis(period) / 1000.0);
    params.put("period", dperiod);
    SPLStream rawJson = SPL.invokeSource(te, "com.ibm.streamsx.inet.http::HTTPGetJSONContent", params, JSONSchemas.JSON);
    TStream<String> string = SPLStreams.toStringStream(rawJson);
    return JSONStreams.deserialize(string);
}
Also used : HashMap(java.util.HashMap) JSONObject(com.ibm.json.java.JSONObject) SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Example 38 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 39 with SPLStream

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

the class KafkaConsumer method subscribe.

/**
     * Subscribe to a topic and create a stream of messages.
     * <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/118">issue#118</a>,
     * multiple consumers will have issues in
     * {@code StreamsContext.Type.STANDALONE}.
     * <p>
     * N.B. due to com.ibm.streamsx.messaging 
     * <a href="https://github.com/IBMStreams/streamsx.messaging/issues/117">issue#117</a>,
     * a consumer in {@code StreamsContext.Type.STANDALONE} subsequently results
     * in an orphaned @{code standalone} processes that continues as the lead
     * group/topic consumer thereby preventing subsequent instances of the
     * group/topic consumer from receiving messages. 
     * <p>
     * N.B. due to com.ibm.streamsx.messaging 
     * <a href="https://github.com/IBMStreams/streamsx.messaging/issues/114">issue#114</a>,
     * a consumer essentially ignores messages generated by producers where the
     * optional {@code key} is {@code null}.
     * e.g., Kafka's {@code kafka-console-producer.sh} tool generates
     * {@code key==null} messages.
     *
     * @param threadsPerTopic number of threads to allocate to processing each
     *        topic.  May be a submission parameter.
     * @param topic the topic to subscribe to.  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 if the Kafka message
     *      lacked a key or it's key was the empty string. 
     * @throws IllegalArgumentException if topic is null.
     * @throws IllegalArgumentException if threadsPerTopic is null.
     * @see Value
     * @see Topology#createSubmissionParameter(String, Class)
     */
public TStream<Message> subscribe(Supplier<Integer> threadsPerTopic, Supplier<String> topic) {
    if (topic == null)
        throw new IllegalArgumentException("topic");
    if (threadsPerTopic == null || (threadsPerTopic.get() != null && threadsPerTopic.get() <= 0))
        throw new IllegalArgumentException("threadsPerTopic");
    Map<String, Object> params = new HashMap<>();
    params.put("topic", topic);
    if (threadsPerTopic instanceof Value && threadsPerTopic.get() == 1)
        // The default is one.
        ;
    else
        params.put("threadsPerTopic", threadsPerTopic);
    if (!config.isEmpty())
        params.put("kafkaProperty", Util.toKafkaProperty(config));
    // workaround streamsx.messaging issue #107
    params.put("propertiesFile", PROP_FILE_PARAM);
    addPropertiesFile();
    // 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.kafka::KafkaConsumer";
    String className = "com.ibm.streamsx.messaging.kafka.KafkaSource";
    SPLStream rawKafka = SPL.invokeSource(te, kind, params, KafkaSchemas.KAFKA);
    SPL.tagOpAsJavaPrimitive(toOp(rawKafka), kind, className);
    TStream<Message> rcvdMsgs = toMessageStream(rawKafka);
    rcvdMsgs.colocate(rawKafka);
    // workaround streamsx.messaging issue#118 w/java8
    // isolate even in the single consumer case since we don't
    // know if others may be subsequently created.
    rcvdMsgs = rcvdMsgs.isolate();
    return rcvdMsgs;
}
Also used : SimpleMessage(com.ibm.streamsx.topology.tuple.SimpleMessage) Message(com.ibm.streamsx.topology.tuple.Message) HashMap(java.util.HashMap) Value(com.ibm.streamsx.topology.logic.Value) SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Example 40 with SPLStream

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

the class MqttStreams method publish.

/**
     * Publish {@code stream} tuples to one or more MQTT topics.
     * <p>
     * If {@code topic} is null, each tuple is published to the topic
     * specified by its {@link Message#getTopic()}.
     * Otherwise, all tuples are published to {@code topic}.
     * <p>
     * The messages added to MQTT include a topic and message.
     * The {@link Message#getKey()} field is ignored.
     * <p>
     * The message is handled with the quality of service
     * indicated by configuration property {@code defaultQOS}.
     * 
     * @param stream the stream to publish
     * @param topic topic to publish to.  May be a submission parameter. May be null.
     * @return the sink element
     * @see Value
     * @see Topology#createSubmissionParameter(String, Class)
     */
public TSink publish(TStream<? extends Message> stream, Supplier<String> topic) {
    stream = stream.lowLatency();
    @SuppressWarnings("unchecked") SPLStream splStream = SPLStreams.convertStream((TStream<Message>) stream, cvtMsgFunc(topic), MqttSchemas.MQTT);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("reconnectionBound", -1);
    params.put("qos", 0);
    params.putAll(Util.configToSplParams(config));
    params.remove("messageQueueSize");
    if (topic == null)
        params.put("topicAttributeName", "topic");
    else
        params.put("topic", 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::MQTTSink";
    String className = "com.ibm.streamsx.messaging.kafka.MqttSinkOperator";
    TSink sink = SPL.invokeSink(kind, splStream, params);
    SPL.tagOpAsJavaPrimitive(sink.operator(), kind, className);
    return sink;
}
Also used : TSink(com.ibm.streamsx.topology.TSink) 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)

Aggregations

SPLStream (com.ibm.streamsx.topology.spl.SPLStream)57 Topology (com.ibm.streamsx.topology.Topology)39 TestTopology (com.ibm.streamsx.topology.test.TestTopology)36 Test (org.junit.Test)32 Tester (com.ibm.streamsx.topology.tester.Tester)20 List (java.util.List)17 Tuple (com.ibm.streams.operator.Tuple)15 HashMap (java.util.HashMap)15 OutputTuple (com.ibm.streams.operator.OutputTuple)10 File (java.io.File)10 JSONObject (com.ibm.json.java.JSONObject)9 StreamSchema (com.ibm.streams.operator.StreamSchema)9 Message (com.ibm.streamsx.topology.tuple.Message)4 LinkedList (java.util.LinkedList)4 Random (java.util.Random)4 TSink (com.ibm.streamsx.topology.TSink)3 SimpleMessage (com.ibm.streamsx.topology.tuple.SimpleMessage)3 ArrayList (java.util.ArrayList)3 SPLStreamsTest (com.ibm.streamsx.topology.test.spl.SPLStreamsTest)2 Map (java.util.Map)2