Class WindowFn<T,W extends BoundedWindow>
- java.lang.Object
-
- org.apache.beam.sdk.transforms.windowing.WindowFn<T,W>
-
- Type Parameters:
T
- type of elements being windowedW
-BoundedWindow
subclass used to represent the windows used by thisWindowFn
- All Implemented Interfaces:
java.io.Serializable
,HasDisplayData
- Direct Known Subclasses:
NonMergingWindowFn
,Sessions
public abstract class WindowFn<T,W extends BoundedWindow> extends java.lang.Object implements java.io.Serializable, HasDisplayData
The argument to theWindow
transform used to assign elements into windows and to determine how windows are merged. SeeWindow
for more information on howWindowFn
s are used and for a library of predefinedWindowFns
.Users will generally want to use the predefined
WindowFns
, but it is also possible to create new subclasses.To create a custom
WindowFn
, inherit from this class and override all required methods. If no merging is required, inherit fromNonMergingWindowFn
instead. If no merging is required and each element is assigned to a single window, inherit fromPartitioningWindowFn
. Inheriting from the most specific subclass will enable more optimizations in the runner.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
WindowFn.AssignContext
Information available when runningassignWindows(org.apache.beam.sdk.transforms.windowing.WindowFn.AssignContext)
.class
WindowFn.MergeContext
Information available when runningmergeWindows(org.apache.beam.sdk.transforms.windowing.WindowFn.MergeContext)
.
-
Constructor Summary
Constructors Constructor Description WindowFn()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
assignsToOneWindow()
Returns true if thisWindowFn
always assigns an element to exactly one window.abstract java.util.Collection<W>
assignWindows(WindowFn.AssignContext c)
Given a timestamp and element, returns the set of windows into which it should be placed.abstract WindowMappingFn<W>
getDefaultWindowMappingFn()
Returns the defaultWindowMappingFn
to use to map main input windows to side input windows.TypeDescriptor<W>
getWindowTypeDescriptor()
Returns aTypeDescriptor
capturing what is known statically about the window type of thisWindowFn
instance's most-derived class.abstract boolean
isCompatible(WindowFn<?,?> other)
Deprecated.please override verifyCompatibility to throw a useful error message; we will remove isCompatible at version 3.0.0boolean
isNonMerging()
Returns true if thisWindowFn
never needs to merge any windows.abstract void
mergeWindows(WindowFn.MergeContext c)
Does whatever merging of windows is necessary.void
populateDisplayData(DisplayData.Builder builder)
Register display data for the given transform or component.void
verifyCompatibility(WindowFn<?,?> other)
ThrowIncompatibleWindowException
if this WindowFn does not perform the same merging as the given $WindowFn
.abstract Coder<W>
windowCoder()
Returns theCoder
used for serializing the windows used by this windowFn.
-
-
-
Method Detail
-
assignWindows
public abstract java.util.Collection<W> assignWindows(WindowFn.AssignContext c) throws java.lang.Exception
Given a timestamp and element, returns the set of windows into which it should be placed.- Throws:
java.lang.Exception
-
mergeWindows
public abstract void mergeWindows(WindowFn.MergeContext c) throws java.lang.Exception
Does whatever merging of windows is necessary.See
MergeOverlappingIntervalWindows.mergeWindows(org.apache.beam.sdk.transforms.windowing.WindowFn.MergeContext)
for an example of how to override this method.- Throws:
java.lang.Exception
-
isCompatible
@Deprecated public abstract boolean isCompatible(WindowFn<?,?> other)
Deprecated.please override verifyCompatibility to throw a useful error message; we will remove isCompatible at version 3.0.0Returns whether this performs the same merging as the givenWindowFn
.
-
verifyCompatibility
public void verifyCompatibility(WindowFn<?,?> other) throws IncompatibleWindowException
ThrowIncompatibleWindowException
if this WindowFn does not perform the same merging as the given $WindowFn
.- Throws:
IncompatibleWindowException
- if compared WindowFns are not compatible.
-
windowCoder
public abstract Coder<W> windowCoder()
Returns theCoder
used for serializing the windows used by this windowFn.
-
getDefaultWindowMappingFn
public abstract WindowMappingFn<W> getDefaultWindowMappingFn()
Returns the defaultWindowMappingFn
to use to map main input windows to side input windows. This should accept arbitrary main input windows, and produce aBoundedWindow
that can be produced by thisWindowFn
.
-
isNonMerging
public boolean isNonMerging()
Returns true if thisWindowFn
never needs to merge any windows.
-
assignsToOneWindow
public boolean assignsToOneWindow()
Returns true if thisWindowFn
always assigns an element to exactly one window.If this varies per-element, or cannot be determined, conservatively return false.
By default, returns false.
-
getWindowTypeDescriptor
public TypeDescriptor<W> getWindowTypeDescriptor()
Returns aTypeDescriptor
capturing what is known statically about the window type of thisWindowFn
instance's most-derived class.In the normal case of a concrete
WindowFn
subclass with no generic type parameters of its own (including anonymous inner classes), this will be a complete non-generic type.
-
populateDisplayData
public void populateDisplayData(DisplayData.Builder builder)
Register display data for the given transform or component.populateDisplayData(DisplayData.Builder)
is invoked by Pipeline runners to collect display data viaDisplayData.from(HasDisplayData)
. Implementations may callsuper.populateDisplayData(builder)
in order to register display data in the current namespace, but should otherwise usesubcomponent.populateDisplayData(builder)
to use the namespace of the subcomponent.By default, does not register any display data. Implementors may override this method to provide their own display data.
- Specified by:
populateDisplayData
in interfaceHasDisplayData
- Parameters:
builder
- The builder to populate with display data.- See Also:
HasDisplayData
-
-