[英]Buffer and flush Apache Beam streaming data
我有一個流作業,初始運行時必須處理大量數據。 DoFn 之一調用支持批處理請求的遠程服務,因此在處理有界集合時,我使用以下方法:
private static final class Function extends DoFn<String, Void> implements Serializable {
private static final long serialVersionUID = 2417984990958377700L;
private static final int LIMIT = 500;
private transient Queue<String> buffered;
@StartBundle
public void startBundle(Context context) throws Exception {
buffered = new LinkedList<>();
}
@ProcessElement
public void processElement(ProcessContext context) throws Exception {
buffered.add(context.element());
if (buffered.size() > LIMIT) {
flush();
}
}
@FinishBundle
public void finishBundle(Context c) throws Exception {
// process remaining
flush();
}
private void flush() {
// build batch request
while (!buffered.isEmpty()) {
buffered.poll();
// do something
}
}
}
有沒有辦法窗口數據,以便可以在無界集合上使用相同的方法?
我試過以下:
pipeline
.apply("Read", Read.from(source))
.apply(WithTimestamps.of(input -> Instant.now()))
.apply(Window.into(FixedWindows.of(Duration.standardMinutes(2L))))
.apply("Process", ParDo.of(new Function()));
但是startBundle
和finishBundle
會為每個元素調用。 是否有機會使用 RxJava(2 分鍾窗口或 100 個元素包):
source
.toFlowable(BackpressureStrategy.LATEST)
.buffer(2, TimeUnit.MINUTES, 100)
這是 per-key-and-windows state和timers新功能的典型用例。
狀態在Beam 博客文章中進行了描述,而對於計時器,您必須依賴於 Javadoc。 不管 javadoc 怎么說支持他們的跑步者,真正的狀態可以在 Beam 的能力矩陣中找到。
該模式與您編寫的非常相似,但狀態允許它與窗口一起工作,也可以跨包工作,因為它們在流中可能非常小。 由於必須以某種方式對狀態進行分區以保持並行性,因此您需要添加某種鍵。 目前沒有自動分片。
private static final class Function extends DoFn<KV<Key, String>, Void> implements Serializable {
private static final long serialVersionUID = 2417984990958377700L;
private static final int LIMIT = 500;
@StateId("bufferedSize")
private final StateSpec<Object, ValueState<Integer>> bufferedSizeSpec =
StateSpecs.value(VarIntCoder.of());
@StateId("buffered")
private final StateSpec<Object, BagState<String>> bufferedSpec =
StateSpecs.bag(StringUtf8Coder.of());
@TimerId("expiry")
private final TimerSpec expirySpec = TimerSpecs.timer(TimeDomain.EVENT_TIME);
@ProcessElement
public void processElement(
ProcessContext context,
BoundedWindow window,
@StateId("bufferedSize") ValueState<Integer> bufferedSizeState,
@StateId("buffered") BagState<String> bufferedState,
@TimerId("expiry") Timer expiryTimer) {
int size = firstNonNull(bufferedSizeState.read(), 0);
bufferedState.add(context.element().getValue());
size += 1;
bufferedSizeState.write(size);
expiryTimer.set(window.maxTimestamp().plus(allowedLateness));
if (size > LIMIT) {
flush(context, bufferedState, bufferedSizeState);
}
}
@OnTimer("expiry")
public void onExpiry(
OnTimerContext context,
@StateId("bufferedSize") ValueState<Integer> bufferedSizeState,
@StateId("buffered") BagState<String> bufferedState) {
flush(context, bufferedState, bufferedSizeState);
}
private void flush(
WindowedContext context,
BagState<String> bufferedState,
ValueState<Integer> bufferedSizeState) {
Iterable<String> buffered = bufferedState.read();
// build batch request from buffered
...
// clear things
bufferedState.clear();
bufferedSizeState.clear();
}
}
在這里做一些筆記:
DoFn
的實例變量,因為實例變量跨窗口沒有內聚力。@StartBundle
初始化。BagState
支持“盲”寫,因此不需要任何讀-修改-寫,只需以與輸出時相同的方式提交新元素即可。@OnTimer("expiry")
代替了@FinishBundle
,因為完成一個包不是每個窗口的事情,而是一個運行器如何執行你的管道的工件。綜上所述,如果您正在向外部系統寫入數據,那么在寫入取決於窗口的寫入方式之前,您可能希望將窗口具體化並重新窗口化到全局窗口中,因為“外部世界是全局窗口”。
apache beam 0.6.0 的文檔說 StateId “當前不受任何跑步者支持”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.