Search in sources :

Example 6 with BOutput

use of com.ibm.streamsx.topology.builder.BOutput in project streamsx.topology by IBMStreams.

the class StreamImpl method _parallel.

private TStream<T> _parallel(Supplier<Integer> width, Routing routing, Function<T, ?> keyer) {
    if (width == null)
        throw new IllegalArgumentException(PortProperties.WIDTH);
    Integer widthVal;
    if (width.get() != null)
        widthVal = width.get();
    else if (width instanceof SubmissionParameter<?>)
        widthVal = ((SubmissionParameter<Integer>) width).getDefaultValue();
    else
        throw new IllegalArgumentException(Messages.getString("CORE_ILLEGAL_WIDTH_NULL"));
    if (widthVal != null && widthVal <= 0)
        throw new IllegalArgumentException(Messages.getString("CORE_ILLEGAL_WIDTH_VALUE"));
    BOutput toBeParallelized = output();
    boolean isPartitioned = false;
    if (keyer != null) {
        final ToIntFunction<T> hasher = new KeyFunctionHasher<>(keyer);
        BOperatorInvocation hashAdder = JavaFunctional.addFunctionalOperator(this, "HashAdder", HASH_ADDER_KIND, hasher);
        hashAdder._json().addProperty(HASH_ADDER, true);
        if (serializer.isPresent()) {
            hashAdder.setParameter("inputSerializer", serializeLogic(serializer.get()));
            JavaFunctional.addDependency(this, hashAdder, serializer.get().getClass());
            // may have more than we need.
            if (getTupleType() != null)
                JavaFunctional.addDependency(this, hashAdder, getTupleType());
            else
                JavaFunctional.copyDependencies(this, operator(), hashAdder);
        }
        hashAdder.layout().addProperty("hidden", true);
        BInputPort ip = connectTo(hashAdder, true, null);
        String hashSchema = ObjectSchemas.schemaWithHash(ip._schema());
        toBeParallelized = hashAdder.addOutput(hashSchema);
        isPartitioned = true;
    }
    BOutput parallelOutput = builder().parallel(toBeParallelized, routing.name(), width);
    if (isPartitioned) {
        parallelOutput._json().addProperty(PortProperties.PARTITIONED, true);
        JsonArray partitionKeys = new JsonArray();
        partitionKeys.add(new JsonPrimitive("__spl_hash"));
        parallelOutput._json().add(PortProperties.PARTITION_KEYS, partitionKeys);
        // Add hash remover
        StreamImpl<T> parallelStream = new StreamImpl<T>(this, parallelOutput, getTupleType(), serializer);
        BOperatorInvocation hashRemover = builder().addOperator("HashRemover", HASH_REMOVER_KIND, null);
        hashRemover.setModel(MODEL_SPL, LANGUAGE_JAVA);
        hashRemover.layout().addProperty("hidden", true);
        @SuppressWarnings("unused") BInputPort pip = parallelStream.connectTo(hashRemover, true, null);
        parallelOutput = hashRemover.addOutput(output._type());
    }
    return addMatchingStream(parallelOutput);
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) BOperatorInvocation(com.ibm.streamsx.topology.builder.BOperatorInvocation) JsonArray(com.google.gson.JsonArray) BOutput(com.ibm.streamsx.topology.builder.BOutput) CONSISTENT(com.ibm.streamsx.topology.generator.operator.OpProperties.CONSISTENT) BInputPort(com.ibm.streamsx.topology.builder.BInputPort) KeyFunctionHasher(com.ibm.streamsx.topology.internal.logic.KeyFunctionHasher)

Example 7 with BOutput

use of com.ibm.streamsx.topology.builder.BOutput 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);
    String schema = output()._type();
    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()._type())) {
            if (s.getTupleClass() != null) {
                // This stream has the direct schema!
                schema = s.output()._type();
                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()._type().equals(schema);
                sourceStreams.set(i, s);
            }
        }
        outputs.add(s.output());
    }
    BOutput unionOutput = builder().addUnion(outputs);
    return new StreamImpl<T>(this, unionOutput, tupleType, serializer);
}
Also used : ArrayList(java.util.ArrayList) 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) CONSISTENT(com.ibm.streamsx.topology.generator.operator.OpProperties.CONSISTENT) HashSet(java.util.HashSet)

Example 8 with BOutput

use of com.ibm.streamsx.topology.builder.BOutput in project streamsx.topology by IBMStreams.

the class EmbeddedTesterRuntime method setupEmbeddedTestHandlers.

private void setupEmbeddedTestHandlers(EmbeddedGraph eg) throws Exception {
    // Embedded does not support a StreamHandler against
    // a connected stream. In that case add a no-op intermediate stream.
    Set<TStream<?>> adds = new HashSet<>();
    // Find all that are connected.
    for (TStream<?> stream : handlers.keySet()) {
        if (stream.output().isConnected())
            adds.add(stream);
    }
    // Insert a filter for the handler.
    for (TStream<?> stream : adds) {
        TStream<?> filter = stream.filter(t -> true);
        Set<StreamHandler<Tuple>> handler = handlers.get(stream);
        handlers.remove(stream);
        handlers.put(filter, handler);
    }
    final JavaTestableGraph tg = eg.getExecutionGraph();
    for (TStream<?> stream : handlers.keySet()) {
        Set<StreamHandler<Tuple>> streamHandlers = handlers.get(stream);
        final BOutput output = stream.output();
        final OutputPortDeclaration portDecl = eg.getOutputPort(output.name());
        for (StreamHandler<Tuple> streamHandler : streamHandlers) {
            tg.registerStreamHandler(portDecl, streamHandler);
        }
    }
}
Also used : TStream(com.ibm.streamsx.topology.TStream) BOutput(com.ibm.streamsx.topology.builder.BOutput) StreamHandler(com.ibm.streams.flow.handlers.StreamHandler) OutputPortDeclaration(com.ibm.streams.flow.declare.OutputPortDeclaration) JavaTestableGraph(com.ibm.streams.flow.javaprimitives.JavaTestableGraph) Tuple(com.ibm.streams.operator.Tuple) HashSet(java.util.HashSet)

Aggregations

BOutput (com.ibm.streamsx.topology.builder.BOutput)8 StreamHandler (com.ibm.streams.flow.handlers.StreamHandler)2 Tuple (com.ibm.streams.operator.Tuple)2 TStream (com.ibm.streamsx.topology.TStream)2 BInputPort (com.ibm.streamsx.topology.builder.BInputPort)2 BOperatorInvocation (com.ibm.streamsx.topology.builder.BOperatorInvocation)2 CONSISTENT (com.ibm.streamsx.topology.generator.operator.OpProperties.CONSISTENT)2 KeyFunctionHasher (com.ibm.streamsx.topology.internal.logic.KeyFunctionHasher)2 HashSet (java.util.HashSet)2 JsonArray (com.google.gson.JsonArray)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 JSONArray (com.ibm.json.java.JSONArray)1 OutputPortDeclaration (com.ibm.streams.flow.declare.OutputPortDeclaration)1 JavaTestableGraph (com.ibm.streams.flow.javaprimitives.JavaTestableGraph)1 OutputTuple (com.ibm.streams.operator.OutputTuple)1 StreamSchema (com.ibm.streams.operator.StreamSchema)1 BOutputPort (com.ibm.streamsx.topology.builder.BOutputPort)1 Print (com.ibm.streamsx.topology.internal.logic.Print)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1