本文整理汇总了Python中streamsx.topology.topology.Topology.subscribe方法的典型用法代码示例。如果您正苦于以下问题:Python Topology.subscribe方法的具体用法?Python Topology.subscribe怎么用?Python Topology.subscribe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类streamsx.topology.topology.Topology
的用法示例。
在下文中一共展示了Topology.subscribe方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from streamsx.topology.topology import Topology [as 别名]
# 或者: from streamsx.topology.topology.Topology import subscribe [as 别名]
def main():
local = sys.argv[1] == "local"
#define needed variables
COMMANDS_TOPIC = "streamsx/iot/device/commands/send" #topic to publish commands to
EVENTS_TOPIC = "streamsx/iot/device/events" #topic to subscribe to for events
incoming_schema = schema.StreamSchema("tuple <rstring typeId, rstring deviceId, rstring eventId,rstring jsonString>")
cmd_schema = schema.StreamSchema('tuple<rstring typeId, rstring deviceId, rstring cmdId, rstring jsonString>')
topo = Topology('ReadingsFromIot')
#Subscribe to events
events = topo.subscribe(EVENTS_TOPIC, incoming_schema,"AllEventsAsJSON")
sensor_events = events.filter(lambda tuple: tuple["eventId"] == "sensors","SensorEventsAsJSON")
readings = sensor_events.map(get_event_data,"ReadingsStream")
readings.print()
#send a command
cmd_stream = sensor_events.map(get_cmd, "CommandsAsJSON")
#convert the commands stream to a SPL structured schema
commands_to_publish = cmd_stream.map(lambda x : (x["typeId"],x["deviceId"],x["cmdId"],x["jsonString"],), schema = cmd_schema, name="CommandsToPublish")
commands_to_publish.publish(COMMANDS_TOPIC, cmd_schema)
if local and len(sys.argv) > 2:
username = sys.argv[2]
password = sys.argv[3]
result = submit_to_service(topo, local, username, password)
else:
result = submit_to_service(topo, local)
print("Submitted job to the service, job id = " + str(result.job.id))
示例2: run
# 需要导入模块: from streamsx.topology.topology import Topology [as 别名]
# 或者: from streamsx.topology.topology.Topology import subscribe [as 别名]
def run(self, context="DISTRIBUTED"):
## Create topology
topo = Topology("HealthcareDemo")
## Ingest, preprocess and aggregate patient data
patientData = topo.subscribe("ingest-physionet", schema.CommonSchema.Json) \
.map(functions.identity) \
.filter(healthcare_functions.PatientFilter(self.patient_id)) \
.transform(healthcare_functions.GenTimestamp(self.sample_rate)) \
.transform(SlidingWindow(length=self.sample_rate, trigger=self.sample_rate-1)) \
.transform(healthcare_functions.aggregate) \
## Calculate RPeak and RR delta
rpeak_data_stream = patientmonitoring_functions.streaming_rpeak(patientData, self.sample_rate, data_label='ECG Lead II')
## Create a view of the data
self.view_data = rpeak_data_stream.view()
## Compile Python Streams application and submit job
streamsx.topology.context.submit(context, topo.graph, username=self.username, password=self.password)