Class MapCoder<K,V>
- Type Parameters:
K
- the type of the keys of the KVs being transcodedV
- the type of the values of the KVs being transcoded
- All Implemented Interfaces:
Serializable
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.beam.sdk.coders.Coder
Coder.Context, Coder.NonDeterministicException
-
Method Summary
Modifier and TypeMethodDescriptionboolean
decode
(InputStream inStream) Decodes a value of typeT
from the given input stream in the given context.decode
(InputStream inStream, Coder.Context context) Decodes a value of typeT
from the given input stream in the given context.void
encode
(Map<K, V> map, OutputStream outStream) Encodes the given value of typeT
onto the given output stream.void
encode
(Map<K, V> map, OutputStream outStream, Coder.Context context) Encodes the given value of typeT
onto the given output stream in the given context.TypeDescriptor
<Map<K, V>> Returns theTypeDescriptor
for the type encoded.static <K,
V> MapCoder <K, V> Produces a MapCoder with the given keyCoder and valueCoder.void
registerByteSizeObserver
(Map<K, V> map, org.apache.beam.sdk.util.common.ElementByteSizeObserver observer) Notifies theElementByteSizeObserver
about the byte size of the encoded value using thisCoder
.structuralValue
(Map<K, V> value) Returns an object with anObject.equals()
method that represents structural equality on the argument.void
ThrowCoder.NonDeterministicException
if the coding is not deterministic.Methods inherited from class org.apache.beam.sdk.coders.StructuredCoder
equals, getComponents, hashCode, toString
Methods inherited from class org.apache.beam.sdk.coders.Coder
getEncodedElementByteSize, getEncodedElementByteSizeUsingCoder, isRegisterByteSizeObserverCheap, verifyDeterministic, verifyDeterministic
-
Method Details
-
of
Produces a MapCoder with the given keyCoder and valueCoder. -
getKeyCoder
-
getValueCoder
-
encode
Description copied from class:Coder
Encodes the given value of typeT
onto the given output stream. Multiple elements can be encoded next to each other on the output stream, each coder should encode information to know how many bytes to read when decoding. A common approach is to prefix the encoding with the element's encoded length.- Specified by:
encode
in classCoder<Map<K,
V>> - Throws:
IOException
- if writing to theOutputStream
fails for some reasonCoderException
- if the value could not be encoded for some reason
-
encode
public void encode(Map<K, V> map, OutputStream outStream, Coder.Context context) throws IOException, CoderExceptionDescription copied from class:Coder
Encodes the given value of typeT
onto the given output stream in the given context.- Overrides:
encode
in classCoder<Map<K,
V>> - Throws:
IOException
- if writing to theOutputStream
fails for some reasonCoderException
- if the value could not be encoded for some reason
-
decode
Description copied from class:Coder
Decodes a value of typeT
from the given input stream in the given context. Returns the decoded value. Multiple elements can be encoded next to each other on the input stream, each coder should encode information to know how many bytes to read when decoding. A common approach is to prefix the encoding with the element's encoded length.- Specified by:
decode
in classCoder<Map<K,
V>> - Throws:
IOException
- if reading from theInputStream
fails for some reasonCoderException
- if the value could not be decoded for some reason
-
decode
public Map<K,V> decode(InputStream inStream, Coder.Context context) throws IOException, CoderException Description copied from class:Coder
Decodes a value of typeT
from the given input stream in the given context. Returns the decoded value.- Overrides:
decode
in classCoder<Map<K,
V>> - Throws:
IOException
- if reading from theInputStream
fails for some reasonCoderException
- if the value could not be decoded for some reason
-
getCoderArguments
-
verifyDeterministic
ThrowCoder.NonDeterministicException
if the coding is not deterministic.In order for a
Coder
to be considered deterministic, the following must be true:- two values that compare as equal (via
Object.equals()
orComparable.compareTo()
, if supported) have the same encoding. - the
Coder
always produces a canonical encoding, which is the same for an instance of an object even if produced on different computers at different times.
- Specified by:
verifyDeterministic
in classCoder<Map<K,
V>> - Throws:
Coder.NonDeterministicException
- always. Not all maps have a deterministic encoding. For example,HashMap
comparison does not depend on element order, so twoHashMap
instances may be equal but produce different encodings.
- two values that compare as equal (via
-
consistentWithEquals
public boolean consistentWithEquals()Description copied from class:Coder
Returnstrue
if thisCoder
is injective with respect toObject.equals(java.lang.Object)
.Whenever the encoded bytes of two values are equal, then the original values are equal according to
Objects.equals()
. Note that this is well-defined fornull
.This condition is most notably false for arrays. More generally, this condition is false whenever
equals()
compares object identity, rather than performing a semantic/structural comparison.By default, returns false.
- Overrides:
consistentWithEquals
in classCoder<Map<K,
V>>
-
structuralValue
Description copied from class:Coder
Returns an object with anObject.equals()
method that represents structural equality on the argument.For any two values
x
andy
of typeT
, if their encoded bytes are the same, then it must be the case thatstructuralValue(x).equals(structuralValue(y))
.Most notably:
- The structural value for an array coder should perform a structural comparison of the contents of the arrays, rather than the default behavior of comparing according to object identity.
- The structural value for a coder accepting
null
should be a proper object with anequals()
method, even if the input value isnull
.
See also
Coder.consistentWithEquals()
.By default, if this coder is
Coder.consistentWithEquals()
, and the value is not null, returns the provided object. Otherwise, encodes the value into abyte[]
, and returns an object that performs array equality on the encoded bytes.- Overrides:
structuralValue
in classCoder<Map<K,
V>>
-
registerByteSizeObserver
public void registerByteSizeObserver(Map<K, V> map, org.apache.beam.sdk.util.common.ElementByteSizeObserver observer) throws ExceptionDescription copied from class:Coder
Notifies theElementByteSizeObserver
about the byte size of the encoded value using thisCoder
.Not intended to be called by user code, but instead by
PipelineRunner
implementations.By default, this notifies
observer
about the byte size of the encoded value using this coder as returned byCoder.getEncodedElementByteSize(T)
. -
getEncodedTypeDescriptor
Description copied from class:Coder
Returns theTypeDescriptor
for the type encoded.- Overrides:
getEncodedTypeDescriptor
in classCoder<Map<K,
V>>
-