com.google.cloud.dataflow.sdk.coders

## Class KvCoder<K,V>

- java.lang.Object
- - com.google.cloud.dataflow.sdk.coders.StandardCoder<T>
  - - com.google.cloud.dataflow.sdk.coders.KvCoderBase<KV<K,V>>
    - - com.google.cloud.dataflow.sdk.coders.KvCoder<K,V>

Type Parameters:
  `K` - the type of the keys of the KVs being transcoded
  `V` - the type of the values of the KVs being transcoded

All Implemented Interfaces:
  Coder<KV<K,V>>, Serializable

    public class KvCoder<K,V>
    extends KvCoderBase<KV<K,V>>

A `KvCoder` encodes `KV`s.

See Also:
  Serialized Form

### Nested Class Summary

### Nested classes/interfaces inherited from interface com.google.cloud.dataflow.sdk.coders.Coder

`Coder.Context, Coder.NonDeterministicException`

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`com.google.cloud.dataflow.sdk.util.CloudObject` `asCloudObject()` Returns the `CloudObject` that represents this `Coder`.
`boolean` `consistentWithEquals()` Returns `true` if this `Coder` is injective with respect to `Object.equals(java.lang.Object)`.
`KV<K,V>` `decode(InputStream inStream, Coder.Context context)` Decodes a value of type `T` from the given input stream in the given context.
`void` `encode(KV<K,V> kv, OutputStream outStream, Coder.Context context)` Encodes the given value of type `T` onto the given output stream in the given context.
`List<? extends Coder<?>>` `getCoderArguments()` If this is a `Coder` for a parameterized type, returns the list of `Coder`s being used for each of the parameters, or returns `null` if this cannot be done or this is not a parameterized type.
`static <K,V> List<Object>` `getInstanceComponents(KV<K,V> exampleValue)`
`Coder<K>` `getKeyCoder()`
`Coder<V>` `getValueCoder()`
`boolean` `isRegisterByteSizeObserverCheap(KV<K,V> kv, Coder.Context context)` Returns whether both keyCoder and valueCoder are considered not expensive.
`static <K,V> KvCoder<K,V>` `of(Coder<K> keyCoder, Coder<V> valueCoder)`
`static KvCoder<?,?>` `of(List<Coder<?>> components)`
`void` `registerByteSizeObserver(KV<K,V> kv, com.google.cloud.dataflow.sdk.util.common.ElementByteSizeObserver observer, Coder.Context context)` Notifies ElementByteSizeObserver about the byte size of the encoded value using this coder.
`Object` `structuralValue(KV<K,V> kv)` Returns an object with an `Object.equals()` method that represents structural equality on the argument.
`void` `verifyDeterministic()` Throw `Coder.NonDeterministicException` if the coding is not deterministic.

### Methods inherited from class com.google.cloud.dataflow.sdk.coders.KvCoderBase

`of`

### Methods inherited from class com.google.cloud.dataflow.sdk.coders.StandardCoder

`equals, getAllowedEncodings, getComponents, getEncodedElementByteSize, getEncodingId, hashCode, toString, verifyDeterministic, verifyDeterministic`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Method Detail

#### of

    public static <K,V> KvCoder<K,V> of(Coder<K> keyCoder,
                                        Coder<V> valueCoder)

#### of

    public static KvCoder<?,?> of(List<Coder<?>> components)

#### getInstanceComponents

    public static <K,V> List<Object> getInstanceComponents(KV<K,V> exampleValue)

#### getKeyCoder

    public Coder<K> getKeyCoder()

#### getValueCoder

    public Coder<V> getValueCoder()

#### encode

    public void encode(KV<K,V> kv,
                       OutputStream outStream,
                       Coder.Context context)
                throws IOException,
                       CoderException

Description copied from interface: `Coder`
Encodes the given value of type `T` onto the given output stream in the given context.

Throws:
  `IOException` - if writing to the `OutputStream` fails for some reason
  `CoderException` - if the value could not be encoded for some reason

#### decode

    public KV<K,V> decode(InputStream inStream,
                          Coder.Context context)
                   throws IOException,
                          CoderException

Description copied from interface: `Coder`
Decodes a value of type `T` from the given input stream in the given context. Returns the decoded value.

Throws:
  `IOException` - if reading from the `InputStream` fails for some reason
  `CoderException` - if the value could not be decoded for some reason

#### getCoderArguments

    public List<? extends Coder<?>> getCoderArguments()

Description copied from interface: `Coder`
If this is a `Coder` for a parameterized type, returns the list of `Coder`s being used for each of the parameters, or returns `null` if this cannot be done or this is not a parameterized type.

#### verifyDeterministic

    public void verifyDeterministic()
                             throws Coder.NonDeterministicException

Description copied from interface: `Coder`
Throw `Coder.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()` or `Comparable.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.

Throws:
  `Coder.NonDeterministicException` - if this coder is not deterministic.

#### consistentWithEquals

    public boolean consistentWithEquals()

Description copied from class: `StandardCoder`
Returns `true` if this `Coder` is injective with respect to `Object.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 for `null`.

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.

Specified by:
  `consistentWithEquals` in interface `Coder<KV<K,V>>`
Overrides:
  `consistentWithEquals` in class `StandardCoder<KV<K,V>>`
Returns:
  `false` for `StandardCoder` unless overridden.

#### structuralValue

    public Object structuralValue(KV<K,V> kv)
                           throws Exception

Description copied from interface: `Coder`
Returns an object with an `Object.equals()` method that represents structural equality on the argument.

For any two values `x` and `y` of type `T`, if their encoded bytes are the same, then it must be the case that `structuralValue(x).equals(@code 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 an `equals()` method, even if the input value is `null`.

See also `Coder.consistentWithEquals()`.

Specified by:
  `structuralValue` in interface `Coder<KV<K,V>>`
Overrides:
  `structuralValue` in class `StandardCoder<KV<K,V>>`
Throws:
  `Exception`

#### asCloudObject

    public com.google.cloud.dataflow.sdk.util.CloudObject asCloudObject()

Description copied from interface: `Coder`
Returns the `CloudObject` that represents this `Coder`.

Specified by:
  `asCloudObject` in interface `Coder<KV<K,V>>`
Overrides:
  `asCloudObject` in class `StandardCoder<KV<K,V>>`

#### isRegisterByteSizeObserverCheap

    public boolean isRegisterByteSizeObserverCheap(KV<K,V> kv,
                                                   Coder.Context context)

Returns whether both keyCoder and valueCoder are considered not expensive.

Specified by:
  `isRegisterByteSizeObserverCheap` in interface `Coder<KV<K,V>>`
Overrides:
  `isRegisterByteSizeObserverCheap` in class `StandardCoder<KV<K,V>>`
Returns:
  `false` unless it is overridden. `StandardCoder.registerByteSizeObserver(T, com.google.cloud.dataflow.sdk.util.common.ElementByteSizeObserver, com.google.cloud.dataflow.sdk.coders.Coder.Context)` invokes `StandardCoder.getEncodedElementByteSize(T, com.google.cloud.dataflow.sdk.coders.Coder.Context)` which requires re-encoding an element unless it is overridden. This is considered expensive.

#### registerByteSizeObserver

    public void registerByteSizeObserver(KV<K,V> kv,
                                         com.google.cloud.dataflow.sdk.util.common.ElementByteSizeObserver observer,
                                         Coder.Context context)
                                  throws Exception

Notifies ElementByteSizeObserver about the byte size of the encoded value using this coder.

Specified by:
  `registerByteSizeObserver` in interface `Coder<KV<K,V>>`
Overrides:
  `registerByteSizeObserver` in class `StandardCoder<KV<K,V>>`
Throws:
  `Exception`