1

我有 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 对象来创建单个事件。

4

2 回答 2

1

您可以合并流,但这只会一个接一个地放置一个元组,并且您需要使用相同类型的流。

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

class Reading {
    Double temperature;
    Double c02Level;
    Boolean isStationary;
}
于 2016-12-25T18:06:05.303 回答
1

在这种情况下,“单轮询结合阅读元组”方法可能是最好的。

更一般地,PlumbingStreams.barrier()可用于合并多个流的相应元组。就像是:

TStream<JsonObject> combinedReadings =
    PlumbingStreams.barrier(Arrays.asList(tempJson,co2Json,sensoryJson))
    .map(list -> combineTuples(list));


static JsonObject combineTuples(JsonObject list...) {
  JsonObject jo = new JsonObject();
  for (JsonObject j : list) {
    for (Entry<String,JsonElement> e : j.entrySet()) {
      jo.addProperty(e.getKey(), e.getValue());
    }
  }
  return jo;
}
于 2017-02-02T15:50:57.897 回答