在压缩的kafka主题中,我有慢速移动的数据,在另一个主题中,我也有快速移动的数据。
1)快速移动的数据是Kafka实时提取的无边界事件。
2)慢速移动数据是用于丰富快速移动数据的元数据。这是一个紧凑的主题,数据很少更新(天/月)。
3)每个快速移动的数据有效载荷都应具有一个元数据有效载荷,该元数据有效载荷应具有与其可以聚合的相同的customerId。
我想针对customerId汇总快/慢移动数据(在两个主题的数据中都很常见)。我想知道您将如何去做?到目前为止:
PTransform<PBegin, PCollection<KV<byte[], byte[]>>> kafka = KafkaIO.<byte[], byte[]>read()
.withBootstrapServers(“url:port")
.withTopics([“fast-moving-data”, “slow-moving-data"])
.withKeyDeserializer(ByteArrayDeserializer.class)
.withValueDeserializer(ByteArrayDeserializer.class)
.updateConsumerProperties((Map) props)
.withoutMetadata();
我注意到我可以使用.withTopics,并具体说明我想使用的不同主题,但是在此之后,我再也找不到能够帮助汇总的示例。任何帮助,将不胜感激。
答案 0 :(得分:1)
我建议单独阅读这些主题,为管道创建两个不同的输入。您可以稍后交叉/加入。穿越它们的方法是将缓慢移动的流作为侧面输入提供给hotpath(快速移动的PCollection的转换)。
请参见此处:https://beam.apache.org/documentation/programming-guide/#side-inputs
答案 1 :(得分:1)
在本SO Q&A中也讨论过的以下模式对于您的用例可能是一个很好的探索。可能会引起问题的一个因素是压缩的缓慢移动流的大小。希望它有用。
对于此模式,我们可以使用GenerateSequence源转换定期(例如每天一次)发出值。 通过在每个元素上激活的数据驱动触发器将该值传递到全局窗口中。 在DoFn中,使用此过程作为触发从有限源中提取数据 创建SideInput以用于下游转换。
值得注意的是,由于此模式在处理时间上使用了全局窗口SideInput触发,因此与在事件时间内要处理的元素的匹配将是不确定的。例如,如果我们有一个在事件时间窗口化的主管道,则这些窗口将看到的SideInput视图的版本将取决于在处理时间而非事件时间触发的最新触发器。
同样重要的是要注意,通常SideInput应该适合内存。
Java(SDK 2.9.0):
在sideinput下面的示例中,每隔很短的时间更新一次,这样可以轻松看到效果。期望侧面输入正在缓慢更新,例如每隔几个小时或每天一次。
在下面的示例代码中,我们利用在DoFn中创建的Map(即View.asSingleton)中,这是此模式的推荐方法。
下面的示例说明了这种模式,请注意View.asSingleton在每次计数器更新时都会重建。
针对您的用例,您可以将GenerateSequence
转换替换为PubSubIO
转换。这有道理吗?
public static void main(String[] args) {
// Create pipeline
PipelineOptions options = PipelineOptionsFactory.fromArgs(args).withValidation()
.as(PipelineOptions.class);
// Using View.asSingleton, this pipeline uses a dummy external service as illustration.
// Run in debug mode to see the output
Pipeline p = Pipeline.create(options);
// Create slowly updating sideinput
PCollectionView<Map<String, String>> map = p
.apply(GenerateSequence.from(0).withRate(1, Duration.standardSeconds(5L)))
.apply(Window.<Long>into(new GlobalWindows())
.triggering(Repeatedly.forever(AfterProcessingTime.pastFirstElementInPane()))
.discardingFiredPanes())
.apply(ParDo.of(new DoFn<Long, Map<String, String>>() {
@ProcessElement public void process(@Element Long input,
OutputReceiver<Map<String, String>> o) {
// Do any external reads needed here...
// We will make use of our dummy external service.
// Every time this triggers, the complete map will be replaced with that read from
// the service.
o.output(DummyExternalService.readDummyData());
}
})).apply(View.asSingleton());
// ---- Consume slowly updating sideinput
// GenerateSequence is only used here to generate dummy data for this illustration.
// You would use your real source for example PubSubIO, KafkaIO etc...
p.apply(GenerateSequence.from(0).withRate(1, Duration.standardSeconds(1L)))
.apply(Window.into(FixedWindows.of(Duration.standardSeconds(1))))
.apply(Sum.longsGlobally().withoutDefaults())
.apply(ParDo.of(new DoFn<Long, KV<Long, Long>>() {
@ProcessElement public void process(ProcessContext c) {
Map<String, String> keyMap = c.sideInput(map);
c.outputWithTimestamp(KV.of(1L, c.element()), Instant.now());
LOG.debug("Value is {} key A is {} and key B is {}"
, c.element(), keyMap.get("Key_A"),keyMap.get("Key_B"));
}
}).withSideInputs(map));
p.run();
}
public static class DummyExternalService {
public static Map<String, String> readDummyData() {
Map<String, String> map = new HashMap<>();
Instant now = Instant.now();
DateTimeFormatter dtf = DateTimeFormat.forPattern("HH:MM:SS");
map.put("Key_A", now.minus(Duration.standardSeconds(30)).toString(dtf));
map.put("Key_B", now.minus(Duration.standardSeconds(30)).toString());
return map;
}
}