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);
}
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);
}
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;
}
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;
}
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);
}
}
Aggregations