org.apache.flink.table.api.dataview.MapView Java Examples

The following examples show how to use org.apache.flink.table.api.dataview.MapView. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MapViewTypeInfoFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public TypeInformation<MapView<K, V>> createTypeInfo(Type t, Map<String, TypeInformation<?>> genericParameters) {
	TypeInformation<?> keyType = genericParameters.get("K");
	TypeInformation<?> valueType = genericParameters.get("V");

	if (keyType == null) {
		// we might can get the keyType later from the MapView constructor
		keyType = new GenericTypeInfo<>(Object.class);
	}

	if (valueType == null) {
		// we might can get the keyType later from the MapView constructor
		valueType = new GenericTypeInfo<>(Object.class);
	}

	//noinspection unchecked
	return new MapViewTypeInfo<>((TypeInformation<K>) keyType, (TypeInformation<V>) valueType);
}
 
Example #2
Source File: LastValueWithRetractAggFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
public void accumulate(GenericRowData acc, Object value, Long order) throws Exception {
	if (value != null) {
		T v = (T) value;
		Long prevOrder = (Long) acc.getField(1);
		if (prevOrder == null || prevOrder <= order) {
			acc.setField(0, v); // acc.lastValue = v
			acc.setField(1, order); // acc.lastOrder = order
		}

		MapView<Long, List<T>> orderToValueMapView = getOrderToValueMapViewFromAcc(acc);
		List<T> valueList = orderToValueMapView.get(order);
		if (valueList == null) {
			valueList = new ArrayList<>();
		}
		valueList.add(v);
		orderToValueMapView.put(order, valueList);
	}
}
 
Example #3
Source File: MapViewTypeInfoFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public TypeInformation<MapView<K, V>> createTypeInfo(Type t, Map<String, TypeInformation<?>> genericParameters) {
	TypeInformation<?> keyType = genericParameters.get("K");
	TypeInformation<?> valueType = genericParameters.get("V");

	if (keyType == null) {
		// we might can get the keyType later from the MapView constructor
		keyType = new GenericTypeInfo<>(Object.class);
	}

	if (valueType == null) {
		// we might can get the keyType later from the MapView constructor
		valueType = new GenericTypeInfo<>(Object.class);
	}

	//noinspection unchecked
	return new MapViewTypeInfo<>((TypeInformation<K>) keyType, (TypeInformation<V>) valueType);
}
 
Example #4
Source File: LastValueWithRetractAggFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
public void retract(GenericRowData acc, Object value) throws Exception {
	if (value != null) {
		T v = (T) value;
		MapView<T, List<Long>> valueToOrderMapView = getValueToOrderMapViewFromAcc(acc);
		List<Long> orderList = valueToOrderMapView.get(v);
		if (orderList != null && orderList.size() > 0) {
			Long order = orderList.get(0);
			orderList.remove(0);
			if (orderList.isEmpty()) {
				valueToOrderMapView.remove(v);
			} else {
				valueToOrderMapView.put(v, orderList);
			}
			retract(acc, value, order);
		}
	}
}
 
Example #5
Source File: LastValueWithRetractAggFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public GenericRowData createAccumulator() {
	// The accumulator schema:
	// lastValue: T
	// lastOrder: Long
	// valueToOrderMap: RawValueData<MapView<T, List<Long>>>
	// orderToValueMap: RawValueData<MapView<Long, List<T>>>
	GenericRowData acc = new GenericRowData(4);
	acc.setField(0, null);
	acc.setField(1, null);
	acc.setField(2, RawValueData.fromObject(
			new MapView<>(getResultType(), new ListTypeInfo<>(Types.LONG))));
	acc.setField(3, RawValueData.fromObject(
			new MapView<>(Types.LONG, new ListTypeInfo<>(getResultType()))));
	return acc;
}
 
Example #6
Source File: FirstValueWithRetractAggFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
public void accumulate(GenericRowData acc, Object value, Long order) throws Exception {
	if (value != null) {
		T v = (T) value;
		Long prevOrder = (Long) acc.getField(1);
		if (prevOrder == null || prevOrder > order) {
			acc.setField(0, v); // acc.firstValue = v
			acc.setField(1, order); // acc.firstOrder = order
		}

		MapView<Long, List<T>> orderToValueMapView = getOrderToValueMapViewFromAcc(acc);
		List<T> valueList = orderToValueMapView.get(order);
		if (valueList == null) {
			valueList = new ArrayList<>();
		}
		valueList.add(v);
		orderToValueMapView.put(order, valueList);
	}
}
 
Example #7
Source File: FirstValueWithRetractAggFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public GenericRow createAccumulator() {
	// The accumulator schema:
	// firstValue: T
	// fistOrder: Long
	// valueToOrderMap: BinaryGeneric<MapView<T, List<Long>>>
	// orderToValueMap: BinaryGeneric<MapView<Long, List<T>>>
	GenericRow acc = new GenericRow(4);
	acc.setField(0, null);
	acc.setField(1, null);
	acc.setField(2, new BinaryGeneric<>(
		new MapView<>(getResultType(), new ListTypeInfo<>(Types.LONG)),
		getValueToOrderMapViewSerializer()));
	acc.setField(3, new BinaryGeneric<>(
		new MapView<>(Types.LONG, new ListTypeInfo<>(getResultType())),
		getOrderToValueMapViewSerializer()));
	return acc;
}
 
Example #8
Source File: FirstValueWithRetractAggFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
public void retract(GenericRowData acc, Object value) throws Exception {
	if (value != null) {
		T v = (T) value;
		MapView<T, List<Long>> valueToOrderMapView = getValueToOrderMapViewFromAcc(acc);
		List<Long> orderList = valueToOrderMapView.get(v);
		if (orderList != null && orderList.size() > 0) {
			Long order = orderList.get(0);
			orderList.remove(0);
			if (orderList.isEmpty()) {
				valueToOrderMapView.remove(v);
			} else {
				valueToOrderMapView.put(v, orderList);
			}
			retract(acc, value, order);
		}
	}
}
 
Example #9
Source File: FirstValueWithRetractAggFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
public void accumulate(GenericRow acc, Object value, Long order) throws Exception {
	if (value != null) {
		T v = (T) value;
		Long prevOrder = (Long) acc.getField(1);
		if (prevOrder == null || prevOrder > order) {
			acc.setField(0, v); // acc.firstValue = v
			acc.setLong(1, order); // acc.firstOrder = order
		}

		MapView<Long, List<T>> orderToValueMapView = getOrderToValueMapViewFromAcc(acc);
		List<T> valueList = orderToValueMapView.get(order);
		if (valueList == null) {
			valueList = new ArrayList<>();
		}
		valueList.add(v);
		orderToValueMapView.put(order, valueList);
	}
}
 
Example #10
Source File: FirstValueWithRetractAggFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
public void retract(GenericRow acc, Object value) throws Exception {
	if (value != null) {
		T v = (T) value;
		MapView<T, List<Long>> valueToOrderMapView = getValueToOrderMapViewFromAcc(acc);
		List<Long> orderList = valueToOrderMapView.get(v);
		if (orderList != null && orderList.size() > 0) {
			Long order = orderList.get(0);
			orderList.remove(0);
			if (orderList.isEmpty()) {
				valueToOrderMapView.remove(v);
			} else {
				valueToOrderMapView.put(v, orderList);
			}
			retract(acc, value, order);
		}
	}
}
 
Example #11
Source File: LastValueWithRetractAggFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
public void retract(GenericRow acc, Object value) throws Exception {
	if (value != null) {
		T v = (T) value;
		MapView<T, List<Long>> valueToOrderMapView = getValueToOrderMapViewFromAcc(acc);
		List<Long> orderList = valueToOrderMapView.get(v);
		if (orderList != null && orderList.size() > 0) {
			Long order = orderList.get(0);
			orderList.remove(0);
			if (orderList.isEmpty()) {
				valueToOrderMapView.remove(v);
			} else {
				valueToOrderMapView.put(v, orderList);
			}
			retract(acc, value, order);
		}
	}
}
 
Example #12
Source File: LastValueWithRetractAggFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
public void accumulate(GenericRow acc, Object value, Long order) throws Exception {
	if (value != null) {
		T v = (T) value;
		Long prevOrder = (Long) acc.getField(1);
		if (prevOrder == null || prevOrder <= order) {
			acc.setField(0, v); // acc.lastValue = v
			acc.setLong(1, order); // acc.lastOrder = order
		}

		MapView<Long, List<T>> orderToValueMapView = getOrderToValueMapViewFromAcc(acc);
		List<T> valueList = orderToValueMapView.get(order);
		if (valueList == null) {
			valueList = new ArrayList<>();
		}
		valueList.add(v);
		orderToValueMapView.put(order, valueList);
	}
}
 
Example #13
Source File: LastValueWithRetractAggFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public GenericRow createAccumulator() {
	// The accumulator schema:
	// lastValue: T
	// lastOrder: Long
	// valueToOrderMap: BinaryGeneric<MapView<T, List<Long>>>
	// orderToValueMap: BinaryGeneric<MapView<Long, List<T>>>
	GenericRow acc = new GenericRow(4);
	acc.setField(0, null);
	acc.setField(1, null);
	acc.setField(2, new BinaryGeneric<>(
			new MapView<>(getResultType(), new ListTypeInfo<>(Types.LONG)), getValueToOrderMapViewSerializer()));
	acc.setField(3, new BinaryGeneric<>(
			new MapView<>(Types.LONG, new ListTypeInfo<>(getResultType())), getOrderToValueMapViewSerializer()));
	return acc;
}
 
Example #14
Source File: JavaUserDefinedAggFunctions.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public DataViewTestAccum createAccumulator() {
	DataViewTestAccum accum = new DataViewTestAccum();
	accum.map = new MapView<>(Types.STRING, Types.INT);
	accum.count = 0L;
	return accum;
}
 
Example #15
Source File: MapViewSerializerUpgradeTest.java    From flink with Apache License 2.0 5 votes vote down vote up
public static MapView<Integer, String> mockTestData() {
	MapView<Integer, String> view = new MapView<>(TypeInformation.of(Integer.class), TypeInformation.of(String.class));
	try {
		view.put(1, "1");
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
	return view;
}
 
Example #16
Source File: MinWithRetractAggFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public MinWithRetractAccumulator<T> createAccumulator() {
	MinWithRetractAccumulator<T> acc = new MinWithRetractAccumulator<>();
	acc.min = null; // min
	acc.mapSize = 0L;
	// store the count for each value
	acc.map = new MapView<>(getValueTypeInfo(), BasicTypeInfo.LONG_TYPE_INFO);
	return acc;
}
 
Example #17
Source File: JavaUserDefinedAggFunctions.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CountDistinctAccum createAccumulator() {
	CountDistinctAccum accum = new CountDistinctAccum();
	accum.map = new MapView<>(Types.STRING, Types.INT);
	accum.count = 0L;
	return accum;
}
 
Example #18
Source File: JavaUserDefinedAggFunctions.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public CountDistinctAccum createAccumulator() {
	CountDistinctAccum accum = new CountDistinctAccum();
	accum.map = new MapView<>(Types.STRING, Types.INT);
	accum.count = 0L;
	return accum;
}
 
Example #19
Source File: JavaUserDefinedAggFunctions.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CountDistinctAccum createAccumulator() {
	CountDistinctAccum accum = new CountDistinctAccum();
	accum.map = new MapView<>(
			org.apache.flink.table.api.Types.STRING(),
			org.apache.flink.table.api.Types.INT());
	accum.count = 0L;
	return accum;
}
 
Example #20
Source File: JavaUserDefinedAggFunctions.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CountDistinctAccum createAccumulator() {
	CountDistinctAccum accum = new CountDistinctAccum();
	accum.map = new MapView<>(Types.STRING, Types.INT);
	accum.count = 0L;
	return accum;
}
 
Example #21
Source File: FirstValueWithRetractAggFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
public void accumulate(GenericRowData acc, Object value) throws Exception {
	if (value != null) {
		T v = (T) value;
		Long order = System.currentTimeMillis();
		MapView<T, List<Long>> valueToOrderMapView = getValueToOrderMapViewFromAcc(acc);
		List<Long> orderList = valueToOrderMapView.get(v);
		if (orderList == null) {
			orderList = new ArrayList<>();
		}
		orderList.add(order);
		valueToOrderMapView.put(v, orderList);
		accumulate(acc, value, order);
	}
}
 
Example #22
Source File: JavaUserDefinedAggFunctions.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CountDistinctAccum createAccumulator() {
	CountDistinctAccum accum = new CountDistinctAccum();
	accum.map = new MapView<>(
			org.apache.flink.table.api.Types.STRING(),
			org.apache.flink.table.api.Types.INT());
	accum.count = 0L;
	return accum;
}
 
Example #23
Source File: JavaUserDefinedAggFunctions.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CountDistinctAccum createAccumulator() {
	CountDistinctAccum accum = new CountDistinctAccum();
	accum.map = new MapView<>(Types.STRING, Types.INT);
	accum.count = 0L;
	return accum;
}
 
Example #24
Source File: FirstValueWithRetractAggFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
public void retract(GenericRowData acc, Object value, Long order) throws Exception {
	if (value != null) {
		T v = (T) value;
		MapView<Long, List<T>> orderToValueMapView = getOrderToValueMapViewFromAcc(acc);
		List<T> valueList = orderToValueMapView.get(order);
		if (valueList == null) {
			return;
		}
		int index = valueList.indexOf(v);
		if (index >= 0) {
			valueList.remove(index);
			if (valueList.isEmpty()) {
				orderToValueMapView.remove(order);
			} else {
				orderToValueMapView.put(order, valueList);
			}
		}
		if (v.equals(acc.getField(0))) { // v == acc.firstValue
			Long startKey = (Long) acc.getField(1);
			Iterator<Long> iter = orderToValueMapView.keys().iterator();
			// find the minimal order which is greater than or equal to `startKey`
			Long nextKey = Long.MAX_VALUE;
			while (iter.hasNext()) {
				Long key = iter.next();
				if (key >= startKey && key < nextKey) {
					nextKey = key;
				}
			}

			if (nextKey != Long.MAX_VALUE) {
				acc.setField(0, orderToValueMapView.get(nextKey).get(0));
				acc.setField(1, nextKey);
			} else {
				acc.setField(0, null);
				acc.setField(1, null);
			}
		}
	}
}
 
Example #25
Source File: LastValueWithRetractAggFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
public void resetAccumulator(GenericRow acc) {
	acc.setField(0, null);
	acc.setField(1, null);
	MapView<T, List<Long>> valueToOrderMapView = getValueToOrderMapViewFromAcc(acc);
	valueToOrderMapView.clear();
	MapView<Long, List<T>> orderToValueMapView = getOrderToValueMapViewFromAcc(acc);
	orderToValueMapView.clear();
}
 
Example #26
Source File: LastValueWithRetractAggFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
public void retract(GenericRow acc, Object value, Long order) throws Exception {
	if (value != null) {
		T v = (T) value;
		MapView<Long, List<T>> orderToValueMapView = getOrderToValueMapViewFromAcc(acc);
		List<T> valueList = orderToValueMapView.get(order);
		if (valueList == null) {
			return;
		}
		int index = valueList.indexOf(v);
		if (index >= 0) {
			valueList.remove(index);
			if (valueList.isEmpty()) {
				orderToValueMapView.remove(order);
			} else {
				orderToValueMapView.put(order, valueList);
			}
		}
		if (v.equals(acc.getField(0))) { // v == acc.firstValue
			Long startKey = (Long) acc.getField(1);
			Iterator<Long> iter = orderToValueMapView.keys().iterator();
			// find the maximal order which is less than or equal to `startKey`
			Long nextKey = Long.MIN_VALUE;
			while (iter.hasNext()) {
				Long key = iter.next();
				if (key <= startKey && key > nextKey) {
					nextKey = key;
				}
			}

			if (nextKey != Long.MIN_VALUE) {
				List<T> values = orderToValueMapView.get(nextKey);
				acc.setField(0, values.get(values.size() - 1));
				acc.setField(1, nextKey);
			} else {
				acc.setField(0, null);
				acc.setField(1, null);
			}
		}
	}
}
 
Example #27
Source File: JavaUserDefinedAggFunctions.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public DataViewTestAccum createAccumulator() {
	DataViewTestAccum accum = new DataViewTestAccum();
	accum.map = new MapView<>(Types.STRING, Types.INT);
	accum.count = 0L;
	return accum;
}
 
Example #28
Source File: LastValueWithRetractAggFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
public void accumulate(GenericRow acc, Object value) throws Exception {
	if (value != null) {
		T v = (T) value;
		Long order = System.currentTimeMillis();
		MapView<T, List<Long>> valueToOrderMapView = getValueToOrderMapViewFromAcc(acc);
		List<Long> orderList = valueToOrderMapView.get(v);
		if (orderList == null) {
			orderList = new ArrayList<>();
		}
		orderList.add(order);
		valueToOrderMapView.put(v, orderList);
		accumulate(acc, value, order);
	}
}
 
Example #29
Source File: MapViewSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * We need to override this as a {@link LegacySerializerSnapshotTransformer}
 * because in Flink 1.6.x and below, this serializer was incorrectly returning
 * directly the snapshot of the nested map serializer as its own snapshot.
 *
 * <p>This method transforms the incorrect map serializer snapshot
 * to be a proper {@link MapViewSerializerSnapshot}.
 */
@Override
public <U> TypeSerializerSnapshot<MapView<K, V>> transformLegacySerializerSnapshot(
		TypeSerializerSnapshot<U> legacySnapshot) {
	if (legacySnapshot instanceof MapViewSerializerSnapshot) {
		return (TypeSerializerSnapshot<MapView<K, V>>) legacySnapshot;
	} else if (legacySnapshot instanceof MapSerializerConfigSnapshot) {
		// first, transform the incorrect map serializer's snapshot
		// into a proper ListSerializerSnapshot
		MapSerializerSnapshot<K, V> transformedNestedMapSerializerSnapshot = new MapSerializerSnapshot<>();
		MapSerializerConfigSnapshot<K, V> snapshot = (MapSerializerConfigSnapshot<K, V>) legacySnapshot;
		CompositeTypeSerializerUtil.setNestedSerializersSnapshots(
				transformedNestedMapSerializerSnapshot,
				snapshot.getNestedSerializersAndConfigs().get(0).f1,
				snapshot.getNestedSerializersAndConfigs().get(1).f1
		);

		// then, wrap the transformed MapSerializerSnapshot
		// as a nested snapshot in the final resulting MapViewSerializerSnapshot
		MapViewSerializerSnapshot<K, V> transformedMapViewSerializerSnapshot = new MapViewSerializerSnapshot<>();
		CompositeTypeSerializerUtil.setNestedSerializersSnapshots(
				transformedMapViewSerializerSnapshot,
				transformedNestedMapSerializerSnapshot
		);

		return transformedMapViewSerializerSnapshot;
	} else {
		throw new UnsupportedOperationException(
				legacySnapshot.getClass().getCanonicalName() + " is not supported.");
	}
}
 
Example #30
Source File: MapViewTypeInfo.java    From flink with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public TypeSerializer<MapView<K, V>> createSerializer(ExecutionConfig config) {
	if (nullSerializer) {
		return (TypeSerializer<MapView<K, V>>) (TypeSerializer<?>) NullSerializer.INSTANCE;
	} else {
		TypeSerializer<K> keySer = keyType.createSerializer(config);
		TypeSerializer<V> valueSer = valueType.createSerializer(config);
		if (nullAware) {
			return new MapViewSerializer<>(new NullAwareMapSerializer<>(keySer, valueSer));
		} else {
			return new MapViewSerializer<>(new MapSerializer<>(keySer, valueSer));
		}
	}
}