K
- key typeV
- value type@TypeInfo(value=MapViewTypeInfoFactory.class) @PublicEvolving public class MapView<K,V> extends Object implements 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 DataType
s 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;
}
}
Modifier and Type | Field and Description |
---|---|
TypeInformation<?> |
keyType
Deprecated.
|
TypeInformation<?> |
valueType
Deprecated.
|
Constructor and Description |
---|
MapView()
Creates a map view.
|
MapView(TypeInformation<?> keyType,
TypeInformation<?> valueType)
Deprecated.
This method uses the old type system. Please use a
DataTypeHint instead
if the reflective type extraction is not successful. |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all entries of this map.
|
boolean |
contains(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.
|
boolean |
equals(Object o) |
V |
get(K key)
Return the value for the specified key or
null if the key is not in the map view. |
Map<K,V> |
getMap()
Returns the entire view's content as an instance of
Map . |
int |
hashCode() |
boolean |
isEmpty()
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 DataType |
newMapViewDataType(DataType keyDataType,
DataType valueDataType)
|
void |
put(K key,
V value)
Inserts a value for the given key into the map view.
|
void |
putAll(Map<K,V> map)
Inserts all mappings from the specified map to this map view.
|
void |
remove(K key)
Deletes the value for the given key.
|
void |
setMap(Map<K,V> map)
Replaces the entire view's content with the content of the given
Map . |
Iterable<V> |
values()
Returns all the values in the map view.
|
@Deprecated public transient TypeInformation<?> keyType
@Deprecated public transient TypeInformation<?> valueType
public MapView()
The DataType
of keys and values is reflectively extracted.
@Deprecated public MapView(TypeInformation<?> keyType, TypeInformation<?> valueType)
DataTypeHint
instead
if the reflective type extraction is not successful.MapView
with the specified key and value types.keyType
- The type of keys of the map view.valueType
- The type of values of the map view.public void setMap(Map<K,V> map)
Map
.public V get(K key) throws Exception
null
if the key is not in the map view.key
- The look up key.Exception
- Thrown if the system cannot get data.public void put(K key, V value) throws Exception
key
- The key for which the value is inserted.value
- The value that is inserted for the key.Exception
- Thrown if the system cannot put data.public void putAll(Map<K,V> map) throws Exception
map
- The map whose entries are inserted into this map view.Exception
- Thrown if the system cannot access the map.public void remove(K key) throws Exception
key
- The key for which the value is deleted.Exception
- Thrown if the system cannot access the map.public boolean contains(K key) throws Exception
key
- The key to check.Exception
- Thrown if the system cannot access the map.public Iterable<Map.Entry<K,V>> entries() throws Exception
Exception
- Thrown if the system cannot access the map.public Iterable<K> keys() throws Exception
Exception
- Thrown if the system cannot access the map.public Iterable<V> values() throws Exception
Exception
- Thrown if the system cannot access the map.public Iterator<Map.Entry<K,V>> iterator() throws Exception
Exception
- Thrown if the system cannot access the map.public boolean isEmpty() throws Exception
Exception
- Thrown if the system cannot access the state.public void clear()
Copyright © 2014–2024 The Apache Software Foundation. All rights reserved.