Class MapView<K,V>
- java.lang.Object
-
- org.apache.flink.table.api.dataview.MapView<K,V>
-
- Type Parameters:
K- key typeV- value type
- All Implemented Interfaces:
DataView
@PublicEvolving public class MapView<K,V> extends Object implements DataView
ADataViewthat providesMap-like functionality in state entries.A
MapViewcan be backed by a JavaHashMapor can leverage Flink's state backends depending on the context. In many unbounded data scenarios, theMapViewdelegates all calls to aMapStateinstead of theHashMap.For aggregating functions, the view can be used as a field in the accumulator of an
AggregateFunctionorTableAggregateFunctionwhen large amounts of data are expected. Aggregate functions might be used at various locations (pre-aggregation, combiners, merging of window slides, etc.) for some of these locations the data view is not backed by state butArrayList.For process table functions, the view can be used as a top-level state entry. Data views in PTFs are always backed by state.
Note: Keys of a
MapViewmust not be null. Nulls in values are supported. For heap-based state backends,hashCode/equalsof the original (i.e. external) class are used. However, the serialization format will use internal data structures.The
DataTypes of the view's keys and values are reflectively extracted from the accumulator definition. This includes the generic argumentKandVof this class. If reflective extraction is not successful, it is possible to use aDataTypeHinton top the accumulator field. It will be mapped to the underlying collection.The following examples show how to specify an
AggregateFunctionwith aMapView:{@code public class MyAccumulator { public MapViewmap = new MapView<>(); // or explicit: // @DataTypeHint("MAP < STRING, INT >") // public MapView map = new MapView<>(); public long count; } public class MyAggregateFunction extends AggregateFunction {
-
-
Constructor Summary
Constructors Constructor Description MapView()Creates a map view.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all entries of this map.booleancontains(K key)Checks if the map view contains a value for a given key.Iterable<Map.Entry<K,V>>entries()Returns all entries of the map view.booleanequals(Object o)Vget(K key)Return the value for the specified key ornullif the key is not in the map view.Map<K,V>getMap()Returns the entire view's content as an instance ofMap.inthashCode()booleanisEmpty()Returns true if the map view contains no key-value mappings, otherwise false.Iterator<Map.Entry<K,V>>iterator()Returns an iterator over all entries of the map view.Iterable<K>keys()Returns all the keys in the map view.static DataTypenewMapViewDataType(DataType keyDataType, DataType valueDataType)voidput(K key, V value)Inserts a value for the given key into the map view.voidputAll(Map<K,V> map)Inserts all mappings from the specified map to this map view.voidremove(K key)Deletes the value for the given key.voidsetMap(Map<K,V> map)Replaces the entire view's content with the content of the givenMap.Iterable<V>values()Returns all the values in the map view.
-
-
-
Constructor Detail
-
MapView
public MapView()
Creates a map view.The
DataTypeof keys and values is reflectively extracted.
-
-
Method Detail
-
setMap
public void setMap(Map<K,V> map)
Replaces the entire view's content with the content of the givenMap.
-
get
public V get(K key) throws Exception
Return the value for the specified key ornullif the key is not in the map view.- Parameters:
key- The key whose associated value is to be returned- Returns:
- The value to which the specified key is mapped, or
nullif this map contains no mapping for the key - Throws:
Exception- Thrown if the system cannot get data.
-
put
public void put(K key, V value) throws Exception
Inserts a value for the given key into the map view. If the map view already contains a value for the key, the existing value is overwritten.- Parameters:
key- The key for which the value is inserted.value- The value that is inserted for the key.- Throws:
Exception- Thrown if the system cannot put data.
-
putAll
public void putAll(Map<K,V> map) throws Exception
Inserts all mappings from the specified map to this map view.- Parameters:
map- The map whose entries are inserted into this map view.- Throws:
Exception- Thrown if the system cannot access the map.
-
remove
public void remove(K key) throws Exception
Deletes the value for the given key.- Parameters:
key- The key for which the value is deleted.- Throws:
Exception- Thrown if the system cannot access the map.
-
contains
public boolean contains(K key) throws Exception
Checks if the map view contains a value for a given key.- Parameters:
key- The key to check.- Returns:
- True if there exists a value for the given key, false otherwise.
- Throws:
Exception- Thrown if the system cannot access the map.
-
entries
public Iterable<Map.Entry<K,V>> entries() throws Exception
Returns all entries of the map view.- Returns:
- An iterable of all the key-value pairs in the map view.
- Throws:
Exception- Thrown if the system cannot access the map.
-
keys
public Iterable<K> keys() throws Exception
Returns all the keys in the map view.- Returns:
- An iterable of all the keys in the map.
- Throws:
Exception- Thrown if the system cannot access the map.
-
values
public Iterable<V> values() throws Exception
Returns all the values in the map view.- Returns:
- An iterable of all the values in the map.
- Throws:
Exception- Thrown if the system cannot access the map.
-
iterator
public Iterator<Map.Entry<K,V>> iterator() throws Exception
Returns an iterator over all entries of the map view.- Returns:
- An iterator over all the mappings in the map.
- Throws:
Exception- Thrown if the system cannot access the map.
-
isEmpty
public boolean isEmpty() throws ExceptionReturns true if the map view contains no key-value mappings, otherwise false.- Returns:
- True if the map view contains no key-value mappings, otherwise false.
- Throws:
Exception- Thrown if the system cannot access the state.
-
clear
public void clear()
Removes all entries of this map.
-
-