Search in sources :

Example 21 with TStream

use of com.ibm.streamsx.topology.TStream in project streamsx.topology by IBMStreams.

the class StreamTest method testUnionNops.

@Test
public void testUnionNops() throws Exception {
    assumeTrue(isMainRun());
    final Topology f = newTopology("Union");
    TStream<String> s1 = f.strings("A1", "B1", "C1", "D1");
    Set<TStream<String>> empty = Collections.emptySet();
    assertSame(s1, s1.union(s1));
    assertSame(s1, s1.union(empty));
    assertSame(s1, s1.union(Collections.singleton(s1)));
}
Also used : TStream(com.ibm.streamsx.topology.TStream) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Test(org.junit.Test)

Example 22 with TStream

use of com.ibm.streamsx.topology.TStream in project streamsx.topology by IBMStreams.

the class StreamImpl method split.

@Override
public List<TStream<T>> split(int n, ToIntFunction<T> splitter) {
    if (n <= 0)
        throw new IllegalArgumentException("n");
    List<TStream<T>> l = new ArrayList<>(n);
    String opName = splitter.getClass().getSimpleName();
    if (opName.isEmpty()) {
        opName = getTupleName() + "Split";
    }
    BOperatorInvocation bop = JavaFunctional.addFunctionalOperator(this, opName, FunctionSplit.class, splitter);
    SourceInfo.setSourceInfo(bop, StreamImpl.class);
    connectTo(bop, true, null);
    Type outputType = refineType(ToIntFunction.class, 0, splitter);
    for (int i = 0; i < n; i++) {
        TStream<T> splitOutput = JavaFunctional.addJavaOutput(this, bop, outputType);
        l.add(splitOutput);
    }
    return l;
}
Also used : TStream(com.ibm.streamsx.topology.TStream) Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList) BOperatorInvocation(com.ibm.streamsx.topology.builder.BOperatorInvocation) Print(com.ibm.streamsx.topology.internal.logic.Print)

Example 23 with TStream

use of com.ibm.streamsx.topology.TStream in project streamsx.topology by IBMStreams.

the class StreamImpl method union.

@SuppressWarnings("unchecked")
@Override
public TStream<T> union(Set<TStream<T>> others) {
    if (others.isEmpty())
        return this;
    Set<TStream<T>> allStreams = new HashSet<>();
    allStreams.addAll(others);
    allStreams.add(this);
    // Check we don't have just a single stream.
    if (allStreams.size() == 1)
        return this;
    List<TStream<T>> sourceStreams = new ArrayList<>();
    sourceStreams.addAll(allStreams);
    StreamSchema schema = output().schema();
    Type tupleType = getTupleType();
    // Unwrap all streams so that we do not add the same stream twice
    // in multiple unions or explicitly and in a union.
    Set<BOutput> outputs = new HashSet<>();
    for (int i = 0; i < sourceStreams.size(); i++) {
        TStream<T> s = sourceStreams.get(i);
        // E..g TStream<String>.
        if (!schema.equals(s.output().schema())) {
            if (s.getTupleClass() != null) {
                // This stream has the direct schema!
                schema = s.output().schema();
                assert getTupleClass() == null;
                tupleType = s.getTupleClass();
                if (i != 0) {
                    // Didn't discover it first
                    // reset to go through the list
                    // again. Note this assumes there
                    // are just two options for the schema
                    // generic or direct
                    // to get back to 0.
                    i = -1;
                    outputs.clear();
                    continue;
                }
            } else {
                assert tupleType instanceof Class;
                s = s.asType((Class<T>) tupleType);
                assert s.output().schema().equals(schema);
                sourceStreams.set(i, s);
            }
        }
        outputs.add(s.output());
    }
    BOutput unionOutput = builder().addUnion(outputs);
    return new StreamImpl<T>(this, unionOutput, tupleType);
}
Also used : ArrayList(java.util.ArrayList) StreamSchema(com.ibm.streams.operator.StreamSchema) Print(com.ibm.streamsx.topology.internal.logic.Print) TStream(com.ibm.streamsx.topology.TStream) Type(java.lang.reflect.Type) BOutput(com.ibm.streamsx.topology.builder.BOutput) HashSet(java.util.HashSet)

Example 24 with TStream

use of com.ibm.streamsx.topology.TStream in project streamsx.topology by IBMStreams.

the class LowLatencyTest method testLowLatencySplit.

@Test
public void testLowLatencySplit() throws Exception {
    // lowLatency().split() is an interesting case because split()
    // has >1 oports.
    final Topology topology = newTopology("testLowLatencySplit");
    int splitWidth = 3;
    String[] strs = { "ch0", "ch1", "ch2" };
    TStream<String> s1 = topology.strings(strs);
    s1 = s1.isolate();
    s1 = s1.lowLatency();
    /////////////////////////////////////
    // assume that if s1.modify and the split().[modify()] are
    // in the same PE, that s1.split() is in the same too
    TStream<String> s2 = s1.modify(unaryGetPEId());
    List<TStream<String>> splits = s1.split(splitWidth, roundRobinSplitter());
    List<TStream<String>> splitChResults = new ArrayList<>();
    for (int i = 0; i < splits.size(); i++) {
        splitChResults.add(splits.get(i).modify(unaryGetPEId()));
    }
    TStream<String> splitChFanin = splitChResults.get(0).union(new HashSet<>(splitChResults.subList(1, splitChResults.size())));
    /////////////////////////////////////
    TStream<String> all = splitChFanin.endLowLatency();
    Tester tester = topology.getTester();
    Condition<Long> uCount = tester.tupleCount(all, strs.length);
    Condition<List<String>> contents = tester.stringContents(all, "");
    Condition<List<String>> s2contents = tester.stringContents(s2, "");
    complete(tester, uCount, 10, TimeUnit.SECONDS);
    Set<String> peIds = new HashSet<>();
    peIds.addAll(contents.getResult());
    peIds.addAll(s2contents.getResult());
    assertEquals("peIds: " + peIds, 1, peIds.size());
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) ArrayList(java.util.ArrayList) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) TStream(com.ibm.streamsx.topology.TStream) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 25 with TStream

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

the class KafkaConsumerFanInTest method kafkaFanInTest.

@Test
public void kafkaFanInTest() throws Exception {
    Topology topo = getTopology();
    // create the producer (produces tuples after a short delay)
    TStream<String> stringSrcStream = topo.strings(Constants.STRING_DATA).modify(new Delay<>(5000));
    SPL.invokeSink(Constants.KafkaProducerOp, KafkaSPLStreamsUtils.convertStreamToKafkaTuple(stringSrcStream), getKafkaParams());
    // create the consumer
    SPLStream consumerStream1 = SPL.invokeSource(topo, Constants.KafkaConsumerOp, getConsumerParams(1), KafkaSPLStreamsUtils.STRING_SCHEMA);
    SPLStream consumerStream2 = SPL.invokeSource(topo, Constants.KafkaConsumerOp, getConsumerParams(2), KafkaSPLStreamsUtils.STRING_SCHEMA);
    SPLStream unionStream = KafkaSPLStreamsUtils.union(Arrays.asList(consumerStream1, consumerStream2), KafkaSPLStreamsUtils.STRING_SCHEMA);
    SPLStream msgStream = SPLStreams.stringToSPLStream(unionStream.convert(t -> t.getString("message")));
    // test the output of the consumer
    StreamsContext<?> context = StreamsContextFactory.getStreamsContext(Type.DISTRIBUTED_TESTER);
    Tester tester = topo.getTester();
    // both consumers consume the same data, so each result is duplicated
    String[] expectedArr = KafkaSPLStreamsUtils.duplicateArrayEntries(Constants.STRING_DATA, 2);
    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 : TStream(com.ibm.streamsx.topology.TStream) Tester(com.ibm.streamsx.topology.tester.Tester) Arrays(java.util.Arrays) Delay(com.ibm.streamsx.kafka.test.utils.Delay) 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) 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) Assert(org.junit.Assert) Tester(com.ibm.streamsx.topology.tester.Tester) List(java.util.List) Topology(com.ibm.streamsx.topology.Topology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Test(org.junit.Test)

Aggregations

TStream (com.ibm.streamsx.topology.TStream)29 Topology (com.ibm.streamsx.topology.Topology)24 Test (org.junit.Test)24 Tester (com.ibm.streamsx.topology.tester.Tester)22 List (java.util.List)20 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)16 HashMap (java.util.HashMap)16 Condition (com.ibm.streamsx.topology.tester.Condition)15 TimeUnit (java.util.concurrent.TimeUnit)14 StreamsContext (com.ibm.streamsx.topology.context.StreamsContext)13 Type (com.ibm.streamsx.topology.context.StreamsContext.Type)13 StreamsContextFactory (com.ibm.streamsx.topology.context.StreamsContextFactory)13 SPLStreams (com.ibm.streamsx.topology.spl.SPLStreams)13 Map (java.util.Map)13 Constants (com.ibm.streamsx.kafka.test.utils.Constants)12 KafkaSPLStreamsUtils (com.ibm.streamsx.kafka.test.utils.KafkaSPLStreamsUtils)12 SPL (com.ibm.streamsx.topology.spl.SPL)12 Assert (org.junit.Assert)12 Delay (com.ibm.streamsx.kafka.test.utils.Delay)11 TestTopology (com.ibm.streamsx.topology.test.TestTopology)11