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