Search in sources :

Example 41 with SPLStream

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

the class KafkaOperatorsStartPositionTest method kafkaStartPositionTest.

@Test
public void kafkaStartPositionTest() throws Exception {
    Topology producerTopo = createTopology("producerTopo");
    // create the producer (produces tuples after a short delay)
    Map<String, Object> producerProps = new HashMap<>();
    producerProps.put("topic", Constants.TOPIC_POS);
    producerProps.put("propertiesFile", Constants.PROPERTIES_FILE_PATH);
    TStream<String> stringSrcStream = producerTopo.strings(Constants.STRING_DATA);
    SPL.invokeSink(Constants.KafkaProducerOp, KafkaSPLStreamsUtils.convertStreamToKafkaTuple(stringSrcStream), producerProps);
    // Launch the producer and allow it to finish writing
    // the data to the queue. Consumer should run AFTER
    // producer is finished.
    @SuppressWarnings("unchecked") Future<BigInteger> future = (Future<BigInteger>) StreamsContextFactory.getStreamsContext(Type.STANDALONE).submit(producerTopo);
    future.get();
    Thread.sleep(TimeUnit.SECONDS.toMillis(20));
    if (!future.isDone()) {
        future.cancel(true);
    }
    // create the consumer
    Topology topo = getTopology();
    Map<String, Object> consumerParams = new HashMap<>();
    consumerParams.put("topic", Constants.TOPIC_POS);
    consumerParams.put("propertiesFile", Constants.PROPERTIES_FILE_PATH);
    consumerParams.put("startPosition", StartPosition.Beginning);
    SPLStream consumerStream = SPL.invokeSource(topo, Constants.KafkaConsumerOp, consumerParams, KafkaSPLStreamsUtils.STRING_SCHEMA);
    SPLStream msgStream = SPLStreams.stringToSPLStream(consumerStream.convert(t -> t.getString("message")));
    // test the output of the consumer
    StreamsContext<?> context = StreamsContextFactory.getStreamsContext(Type.DISTRIBUTED_TESTER);
    Tester tester = topo.getTester();
    Condition<List<String>> condition = KafkaSPLStreamsUtils.stringContentsUnordered(tester, msgStream, Constants.STRING_DATA);
    tester.complete(context, new HashMap<>(), condition, 30, TimeUnit.SECONDS);
    // check the results
    Assert.assertTrue(condition.getResult().size() > 0);
    Assert.assertTrue(condition.getResult().toString(), condition.valid());
}
Also used : TStream(com.ibm.streamsx.topology.TStream) Tester(com.ibm.streamsx.topology.tester.Tester) StreamsContextFactory(com.ibm.streamsx.topology.context.StreamsContextFactory) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) HashMap(java.util.HashMap) Test(org.junit.Test) KafkaSPLStreamsUtils(com.ibm.streamsx.kafka.test.utils.KafkaSPLStreamsUtils) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Future(java.util.concurrent.Future) Topology(com.ibm.streamsx.topology.Topology) StreamsContext(com.ibm.streamsx.topology.context.StreamsContext) Constants(com.ibm.streamsx.kafka.test.utils.Constants) Map(java.util.Map) SPL(com.ibm.streamsx.topology.spl.SPL) Condition(com.ibm.streamsx.topology.tester.Condition) Type(com.ibm.streamsx.topology.context.StreamsContext.Type) SPLStreams(com.ibm.streamsx.topology.spl.SPLStreams) BigInteger(java.math.BigInteger) Assert(org.junit.Assert) Tester(com.ibm.streamsx.topology.tester.Tester) HashMap(java.util.HashMap) Topology(com.ibm.streamsx.topology.Topology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) BigInteger(java.math.BigInteger) Future(java.util.concurrent.Future) List(java.util.List) Test(org.junit.Test)

Example 42 with SPLStream

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

the class KafkaOperatorsTopicPartitionTest method kafkaTopicPartitionTest.

@Test
public void kafkaTopicPartitionTest() throws Exception {
    Topology topo = getTopology();
    topo.addFileDependency("etc/custom_partitioner.properties", "etc");
    topo.addFileDependency("etc/custompartitioner.jar", "etc");
    // create producer
    TStream<Message<Integer, String>> src = topo.limitedSource(new MySupplier(), 9).modify(new Delay<>(Constants.PRODUCER_DELAY));
    SPLStream outStream = SPLStreams.convertStream(src, new MessageConverter(), SCHEMA);
    SPL.invokeSink(Constants.KafkaProducerOp, outStream, getKafkaProducerParams());
    // create the consumers
    SPLStream msgStream1 = createConsumer(topo, 0);
    SPLStream msgStream2 = createConsumer(topo, 1);
    SPLStream msgStream3 = createConsumer(topo, 2);
    Set<TStream<Tuple>> s = new HashSet<>();
    s.add(msgStream2);
    s.add(msgStream3);
    TStream<String> unionStream = msgStream1.union(s).transform(t -> t.getString("message"));
    SPLStream msgStream = SPLStreams.stringToSPLStream(unionStream);
    StreamsContext<?> context = StreamsContextFactory.getStreamsContext(Type.DISTRIBUTED_TESTER);
    Tester tester = topo.getTester();
    String[] expectedArr = { "A0", "B1", "C2", "A3", "B4", "C5", "A6", "B7", "C8" };
    Condition<List<String>> condition = KafkaSPLStreamsUtils.stringContentsUnordered(tester, msgStream, expectedArr);
    tester.complete(context, new HashMap<>(), condition, 30, TimeUnit.SECONDS);
    // check the results
    Assert.assertTrue(condition.getResult().size() > 0);
    Assert.assertTrue(condition.getResult().toString(), condition.valid());
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) Message(com.ibm.streamsx.kafka.test.utils.Message) Topology(com.ibm.streamsx.topology.Topology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) TStream(com.ibm.streamsx.topology.TStream) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 43 with SPLStream

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

the class KafkaSPLStreamsUtils method union.

public static SPLStream union(List<SPLStream> streams, StreamSchema schema) {
    if (streams.size() == 0)
        throw new IllegalArgumentException("At least one stream must be provided.");
    if (streams.size() == 1) {
        return streams.get(0);
    } else {
        SPLStream stream1 = streams.get(0);
        Set<TStream<Tuple>> streamSet = new HashSet<TStream<Tuple>>(streams);
        streamSet.remove(stream1);
        return SPLStreams.convertStream(stream1.union(streamSet), getTupleStreamConvert(), schema);
    }
}
Also used : TStream(com.ibm.streamsx.topology.TStream) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Tuple(com.ibm.streams.operator.Tuple) OutputTuple(com.ibm.streams.operator.OutputTuple) HashSet(java.util.HashSet)

Example 44 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 45 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)

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