Search in sources :

Example 1 with KeyFunctionHasher

use of com.ibm.streamsx.topology.internal.logic.KeyFunctionHasher in project streamsx.topology by IBMStreams.

the class StreamImpl method _parallel.

private TStream<T> _parallel(Supplier<Integer> width, Function<T, ?> keyer) {
    if (width == null)
        throw new IllegalArgumentException("width");
    Integer widthVal;
    if (width.get() != null)
        widthVal = width.get();
    else if (width instanceof SubmissionParameter<?>)
        widthVal = ((SubmissionParameter<Integer>) width).getDefaultValue();
    else
        throw new IllegalArgumentException("Illegal width Supplier: width.get() returns null.");
    if (widthVal != null && widthVal <= 0)
        throw new IllegalArgumentException("The parallel width must be greater than or equal to 1.");
    BOutput toBeParallelized = output();
    boolean isPartitioned = false;
    if (keyer != null) {
        final ToIntFunction<T> hasher = new KeyFunctionHasher<>(keyer);
        BOperatorInvocation hashAdder = JavaFunctional.addFunctionalOperator(this, "HashAdder", HashAdder.class, hasher);
        // hashAdder.json().put("routing", routing.toString());
        BInputPort ip = connectTo(hashAdder, true, null);
        StreamSchema hashSchema = ip.port().getStreamSchema().extend("int32", "__spl_hash");
        toBeParallelized = hashAdder.addOutput(hashSchema);
        isPartitioned = true;
    }
    BOutput parallelOutput = builder().parallel(toBeParallelized, width);
    if (isPartitioned) {
        parallelOutput.json().put("partitioned", true);
        JSONArray partitionKeys = new JSONArray();
        partitionKeys.add("__spl_hash");
        parallelOutput.json().put("partitionedKeys", partitionKeys);
        // Add hash remover
        StreamImpl<T> parallelStream = new StreamImpl<T>(this, parallelOutput, getTupleType());
        BOperatorInvocation hashRemover = builder().addOperator(HashRemover.class, null);
        BInputPort pip = parallelStream.connectTo(hashRemover, true, null);
        parallelOutput = hashRemover.addOutput(pip.port().getStreamSchema().remove("__spl_hash"));
    }
    return addMatchingStream(parallelOutput);
}
Also used : JSONArray(com.ibm.json.java.JSONArray) BOperatorInvocation(com.ibm.streamsx.topology.builder.BOperatorInvocation) StreamSchema(com.ibm.streams.operator.StreamSchema) BOutput(com.ibm.streamsx.topology.builder.BOutput) BInputPort(com.ibm.streamsx.topology.builder.BInputPort) KeyFunctionHasher(com.ibm.streamsx.topology.internal.logic.KeyFunctionHasher)

Aggregations

JSONArray (com.ibm.json.java.JSONArray)1 StreamSchema (com.ibm.streams.operator.StreamSchema)1 BInputPort (com.ibm.streamsx.topology.builder.BInputPort)1 BOperatorInvocation (com.ibm.streamsx.topology.builder.BOperatorInvocation)1 BOutput (com.ibm.streamsx.topology.builder.BOutput)1 KeyFunctionHasher (com.ibm.streamsx.topology.internal.logic.KeyFunctionHasher)1