java - 在 Apache Edgent 中加入不同类型的流

标签 java json apache-edgent

我有 3 个流:

TStream<Double> tempReadings=topology.poll(tempSensor, 10, TimeUnit.SECONDS);
TStream<Double> co2Readings=topology.poll(co2Sensor, 10, TimeUnit.SECONDS);
TStream<Boolean> stationaryReadings=topology.poll(stationarySensor, 10, TimeUnit.SECONDS);

我目前从 3 个 JSON 对象创建 3 个独立的设备事件:

TStream<JsonObject> tempJson=tempReadings.map(tuple->{
    JsonObject json=new JsonObject();
    json.addProperty("Temperature", tuple);
    return json;
});
TStream<JsonObject> co2Json=co2Readings.map(tuple->{
    JsonObject json=new JsonObject();
    json.addProperty("C02Level", tuple);
    return json;
});
TStream<JsonObject> sensoryJson=stationaryReadings.map(tuple->{
    JsonObject json=new JsonObject();
    json.addProperty("isStationary", tuple);
    return json;
});

我想通过将这些流连接在一起并创建 1 个具有三个属性(Temperature、C02Level 和 isStationary)的 JSON 对象来创建单个事件。

最佳答案

您可以union流,但这只会将一个元组放在另一个元组之后,并且您需要使用相同类型的流。

如果您想一次读取所有 3 个属性,您可以创建一个返回“读数”对象的传感器:

class Reading {
    Double temperature;
    Double c02Level;
    Boolean isStationary;
}

关于java - 在 Apache Edgent 中加入不同类型的流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41280097/

相关文章:

java - 什么是原始类型,为什么我们不应该使用它呢?

c# - java构造函数上的c sharp等价物

java - 将所有整数值转换为 JSON 中的字符串

java - TStream 中的元组到底是什么?

java - 使用其他基本运算符是否可以具有与 CoFlatMapFunction 相同的行为?

Java:三种可能类型的一个字段?

java - jetty 服务器 : keep alive timeout for HTTP connection

json - POST 之前的 gzip JSON 负载

python - Elasticsearch Python从JSON创建别名

java - 如何使用 Apache Edgent 过滤元组流