use of com.ibm.streams.operator.StreamSchema in project streamsx.topology by IBMStreams.
the class BOperatorInvocation method setParameter.
/*
public BOperatorInvocation(GraphBuilder bt, String kind,
Map<String, ? extends Object> params) {
this(bt, Operator.class, params);
json().put("kind", kind);
}
*/
public void setParameter(String name, Object value) {
Object jsonValue = value;
String jsonType = null;
if (value instanceof JSONAble) {
value = ((JSONAble) value).toJSON();
}
if (value instanceof JSONObject) {
JSONObject jo = ((JSONObject) value);
if (jo.get("type") == null || jo.get("value") == null)
throw new IllegalArgumentException("Illegal JSONObject " + jo);
String type = (String) jo.get("type");
Object val = (JSONObject) jo.get("value");
if ("__spl_value".equals(type)) {
/*
* The Value object is
* <pre><code>
* object {
* type : "__spl_value"
* value : object {
* value : any. non-null. type appropriate for metaType
* metaType : com.ibm.streams.operator.Type.MetaType.name() string
* }
* }
* </code></pre>
*/
// unwrap and fall through to handling for the wrapped value
JSONObject splValue = (JSONObject) val;
value = splValue.get("value");
jsonValue = value;
String metaType = (String) splValue.get("metaType");
if ("USTRING".equals(metaType) || "UINT8".equals(metaType) || "UINT16".equals(metaType) || "UINT32".equals(metaType) || "UINT64".equals(metaType)) {
jsonType = metaType;
}
// fall through to handle jsonValue as usual
} else {
// other kinds of JSONObject handled below
}
} else if (value instanceof Supplier<?>) {
value = ((Supplier<?>) value).get();
jsonValue = value;
}
if (value instanceof String) {
op.setStringParameter(name, (String) value);
if (jsonType == null)
jsonType = MetaType.RSTRING.name();
} else if (value instanceof Byte) {
op.setByteParameter(name, (Byte) value);
if (jsonType == null)
jsonType = MetaType.INT8.name();
} else if (value instanceof Short) {
op.setShortParameter(name, (Short) value);
if (jsonType == null)
jsonType = MetaType.INT16.name();
} else if (value instanceof Integer) {
op.setIntParameter(name, (Integer) value);
if (jsonType == null)
jsonType = MetaType.INT32.name();
} else if (value instanceof Long) {
op.setLongParameter(name, (Long) value);
if (jsonType == null)
jsonType = MetaType.INT64.name();
} else if (value instanceof Float) {
op.setFloatParameter(name, (Float) value);
jsonType = MetaType.FLOAT32.name();
} else if (value instanceof Double) {
op.setDoubleParameter(name, (Double) value);
jsonType = MetaType.FLOAT64.name();
} else if (value instanceof Boolean) {
op.setBooleanParameter(name, (Boolean) value);
} else if (value instanceof BigDecimal) {
op.setBigDecimalParameter(name, (BigDecimal) value);
// Need to maintain exact value
jsonValue = value.toString();
} else if (value instanceof Enum) {
op.setCustomLiteralParameter(name, (Enum<?>) value);
jsonValue = ((Enum<?>) value).name();
jsonType = JParamTypes.TYPE_ENUM;
} else if (value instanceof StreamSchema) {
jsonValue = ((StreamSchema) value).getLanguageType();
jsonType = JParamTypes.TYPE_SPLTYPE;
} else if (value instanceof String[]) {
String[] sa = (String[]) value;
JSONArray a = new JSONArray(sa.length);
for (String vs : sa) a.add(vs);
jsonValue = a;
op.setStringParameter(name, sa);
} else if (value instanceof Attribute) {
Attribute attr = (Attribute) value;
jsonValue = attr.getName();
jsonType = JParamTypes.TYPE_ATTRIBUTE;
op.setAttributeParameter(name, attr.getName());
} else if (value instanceof JSONObject) {
JSONObject jo = (JSONObject) value;
jsonType = (String) jo.get("type");
jsonValue = (JSONObject) jo.get("value");
} else {
throw new IllegalArgumentException("Type for parameter " + name + " is not supported:" + value.getClass());
}
// Set the value as JSON
JSONObject param = new JSONObject();
param.put("value", jsonValue);
if (jsonType != null)
param.put("type", jsonType);
jparams.put(name, param);
}
use of com.ibm.streams.operator.StreamSchema in project streamsx.topology by IBMStreams.
the class Topology method subscribe.
/**
* Declare a stream that is a subscription to {@code topic}.
* A topic is published using {@link TStream#publish(String)}.
* Subscribers are matched to published streams when the {@code topic}
* is an exact match and the type of the stream ({@code T},
* {@code tupleTypeClass}) is an exact match.
* <BR>
* Publish-subscribe is a many to many relationship,
* multiple streams from multiple applications may
* be published on the same topic and type. Multiple
* subscribers may subscribe to a topic and type.
* <BR>
* A subscription will match all publishers using the
* same topic and tuple type. Tuples on the published
* streams will appear on the returned stream, as
* a single stream.
* <BR>
* The subscription is dynamic, the returned stream
* will subscribe to a matching stream published by
* a newly submitted application (a job), and stops a
* subscription when an running job is cancelled.
* <P>
* Publish-subscribe only works when the topology is
* submitted to a {@link com.ibm.streamsx.topology.context.StreamsContext.Type#DISTRIBUTED}
* context. This allows different applications (or
* even within the same application) to communicate
* using published streams.
* </P>
* <P>
* If {@code tupleTypeClass} is {@code JSONObject.class} then the
* subscription is the generic IBM Streams schema for JSON
* ({@link JSONSchemas#JSON}). Streams of type {@code JSONObject}
* are always published and subscribed using the generic schema
* to allow interchange between applications implemented in
* different languages.
* </P>
* @param topic Topic to subscribe to.
* @param tupleTypeClass Type to subscribe to.
* @return Stream the will contain tuples from matching publishers.
*
* @see TStream#publish(String)
* @see SPLStreams#subscribe(TopologyElement, String, com.ibm.streams.operator.StreamSchema)
*/
public <T> TStream<T> subscribe(String topic, Class<T> tupleTypeClass) {
checkTopicFilter(topic);
if (JSONObject.class.equals(tupleTypeClass)) {
@SuppressWarnings("unchecked") TStream<T> json = (TStream<T>) SPLStreams.subscribe(this, topic, JSONSchemas.JSON).toJSON();
return json;
}
StreamSchema mappingSchema = Schemas.getSPLMappingSchema(tupleTypeClass);
SPLStream splImport;
// Subscribed as an SPL Stream.
if (Schemas.usesDirectSchema(tupleTypeClass)) {
splImport = SPLStreams.subscribe(this, topic, mappingSchema);
} else {
Map<String, Object> params = new HashMap<>();
params.put("topic", topic);
params.put("class", tupleTypeClass.getName());
params.put("streamType", mappingSchema);
splImport = SPL.invokeSource(this, "com.ibm.streamsx.topology.topic::SubscribeJava", params, mappingSchema);
}
return new StreamImpl<T>(this, splImport.output(), tupleTypeClass);
}
use of com.ibm.streams.operator.StreamSchema in project streamsx.topology by IBMStreams.
the class SimpleEmbeddedTest method testIsSupportedNeg.
@Test
public void testIsSupportedNeg() throws Exception {
StreamSchema AnySchema = Type.Factory.getStreamSchema("tuple<rstring ticker>");
Topology topology = new Topology("test");
TStream<String> hw = topology.strings("Hello", "World!", "Test!!");
// add SPL (non-java) operator
FileSPLStreams.csvReader(hw, AnySchema);
StreamsContext<?> sc = StreamsContextFactory.getStreamsContext(StreamsContext.Type.EMBEDDED);
assertTrue(!sc.isSupported(topology));
sc = StreamsContextFactory.getStreamsContext(StreamsContext.Type.EMBEDDED_TESTER);
assertTrue(!sc.isSupported(topology));
}
use of com.ibm.streams.operator.StreamSchema in project streamsx.topology by IBMStreams.
the class PythonFunctionalOperatorsTest method testNonHashableMapInput.
@Test(expected = Exception.class)
public void testNonHashableMapInput() throws Exception {
StreamSchema bad = Type.Factory.getStreamSchema("tuple<map<set<int32>,rstring> a>");
_testSchemaBuild(bad, INT32_SCHEMA);
}
use of com.ibm.streams.operator.StreamSchema in project streamsx.topology by IBMStreams.
the class PythonFunctionalOperatorsTest method testNonHashableSetOutput.
@Test(expected = Exception.class)
public void testNonHashableSetOutput() throws Exception {
StreamSchema bad = Type.Factory.getStreamSchema("tuple<set<list<int32>> a>");
_testSchemaBuild(INT32_SCHEMA, bad);
}
Aggregations