Search in sources :

Example 16 with JSONArray

use of com.ibm.json.java.JSONArray in project streamsx.topology by IBMStreams.

the class JSONStreamsTest method testFlattenWithAttributes.

@Test
public void testFlattenWithAttributes() throws Exception {
    final Topology t = new Topology();
    JSONObject e1 = new JSONObject();
    e1.put("val", "hello");
    JSONObject e2 = new JSONObject();
    e2.put("val", "goodbye");
    e2.put("a", "def");
    final JSONObject value = new JSONObject();
    {
        value.put("a", "abc");
        final JSONArray array = new JSONArray();
        array.add(e1);
        array.add(e2);
        value.put("greetings", array);
    }
    List<JSONObject> inputs = new ArrayList<>();
    inputs.add(value);
    final JSONObject value2 = new JSONObject();
    {
        final JSONArray array2 = new JSONArray();
        array2.add(e1.clone());
        array2.add(e2.clone());
        value2.put("greetings", array2);
    }
    inputs.add(value2);
    TStream<JSONObject> s = t.constants(inputs);
    TStream<JSONObject> jsonm = JSONStreams.flattenArray(s, "greetings", "a");
    ;
    TStream<String> output = JSONStreams.serialize(jsonm);
    JSONObject e1r = (JSONObject) e1.clone();
    e1r.put("a", "abc");
    assertFalse(e1.containsKey("a"));
    completeAndValidate(output, 10, e1r.toString(), e2.toString(), e1.toString(), e2.toString());
}
Also used : JSONObject(com.ibm.json.java.JSONObject) JSONArray(com.ibm.json.java.JSONArray) ArrayList(java.util.ArrayList) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Test(org.junit.Test)

Example 17 with JSONArray

use of com.ibm.json.java.JSONArray in project streamsx.topology by IBMStreams.

the class SPL method newToolkitDepInfo.

private static JSONObject newToolkitDepInfo(TopologyElement te) {
    JSONArray tks = (JSONArray) te.topology().getConfig().get(TOOLKITS);
    if (tks == null) {
        te.topology().getConfig().put(TOOLKITS, tks = new JSONArray());
    }
    JSONObject tkinfo = new JSONObject();
    tks.add(tkinfo);
    return tkinfo;
}
Also used : JSONObject(com.ibm.json.java.JSONObject) JSONArray(com.ibm.json.java.JSONArray)

Example 18 with JSONArray

use of com.ibm.json.java.JSONArray in project streamsx.topology by IBMStreams.

the class PlacementInfo method updatePlacementJSON.

/**
     * Update an element's placement configuration.
     */
private void updatePlacementJSON(Placeable<?> element) {
    JSONObject placement = JOperatorConfig.createJSONItem(element.operator().json(), PLACEMENT);
    placement.put(PLACEMENT_EXPLICIT_COLOCATE_ID, fusingIds.get(element));
    Set<String> elementResourceTags = resourceTags.get(element);
    if (elementResourceTags != null && !elementResourceTags.isEmpty()) {
        JSONArray listOfTags = new JSONArray();
        listOfTags.addAll(elementResourceTags);
        placement.put(PLACEMENT_RESOURCE_TAGS, listOfTags);
    }
}
Also used : JSONObject(com.ibm.json.java.JSONObject) JSONArray(com.ibm.json.java.JSONArray)

Example 19 with JSONArray

use of com.ibm.json.java.JSONArray in project streamsx.topology by IBMStreams.

the class SourceInfo method setSourceInfo.

public static void setSourceInfo(BOperatorInvocation bop, Class<?> calledClass) {
    StackTraceElement[] stack = Thread.currentThread().getStackTrace();
    StackTraceElement calledMethod = null;
    StackTraceElement caller = null;
    boolean foundCalled = false;
    for (int i = 0; i < stack.length; i++) {
        StackTraceElement ste = stack[i];
        if (calledClass.getName().equals(ste.getClassName())) {
            foundCalled = true;
            calledMethod = ste;
            continue;
        }
        if (foundCalled) {
            caller = ste;
            break;
        }
    }
    JSONArray ja = (JSONArray) bop.json().get("sourcelocation");
    if (ja == null)
        bop.json().put("sourcelocation", ja = new JSONArray());
    JSONObject sourceInfo = new JSONObject();
    if (caller != null) {
        if (caller.getFileName() != null)
            sourceInfo.put("file", caller.getFileName());
        if (caller.getClassName() != null)
            sourceInfo.put("class", caller.getClassName());
        if (caller.getMethodName() != null)
            sourceInfo.put("method", caller.getMethodName());
        if (caller.getLineNumber() > 0)
            sourceInfo.put("line", caller.getLineNumber());
    }
    if (calledMethod != null)
        sourceInfo.put("topology.method", calledMethod.getMethodName());
    ja.add(sourceInfo);
}
Also used : JSONObject(com.ibm.json.java.JSONObject) JSONArray(com.ibm.json.java.JSONArray)

Example 20 with JSONArray

use of com.ibm.json.java.JSONArray 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)20 JSONObject (com.ibm.json.java.JSONObject)19 Topology (com.ibm.streamsx.topology.Topology)7 Test (org.junit.Test)7 TestTopology (com.ibm.streamsx.topology.test.TestTopology)6 Tester (com.ibm.streamsx.topology.tester.Tester)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 StreamConnection (com.ibm.streams.flow.declare.StreamConnection)2 StreamSchema (com.ibm.streams.operator.StreamSchema)2 BOperatorInvocation (com.ibm.streamsx.topology.builder.BOperatorInvocation)2 JsonObject (com.google.gson.JsonObject)1 OrderedJSONObject (com.ibm.json.java.OrderedJSONObject)1 Attribute (com.ibm.streams.operator.Attribute)1 Tuple (com.ibm.streams.operator.Tuple)1 BInputPort (com.ibm.streamsx.topology.builder.BInputPort)1 BOperator (com.ibm.streamsx.topology.builder.BOperator)1 BOutput (com.ibm.streamsx.topology.builder.BOutput)1 Supplier (com.ibm.streamsx.topology.function.Supplier)1 KeyFunctionHasher (com.ibm.streamsx.topology.internal.logic.KeyFunctionHasher)1