use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.health by IBMStreams.
the class JsonSubscriber method subscribe.
public static TStream<String> subscribe(Topology topo, String topic) {
SPLStream splStream = SPLStreams.subscribe(topo, topic, JSONSchemas.JSON);
TStream<String> stringStream = splStream.transform(new SplToTStream());
return stringStream;
}
use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.
the class StreamImpl method publish.
@Override
public void publish(String topic, boolean allowFilter) {
checkTopicName(topic);
Type tupleType = getTupleType();
if (JSONObject.class.equals(tupleType)) {
filtersNotAllowed(allowFilter);
@SuppressWarnings("unchecked") TStream<JSONObject> json = (TStream<JSONObject>) this;
JSONStreams.toSPL(json).publish(topic, allowFilter);
return;
}
BOperatorInvocation op;
if (Schemas.usesDirectSchema(tupleType) || ((TStream<T>) this) instanceof SPLStream) {
// would not allow a filter against.
if (String.class != tupleType && !(((TStream<T>) this) instanceof SPLStream))
filtersNotAllowed(allowFilter);
// Publish as a stream consumable by SPL & Java/Scala
Map<String, Object> publishParms = new HashMap<>();
publishParms.put("topic", topic);
publishParms.put("allowFilter", allowFilter);
op = builder().addSPLOperator("Publish", "com.ibm.streamsx.topology.topic::Publish", publishParms);
} else if (getTupleClass() != null) {
filtersNotAllowed(allowFilter);
// Publish as a stream consumable only by Java/Scala
Map<String, Object> params = new HashMap<>();
params.put("topic", topic);
params.put("class", getTupleClass().getName());
op = builder().addSPLOperator("Publish", "com.ibm.streamsx.topology.topic::PublishJava", params);
} else {
throw new IllegalStateException("A TStream with a tuple type that contains a generic or unknown type cannot be published");
}
SourceInfo.setSourceInfo(op, SPL.class);
this.connectTo(op, false, null);
}
use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.
the class JSONStreamsTest method checkJsonOutput.
private JSONArtifact checkJsonOutput(JSONArtifact expected, TStream<String> jsonString) throws Exception, IOException {
assertEquals(String.class, jsonString.getTupleClass());
assertEquals(String.class, jsonString.getTupleType());
SPLStream splS = SPLStreams.stringToSPLStream(jsonString);
MostRecent<Tuple> mr = jsonString.topology().getTester().splHandler(splS, new MostRecent<Tuple>());
Condition<Long> singleTuple = jsonString.topology().getTester().tupleCount(splS, 1);
complete(jsonString.topology().getTester(), singleTuple, 10, TimeUnit.SECONDS);
JSONArtifact rv = JSON.parse(mr.getMostRecentTuple().getString(0));
assertEquals(expected, rv);
return rv;
}
use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.health by IBMStreams.
the class JsonPublisher method publish.
public static void publish(TStream<String> jsonInputStream, String topic) {
SPLStream splStream = SPLStreams.convertStream(jsonInputStream, new JsonToSpl(), JSONSchemas.JSON);
splStream.publish(topic);
}
use of com.ibm.streamsx.topology.spl.SPLStream 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);
}
Aggregations