本文整理汇总了Java中org.apache.flink.api.java.functions.KeySelector类的典型用法代码示例。如果您正苦于以下问题:Java KeySelector类的具体用法?Java KeySelector怎么用?Java KeySelector使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
KeySelector类属于org.apache.flink.api.java.functions包,在下文中一共展示了KeySelector类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeSinglePatternAfterMigrationSnapshot
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
/**
* Manually run this to write binary snapshot data.
*/
@Ignore
@Test
public void writeSinglePatternAfterMigrationSnapshot() throws Exception {
KeySelector<Event, Integer> keySelector = new KeySelector<Event, Integer>() {
private static final long serialVersionUID = -4873366487571254798L;
@Override
public Integer getKey(Event value) throws Exception {
return value.getId();
}
};
final Event startEvent1 = new Event(42, "start", 1.0);
OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness =
new KeyedOneInputStreamOperatorTestHarness<>(
getKeyedCepOpearator(false, new SinglePatternNFAFactory()),
keySelector,
BasicTypeInfo.INT_TYPE_INFO);
try {
harness.setup();
harness.open();
harness.processWatermark(new Watermark(5));
// do snapshot and save to file
OperatorStateHandles snapshot = harness.snapshot(0L, 0L);
OperatorSnapshotUtil.writeStateHandle(snapshot,
"src/test/resources/cep-migration-single-pattern-afterwards-flink" + flinkGenerateSavepointVersion + "-snapshot");
} finally {
harness.close();
}
}
示例2: testFoldWindowState
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
@Test
public void testFoldWindowState() throws Exception {
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setStreamTimeCharacteristic(TimeCharacteristic.IngestionTime);
env.registerTypeWithKryoSerializer(File.class, JavaSerializer.class);
DataStream<String> src = env.fromElements("abc");
SingleOutputStreamOperator<?> result = src
.keyBy(new KeySelector<String, String>() {
@Override
public String getKey(String value) {
return null;
}
})
.timeWindow(Time.milliseconds(1000))
.fold(new File("/"), new FoldFunction<String, File>() {
@Override
public File fold(File a, String e) {
return null;
}
});
validateStateDescriptorConfigured(result);
}
示例3: toWarningStream
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
private static <TWarningType extends IWarning> DataStream<TWarningType> toWarningStream(DataStream<LocalWeatherData> localWeatherDataDataStream, IWarningPattern<LocalWeatherData, TWarningType> warningPattern) {
PatternStream<LocalWeatherData> tempPatternStream = CEP.pattern(
localWeatherDataDataStream.keyBy(new KeySelector<LocalWeatherData, String>() {
@Override
public String getKey(LocalWeatherData localWeatherData) throws Exception {
return localWeatherData.getStation().getWban();
}
}),
warningPattern.getEventPattern());
DataStream<TWarningType> warnings = tempPatternStream.select(new PatternSelectFunction<LocalWeatherData, TWarningType>() {
@Override
public TWarningType select(Map<String, List<LocalWeatherData>> map) throws Exception {
return warningPattern.create(map);
}
}, new GenericTypeInfo<TWarningType>(warningPattern.getWarningTargetType()));
return warnings;
}
示例4: getAnomalySteam
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
public DataStream<Tuple3<K, AnomalyResult, RV>> getAnomalySteam(DataStream<V> ds, KeySelector<V, K> keySelector , KeySelector<V,Double> valueSelector, PayloadFold<V, RV> valueFold, Time window) {
KeyedStream<V, K> keyedInput = ds
.keyBy(keySelector);
TypeInformation<Object> foldResultType = TypeExtractor.getUnaryOperatorReturnType(valueFold,
PayloadFold.class,
false,
false,
ds.getType(),
"PayloadFold",
false);
TypeInformation<Tuple3<K,Tuple4<Double,Double,Long,Long>,RV>> resultType = (TypeInformation) new TupleTypeInfo<>(Tuple3.class,
new TypeInformation[] {keyedInput.getKeyType(), new TupleTypeInfo(Tuple4.class,
BasicTypeInfo.DOUBLE_TYPE_INFO, BasicTypeInfo.DOUBLE_TYPE_INFO, BasicTypeInfo.LONG_TYPE_INFO,BasicTypeInfo.LONG_TYPE_INFO), foldResultType});
Tuple3<K,Tuple4<Double,Double,Long,Long>, RV> init= new Tuple3<>(null,new Tuple4<>(0d,0d,0l,0l), valueFold.getInit());
KeyedStream<Tuple3<K,Tuple4<Double,Double,Long,Long>,RV>, Tuple> kPreStream = keyedInput
.timeWindow(window)
.apply(init, new ExtCountSumFold<>(keySelector, valueSelector, valueFold, resultType), new ExtWindowTimeExtractor(resultType))
.keyBy(0);
return kPreStream.flatMap(afm);
}
示例5: getAnomalySteam
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
public DataStream<Tuple2<K, AnomalyResult>> getAnomalySteam(DataStream<V> ds, KeySelector<V, K> keySelector , Time window) {
KeyedStream<V, K> keyedInput = ds
.keyBy(keySelector);
TypeInformation<Tuple2<K,Tuple4<Double,Double,Long,Long>>> resultType = (TypeInformation) new TupleTypeInfo<>(Tuple2.class,
new TypeInformation[] {keyedInput.getKeyType(), new TupleTypeInfo(Tuple4.class,
BasicTypeInfo.DOUBLE_TYPE_INFO, BasicTypeInfo.DOUBLE_TYPE_INFO, BasicTypeInfo.LONG_TYPE_INFO,BasicTypeInfo.LONG_TYPE_INFO)});
Tuple2<K,Tuple4<Double,Double,Long,Long>> init= new Tuple2<>(null,new Tuple4<>(0d,0d,0l,0l));
KeyedStream<Tuple2<K,Tuple4<Double,Double,Long,Long>>, Tuple> kPreStream = keyedInput
.timeWindow(window)
.apply(init, new CountWindFold<>(keySelector, window, resultType),new WindowTimeExtractor(resultType))
.keyBy(0);
return kPreStream.flatMap(afm);
}
示例6: getAnomalySteam
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
public DataStream<Tuple2<K, AnomalyResult>> getAnomalySteam(DataStream<V> ds, KeySelector<V, K> keySelector, KeySelector<V,Double> valueSelector, Time window) {
KeyedStream<V, K> keyedInput = ds
.keyBy(keySelector);
TypeInformation<Tuple2<K,Tuple4<Double,Double,Long,Long>>> resultType = (TypeInformation) new TupleTypeInfo<>(Tuple2.class,
new TypeInformation[] {keyedInput.getKeyType(), new TupleTypeInfo(Tuple4.class,
BasicTypeInfo.DOUBLE_TYPE_INFO, BasicTypeInfo.DOUBLE_TYPE_INFO, BasicTypeInfo.LONG_TYPE_INFO,BasicTypeInfo.LONG_TYPE_INFO)});
Tuple2<K,Tuple4<Double,Double,Long,Long>> init= new Tuple2<>(null,new Tuple4<>(0d,0d,0l,0l));
KeyedStream<Tuple2<K,Tuple4<Double,Double,Long,Long>>, Tuple> kPreStream = keyedInput
.timeWindow(window)
.apply(init, new CountSumFold<>(keySelector,valueSelector, resultType),new WindowTimeExtractor(resultType))
.keyBy(0);
return kPreStream.flatMap(afm);
}
示例7: getSelectorForKeys
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
public static <X> KeySelector<X, Tuple> getSelectorForKeys(Keys<X> keys, TypeInformation<X> typeInfo, ExecutionConfig executionConfig) {
if (!(typeInfo instanceof CompositeType)) {
throw new InvalidTypesException(
"This key operation requires a composite type such as Tuples, POJOs, or Case Classes.");
}
CompositeType<X> compositeType = (CompositeType<X>) typeInfo;
int[] logicalKeyPositions = keys.computeLogicalKeyPositions();
int numKeyFields = logicalKeyPositions.length;
TypeInformation<?>[] typeInfos = keys.getKeyFieldTypes();
// use ascending order here, the code paths for that are usually a slight bit faster
boolean[] orders = new boolean[numKeyFields];
for (int i = 0; i < numKeyFields; i++) {
orders[i] = true;
}
TypeComparator<X> comparator = compositeType.createComparator(logicalKeyPositions, orders, 0, executionConfig);
return new ComparableKeySelector<>(comparator, numKeyFields, new TupleTypeInfo<>(typeInfos));
}
示例8: getAnomalySteam
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
public DataStream<Tuple3<K, AnomalyResult, RV>> getAnomalySteam(DataStream<V> ds, KeySelector<V, K> keySelector, PayloadFold<V, RV> valueFold, Time window) {
KeyedStream<V, K> keyedInput = ds
.keyBy(keySelector);
TypeInformation<Object> foldResultType = TypeExtractor.getUnaryOperatorReturnType(valueFold,
PayloadFold.class,
false,
false,
ds.getType(),
"PayloadFold",
false);
TypeInformation<Tuple3<K,Tuple4<Double,Double,Long,Long>,RV>> resultType = (TypeInformation) new TupleTypeInfo<>(Tuple3.class,
new TypeInformation[] {keyedInput.getKeyType(), new TupleTypeInfo(Tuple4.class,
BasicTypeInfo.DOUBLE_TYPE_INFO, BasicTypeInfo.DOUBLE_TYPE_INFO, BasicTypeInfo.LONG_TYPE_INFO,BasicTypeInfo.LONG_TYPE_INFO), foldResultType});
Tuple3<K,Tuple4<Double,Double,Long,Long>, RV> init= new Tuple3<>(null,new Tuple4<>(0d,0d,0l,0l), valueFold.getInit());
KeyedStream<Tuple3<K,Tuple4<Double,Double,Long,Long>,RV>, Tuple> kPreStream = keyedInput
.timeWindow(window)
.apply(init, new ExtCountWindFold<>(keySelector,valueFold, window, resultType),new ExtWindowTimeExtractor(resultType))
.keyBy(0);
return kPreStream.flatMap(afm);
}
示例9: getAnomalySteam
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
public DataStream<Tuple2<K, AnomalyResult>> getAnomalySteam(DataStream<V> ds, KeySelector<V, K> keySelector, KeySelector<V,Double> valueSelector, Time window) {
KeyedStream<V, K> keyedInput = ds
.keyBy(keySelector);
TypeInformation<Tuple2<K,Tuple4<Double,Double,Long,Long>>> resultType = (TypeInformation) new TupleTypeInfo<>(Tuple2.class,
new TypeInformation[] {keyedInput.getKeyType(), new TupleTypeInfo(Tuple4.class,
BasicTypeInfo.DOUBLE_TYPE_INFO, BasicTypeInfo.DOUBLE_TYPE_INFO, BasicTypeInfo.LONG_TYPE_INFO,BasicTypeInfo.LONG_TYPE_INFO)});
Tuple2<K,Tuple4<Double,Double,Long,Long>> init= new Tuple2<>(null,new Tuple4<>(0d,0d,0l,0l));
KeyedStream<Tuple2<K,Tuple4<Double,Double,Long,Long>>, Tuple> kPreStream = keyedInput
.timeWindow(window)
.apply(init, new CountSumFold<>(keySelector,valueSelector, resultType),new WindowTimeExtractor<>(resultType))
.keyBy(0);
return kPreStream.flatMap(afm);
}
示例10: getTestHarness
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
private KeyedOneInputStreamOperatorTestHarness<Integer, Event, Map<String, List<Event>>> getTestHarness(
int maxParallelism,
int taskParallelism,
int subtaskIdx) throws Exception {
KeySelector<Event, Integer> keySelector = new TestKeySelector();
return new KeyedOneInputStreamOperatorTestHarness<>(
getKeyedCepOpearator(
false,
new NFAFactory()),
keySelector,
BasicTypeInfo.INT_TYPE_INFO,
maxParallelism,
taskParallelism,
subtaskIdx);
}
示例11: testSetupOfKeyGroupPartitioner
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
/**
* Tests that the KeyGroupStreamPartitioner are properly set up with the correct value of
* maximum parallelism.
*/
@Test
public void testSetupOfKeyGroupPartitioner() {
int maxParallelism = 42;
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.getConfig().setMaxParallelism(maxParallelism);
DataStream<Integer> source = env.fromElements(1, 2, 3);
DataStream<Integer> keyedResult = source.keyBy(new KeySelector<Integer, Integer>() {
private static final long serialVersionUID = 9205556348021992189L;
@Override
public Integer getKey(Integer value) throws Exception {
return value;
}
}).map(new NoOpIntMap());
keyedResult.addSink(new DiscardingSink<Integer>());
StreamGraph graph = env.getStreamGraph();
StreamNode keyedResultNode = graph.getStreamNode(keyedResult.getId());
StreamPartitioner<?> streamPartitioner = keyedResultNode.getInEdges().get(0).getPartitioner();
}
示例12: testPOJOnoHashCodeKeyRejection
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
@Test
public void testPOJOnoHashCodeKeyRejection() {
KeySelector<POJOWithoutHashCode, POJOWithoutHashCode> keySelector =
new KeySelector<POJOWithoutHashCode, POJOWithoutHashCode>() {
@Override
public POJOWithoutHashCode getKey(POJOWithoutHashCode value) throws Exception {
return value;
}
};
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStream<POJOWithoutHashCode> input = env.fromElements(
new POJOWithoutHashCode(new int[] {1, 2}));
// adjust the rule
expectedException.expect(InvalidProgramException.class);
input.keyBy(keySelector);
}
示例13: KeyedTwoInputStreamOperatorTestHarness
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
public KeyedTwoInputStreamOperatorTestHarness(
TwoInputStreamOperator<IN1, IN2, OUT> operator,
KeySelector<IN1, K> keySelector1,
KeySelector<IN2, K> keySelector2,
TypeInformation<K> keyType,
int maxParallelism,
int numSubtasks,
int subtaskIndex) throws Exception {
super(operator, maxParallelism, numSubtasks, subtaskIndex);
ClosureCleaner.clean(keySelector1, false);
ClosureCleaner.clean(keySelector2, false);
config.setStatePartitioner(0, keySelector1);
config.setStatePartitioner(1, keySelector2);
config.setStateKeySerializer(keyType.createSerializer(executionConfig));
setupMockTaskCreateKeyedBackend();
}
示例14: testSortPartitionWithKeySelector1
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
@Test
public void testSortPartitionWithKeySelector1() throws Exception {
/*
* Test sort partition on an extracted key
*/
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(4);
DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.get3TupleDataSet(env);
List<Tuple1<Boolean>> result = ds
.map(new IdMapper<Tuple3<Integer, Long, String>>()).setParallelism(4) // parallelize input
.sortPartition(new KeySelector<Tuple3<Integer, Long, String>, Long>() {
@Override
public Long getKey(Tuple3<Integer, Long, String> value) throws Exception {
return value.f1;
}
}, Order.ASCENDING)
.mapPartition(new OrderCheckMapper<>(new Tuple3AscendingChecker()))
.distinct().collect();
String expected = "(true)\n";
compareResultAsText(result, expected);
}
示例15: testSortPartitionWithKeySelector2
import org.apache.flink.api.java.functions.KeySelector; //导入依赖的package包/类
@Test
public void testSortPartitionWithKeySelector2() throws Exception {
/*
* Test sort partition on an extracted key
*/
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(4);
DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.get3TupleDataSet(env);
List<Tuple1<Boolean>> result = ds
.map(new IdMapper<Tuple3<Integer, Long, String>>()).setParallelism(4) // parallelize input
.sortPartition(new KeySelector<Tuple3<Integer, Long, String>, Tuple2<Integer, Long>>() {
@Override
public Tuple2<Integer, Long> getKey(Tuple3<Integer, Long, String> value) throws Exception {
return new Tuple2<>(value.f0, value.f1);
}
}, Order.DESCENDING)
.mapPartition(new OrderCheckMapper<>(new Tuple3Checker()))
.distinct().collect();
String expected = "(true)\n";
compareResultAsText(result, expected);
}