Class MapView<K,V>
- Type Parameters:
K- key typeV- value type
- All Implemented Interfaces:
DataView
DataView that provides Map-like functionality in the accumulator of an AggregateFunction or TableAggregateFunction when large amounts of data are expected.
A MapView can be backed by a Java HashMap or can leverage Flink's state
backends depending on the context in which the aggregate function is used. In many unbounded data
scenarios, the MapView delegates all calls to a MapState instead of the HashMap.
Note: Keys of a MapView must not be null. Nulls in values are supported. For
heap-based state backends, hashCode/equals of 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 argument K and V of this class.
If reflective extraction is not successful, it is possible to use a DataTypeHint on top
the accumulator field. It will be mapped to the underlying collection.
The following examples show how to specify an AggregateFunction with a MapView:
public class MyAccumulator {
public MapView<String, Integer> map = new MapView<>();
// or explicit:
// {@literal @}DataTypeHint("MAP<STRING, INT>")
// public MapView<String, Integer> map = new MapView<>();
public long count;
}
public class MyAggregateFunction extends AggregateFunction<Long, MyAccumulator> {
{@literal @}Override
public MyAccumulator createAccumulator() {
return new MyAccumulator();
}
public void accumulate(MyAccumulator accumulator, String id) {
if (!accumulator.map.contains(id)) {
accumulator.map.put(id, 1);
accumulator.count++;
}
}
{@literal @}Override
public Long getValue(MyAccumulator accumulator) {
return accumulator.count;
}
}
MapView(TypeInformation<?> keyType, TypeInformation<?> valueType) method was
deprecated and removed. Please use a DataTypeHint instead.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all entries of this map.booleanChecks if the map view contains a value for a given key.entries()Returns all entries of the map view.booleanReturn the value for the specified key ornullif the key is not in the map view.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()Returns an iterator over all entries of the map view.keys()Returns all the keys in the map view.static DataTypenewMapViewDataType(DataType keyDataType, DataType valueDataType) voidInserts a value for the given key into the map view.voidInserts all mappings from the specified map to this map view.voidDeletes the value for the given key.voidReplaces the entire view's content with the content of the givenMap.values()Returns all the values in the map view.
-
Constructor Details
-
MapView
public MapView()Creates a map view.The
DataTypeof keys and values is reflectively extracted.
-
-
Method Details
-
getMap
Returns the entire view's content as an instance ofMap. -
setMap
Replaces the entire view's content with the content of the givenMap. -
get
Return the value for the specified key ornullif the key is not in the map view.- Parameters:
key- The look up key.- Returns:
- The value for the specified key.
- Throws:
Exception- Thrown if the system cannot get data.
-
put
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
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
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
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
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
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
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
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
Returns 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. -
equals
-
hashCode
public int hashCode() -
newMapViewDataType
-