Search in sources :

Example 26 with JSONObject

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

the class PublishSubscribeJsonPythonTest method testPublishJsonFlatMap.

/**
	 * Json Subscribe feeding a flat map
	 */
@Test
public void testPublishJsonFlatMap() throws Exception {
    Random r = new Random();
    final Topology t = new Topology();
    JSONObject j1 = new JSONObject();
    j1.put("a", r.nextLong());
    j1.put("b", "Hello:" + r.nextInt(200));
    JSONObject j2 = new JSONObject();
    j2.put("a", r.nextLong());
    j2.put("b", "Goodbye:" + r.nextInt(200));
    JSONObject j3 = new JSONObject();
    j3.put("a", r.nextLong());
    j3.put("b", "So long:" + r.nextInt(200));
    String s1a = j1.get("a").toString();
    String s1b = j1.get("b").toString();
    String s2a = j2.get("a").toString();
    String s2b = j2.get("b").toString();
    String s3a = j3.get("a").toString();
    String s3b = j3.get("b").toString();
    includePythonApp(t, "json_flatmap_string.py", "json_flatmap_string::json_flatmap_str");
    TStream<JSONObject> source = t.constants(Arrays.asList(j1, j2, j3));
    source = addStartupDelay(source).asType(JSONObject.class);
    source.publish("pytest/json/flatmap");
    TStream<String> subscribe = t.subscribe("pytest/json/flatmap/result", String.class);
    completeAndValidate(subscribe, 60, s1a, s1b, s2a, s2b, s3a, s3b);
}
Also used : Random(java.util.Random) JSONObject(com.ibm.json.java.JSONObject) Topology(com.ibm.streamsx.topology.Topology) Test(org.junit.Test)

Example 27 with JSONObject

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

the class HTTPStreams method getJSON.

/**
     * Periodically poll a web service using HTTP {@code GET} for
     * {@code application/json} data. Declares a source stream that will contain
     * a single tuple for each successful {@code GET}. The tuple is the complete
     * JSON ({@code application/json} content) returned by the request.
     * 
     * @param te
     *            Topology the source stream will be contained in.
     * @param url
     *            URL to poll.
     * @param period
     *            Poling period.
     * @param unit
     *            Unit for {@code period}.
     * @return Stream that will contain the JSON tuples from periodic HTTP
     *         {@code GET} requests.
     */
public static TStream<JSONObject> getJSON(TopologyElement te, String url, long period, TimeUnit unit) {
    Map<String, Object> params = new HashMap<>();
    params.put("url", url);
    double dperiod = (unit.toMillis(period) / 1000.0);
    params.put("period", dperiod);
    SPLStream rawJson = SPL.invokeSource(te, "com.ibm.streamsx.inet.http::HTTPGetJSONContent", params, JSONSchemas.JSON);
    TStream<String> string = SPLStreams.toStringStream(rawJson);
    return JSONStreams.deserialize(string);
}
Also used : HashMap(java.util.HashMap) JSONObject(com.ibm.json.java.JSONObject) SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Example 28 with JSONObject

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

the class GraphBuilder method isInLowLatencyRegion.

public boolean isInLowLatencyRegion(BOperator... operators) {
    // handle nested low latency regions
    JSONObject graph = complete();
    final VisitController visitController = new VisitController(Direction.UPSTREAM);
    final int[] openRegionCount = { 0 };
    for (BOperator operator : operators) {
        JSONObject jop = operator.complete();
        GraphUtilities.visitOnce(visitController, Collections.singleton(JSON4JUtilities.gson(jop)), JSON4JUtilities.gson(graph), new Consumer<JsonObject>() {

            private static final long serialVersionUID = 1L;

            @Override
            public void accept(JsonObject jo) {
                String kind = GsonUtilities.jstring(jo, "kind");
                if (LOW_LATENCY.kind().equals(kind)) {
                    if (openRegionCount[0] <= 0)
                        visitController.setStop();
                    else
                        openRegionCount[0]--;
                } else if (END_LOW_LATENCY.kind().equals(kind))
                    openRegionCount[0]++;
            }
        });
        if (visitController.stopped() || openRegionCount[0] > 0)
            return true;
    }
    return false;
}
Also used : VisitController(com.ibm.streamsx.topology.generator.spl.GraphUtilities.VisitController) OrderedJSONObject(com.ibm.json.java.OrderedJSONObject) JSONObject(com.ibm.json.java.JSONObject) JsonObject(com.google.gson.JsonObject)

Example 29 with JSONObject

use of com.ibm.json.java.JSONObject 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 30 with JSONObject

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

the class StreamsContextSubmit method main.

public static void main(String[] args) throws Exception {
    String context = args[0];
    String JSONPath = args[1];
    File JSONFile = new File(JSONPath);
    try (FileInputStream fis = new FileInputStream(JSONFile)) {
        JSONObject json = JSONObject.parse(fis);
        fis.close();
        StreamsContext<?> sc = StreamsContextFactory.getStreamsContext(context);
        Object rc = sc.submit(json).get();
        if (rc instanceof Integer)
            System.exit((Integer) rc);
    }
}
Also used : JSONObject(com.ibm.json.java.JSONObject) JSONObject(com.ibm.json.java.JSONObject) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

JSONObject (com.ibm.json.java.JSONObject)56 JSONArray (com.ibm.json.java.JSONArray)19 Topology (com.ibm.streamsx.topology.Topology)18 Test (org.junit.Test)18 TestTopology (com.ibm.streamsx.topology.test.TestTopology)14 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)6 Tester (com.ibm.streamsx.topology.tester.Tester)5 JsonObject (com.google.gson.JsonObject)4 HashMap (java.util.HashMap)4 Random (java.util.Random)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 OrderedJSONObject (com.ibm.json.java.OrderedJSONObject)2 StreamConnection (com.ibm.streams.flow.declare.StreamConnection)2 StreamSchema (com.ibm.streams.operator.StreamSchema)2 BOperatorInvocation (com.ibm.streamsx.topology.builder.BOperatorInvocation)2 File (java.io.File)2 IOException (java.io.IOException)2 Map (java.util.Map)2 Attribute (com.ibm.streams.operator.Attribute)1