Search in sources :

Example 11 with JSONArray

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

the class GraphBuilder method complete.

@Override
public JSONObject complete() {
    JSONObject json = json();
    JSONArray oa = new JSONArray(ops.size());
    for (BOperator op : ops) {
        oa.add(op.complete());
    }
    json.put("operators", oa);
    return json;
}
Also used : OrderedJSONObject(com.ibm.json.java.OrderedJSONObject) JSONObject(com.ibm.json.java.JSONObject) JSONArray(com.ibm.json.java.JSONArray)

Example 12 with JSONArray

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

the class DependencyResolver method resolveDependencies.

/**
     * Resolve the dependencies.
     * Creates entries in the graph config that will
     * result in files being copied into the toolkit.
     */
public void resolveDependencies() throws IOException, URISyntaxException {
    JSONObject graphConfig = topology.builder().getConfig();
    JSONArray includes = (JSONArray) graphConfig.get("includes");
    if (includes == null)
        graphConfig.put("includes", includes = new JSONArray());
    for (BOperatorInvocation op : operatorToJarDependencies.keySet()) {
        ArrayList<String> jars = new ArrayList<String>();
        for (Path source : operatorToJarDependencies.get(op)) {
            String jarName = resolveDependency(source, includes);
            jars.add("impl/lib/" + jarName);
        }
        String[] jarPaths = jars.toArray(new String[jars.size()]);
        op.setParameter("jar", jarPaths);
    }
    ArrayList<String> jars = new ArrayList<String>();
    for (Path source : globalDependencies) {
        String jarName = resolveDependency(source, includes);
        jars.add("impl/lib/" + jarName);
    }
    List<BOperator> ops = topology.builder().getOps();
    if (jars.size() != 0) {
        for (BOperator op : ops) {
            if (op instanceof BOperatorInvocation) {
                BOperatorInvocation bop = (BOperatorInvocation) op;
                if (Functional.class.isAssignableFrom(bop.op().getOperatorClass())) {
                    JSONObject params = (JSONObject) bop.json().get("parameters");
                    JSONObject op_jars = (JSONObject) params.get("jar");
                    if (null == op_jars) {
                        JSONObject val = new JSONObject();
                        val.put("value", new JSONArray());
                        params.put("jar", val);
                        op_jars = val;
                    }
                    JSONArray value = (JSONArray) op_jars.get("value");
                    for (String jar : jars) {
                        value.add(jar);
                    }
                }
            }
        }
    }
    for (Artifact dep : globalFileDependencies) resolveFileDependency(dep, includes);
}
Also used : Path(java.nio.file.Path) JSONObject(com.ibm.json.java.JSONObject) JSONArray(com.ibm.json.java.JSONArray) ArrayList(java.util.ArrayList) BOperatorInvocation(com.ibm.streamsx.topology.builder.BOperatorInvocation) BOperator(com.ibm.streamsx.topology.builder.BOperator)

Example 13 with JSONArray

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

the class WindowTest method testContinuousAggregateLastSeconds.

/**
     * Test a continuous aggregation.
     */
@Test
public void testContinuousAggregateLastSeconds() throws Exception {
    final Topology t = newTopology();
    TStream<String> source = t.periodicSource(new PeriodicStrings(), 100, TimeUnit.MILLISECONDS);
    TStream<JSONObject> aggregate = source.last(3, TimeUnit.SECONDS).aggregate(new AggregateStrings());
    TStream<String> strings = JSONStreams.serialize(aggregate);
    Tester tester = t.getTester();
    Condition<List<String>> contents = tester.stringContents(strings);
    // 10 tuples per second, each is aggregated, so 15 seconds is around 150 tuples.
    Condition<Long> ending = tester.atLeastTupleCount(strings, 150);
    complete(tester, ending, 30, TimeUnit.SECONDS);
    assertTrue(ending.valid());
    long startTs = 0;
    for (String output : contents.getResult()) {
        JSONObject agg = JSONObject.parse(output);
        JSONArray items = (JSONArray) agg.get("items");
        long ts = (Long) agg.get("ts");
        // Should see around 30 tuples per window, once we
        // pass the first three seconds.
        assertTrue("Number of tuples in window:" + items.size(), items.size() <= 45);
        if (agg.containsKey("delta")) {
            long delta = (Long) agg.get("delta");
            assertTrue(delta >= 0);
            if (startTs == 0) {
                startTs = ts;
            } else {
                long diff = ts - startTs;
                if (diff > 3000)
                    assertTrue("Number of tuples in window:" + items.size(), items.size() >= 25);
            }
        }
    }
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) JSONArray(com.ibm.json.java.JSONArray) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) JSONObject(com.ibm.json.java.JSONObject) List(java.util.List) Test(org.junit.Test)

Example 14 with JSONArray

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

the class JSONStreamsTest method testDeserializeArray.

/**
     * Test that if the serialized value is
     * an array, it ends up wrapped in an object.
     */
@Test
public void testDeserializeArray() throws Exception {
    final String data = "[ 100, 500, false, 200, 400 ]";
    final Topology t = new Topology();
    TStream<String> array = t.strings(data);
    TStream<JSONObject> json = JSONStreams.deserialize(array);
    TStream<String> jsonString = JSONStreams.serialize(json);
    JSONArray ja = (JSONArray) JSON.parse(data);
    JSONObject jo = new JSONObject();
    jo.put("payload", ja);
    checkJsonOutput(jo, jsonString);
}
Also used : JSONObject(com.ibm.json.java.JSONObject) JSONArray(com.ibm.json.java.JSONArray) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Test(org.junit.Test)

Example 15 with JSONArray

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

the class JSONStreamsTest method testFlatten.

@Test
public void testFlatten() throws Exception {
    final Topology t = new Topology();
    final JSONObject value = new JSONObject();
    final JSONArray array = new JSONArray();
    JSONObject e1 = new JSONObject();
    e1.put("val", "hello");
    array.add(e1);
    JSONObject e2 = new JSONObject();
    e2.put("val", "goodbye");
    array.add(e2);
    JSONObject e3 = new JSONObject();
    e3.put("val", "farewell");
    array.add(e3);
    value.put("greetings", array);
    List<JSONObject> inputs = new ArrayList<>();
    inputs.add(value);
    // no list present
    inputs.add(new JSONObject());
    JSONObject emptyList = new JSONObject();
    emptyList.put("greetings", new JSONArray());
    inputs.add(emptyList);
    TStream<JSONObject> s = t.constants(inputs);
    TStream<JSONObject> jsonm = JSONStreams.flattenArray(s, "greetings");
    TStream<String> output = JSONStreams.serialize(jsonm);
    completeAndValidate(output, 10, e1.toString(), e2.toString(), e3.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)

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