Search in sources :

Example 11 with KeySelector

use of org.apache.flink.api.java.functions.KeySelector in project flink by apache.

the class JoinOperatorTest method testJoinKeySelectors1.

@Test
public void testJoinKeySelectors1() {
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    DataSet<CustomType> ds1 = env.fromCollection(customTypeData);
    DataSet<CustomType> ds2 = env.fromCollection(customTypeData);
    // should work
    try {
        ds1.join(ds2).where(new KeySelector<CustomType, Long>() {

            @Override
            public Long getKey(CustomType value) {
                return value.myLong;
            }
        }).equalTo(new KeySelector<CustomType, Long>() {

            @Override
            public Long getKey(CustomType value) {
                return value.myLong;
            }
        });
    } catch (Exception e) {
        Assert.fail();
    }
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) KeySelector(org.apache.flink.api.java.functions.KeySelector) InvalidProgramException(org.apache.flink.api.common.InvalidProgramException) Test(org.junit.Test)

Example 12 with KeySelector

use of org.apache.flink.api.java.functions.KeySelector in project flink by apache.

the class GroupingTest method testGroupSortByKeySelector2.

@SuppressWarnings("serial")
@Test(expected = InvalidProgramException.class)
public void testGroupSortByKeySelector2() {
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    DataSet<Tuple4<Integer, Long, CustomType, Long[]>> tupleDs = env.fromCollection(tupleWithCustomData, tupleWithCustomInfo);
    // should not work
    tupleDs.groupBy(new KeySelector<Tuple4<Integer, Long, CustomType, Long[]>, Long>() {

        @Override
        public Long getKey(Tuple4<Integer, Long, CustomType, Long[]> value) throws Exception {
            return value.f1;
        }
    }).sortGroup(new KeySelector<Tuple4<Integer, Long, CustomType, Long[]>, CustomType>() {

        @Override
        public CustomType getKey(Tuple4<Integer, Long, CustomType, Long[]> value) throws Exception {
            return value.f2;
        }
    }, Order.ASCENDING);
}
Also used : Tuple4(org.apache.flink.api.java.tuple.Tuple4) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) KeySelector(org.apache.flink.api.java.functions.KeySelector) InvalidProgramException(org.apache.flink.api.common.InvalidProgramException) Test(org.junit.Test)

Example 13 with KeySelector

use of org.apache.flink.api.java.functions.KeySelector in project flink by apache.

the class JoinOperatorTest method testJoinProjection6.

@Test
public void testJoinProjection6() {
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    DataSet<CustomType> ds1 = env.fromCollection(customTypeData);
    DataSet<CustomType> ds2 = env.fromCollection(customTypeData);
    // should work
    try {
        ds1.join(ds2).where(new KeySelector<CustomType, Long>() {

            @Override
            public Long getKey(CustomType value) {
                return value.myLong;
            }
        }).equalTo(new KeySelector<CustomType, Long>() {

            @Override
            public Long getKey(CustomType value) {
                return value.myLong;
            }
        }).projectFirst().projectSecond();
    } catch (Exception e) {
        System.out.println("FAILED: " + e);
        e.printStackTrace();
        Assert.fail();
    }
}
Also used : ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) KeySelector(org.apache.flink.api.java.functions.KeySelector) InvalidProgramException(org.apache.flink.api.common.InvalidProgramException) Test(org.junit.Test)

Example 14 with KeySelector

use of org.apache.flink.api.java.functions.KeySelector in project flink by apache.

the class AvroTypeExtractionTest method testWithKryoGenericSer.

@Test
public void testWithKryoGenericSer() throws Exception {
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().enableForceKryo();
    Path in = new Path(inFile.getAbsoluteFile().toURI());
    AvroInputFormat<User> users = new AvroInputFormat<>(in, User.class);
    DataSet<User> usersDS = env.createInput(users);
    DataSet<Tuple2<String, Integer>> res = usersDS.groupBy((KeySelector<User, String>) value -> String.valueOf(value.getName())).reduceGroup((GroupReduceFunction<User, Tuple2<String, Integer>>) (values, out) -> {
        for (User u : values) {
            out.collect(new Tuple2<>(u.getName().toString(), 1));
        }
    }).returns(Types.TUPLE(Types.STRING, Types.INT));
    res.writeAsText(resultPath);
    env.execute("Avro Key selection");
    expected = "(Charlie,1)\n(Alyssa,1)\n";
}
Also used : Path(org.apache.flink.core.fs.Path) Arrays(java.util.Arrays) Tuple2(org.apache.flink.api.java.tuple.Tuple2) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) MultipleProgramsTestBase(org.apache.flink.test.util.MultipleProgramsTestBase) MapFunction(org.apache.flink.api.common.functions.MapFunction) AvroRecordInputFormatTest(org.apache.flink.formats.avro.AvroRecordInputFormatTest) DataSet(org.apache.flink.api.java.DataSet) Path(org.apache.flink.core.fs.Path) After(org.junit.After) Map(java.util.Map) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) Types(org.apache.flink.api.common.typeinfo.Types) AvroInputFormat(org.apache.flink.formats.avro.AvroInputFormat) Fixed16(org.apache.flink.formats.avro.generated.Fixed16) KeySelector(org.apache.flink.api.java.functions.KeySelector) GroupReduceFunction(org.apache.flink.api.common.functions.GroupReduceFunction) Test(org.junit.Test) File(java.io.File) Rule(org.junit.Rule) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) User(org.apache.flink.formats.avro.generated.User) Assert(org.junit.Assert) TemporaryFolder(org.junit.rules.TemporaryFolder) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) User(org.apache.flink.formats.avro.generated.User) GroupReduceFunction(org.apache.flink.api.common.functions.GroupReduceFunction) AvroInputFormat(org.apache.flink.formats.avro.AvroInputFormat) Tuple2(org.apache.flink.api.java.tuple.Tuple2) AvroRecordInputFormatTest(org.apache.flink.formats.avro.AvroRecordInputFormatTest) Test(org.junit.Test)

Example 15 with KeySelector

use of org.apache.flink.api.java.functions.KeySelector in project flink by apache.

the class QsStateProducer method main.

public static void main(final String[] args) throws Exception {
    final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    ParameterTool tool = ParameterTool.fromArgs(args);
    String tmpPath = tool.getRequired("tmp-dir");
    String stateBackendType = tool.getRequired("state-backend");
    StateBackend stateBackend;
    switch(stateBackendType) {
        case "rocksdb":
            stateBackend = new RocksDBStateBackend(tmpPath);
            break;
        case "fs":
            stateBackend = new FsStateBackend(tmpPath);
            break;
        case "memory":
            stateBackend = new MemoryStateBackend();
            break;
        default:
            throw new RuntimeException("Unsupported state backend " + stateBackendType);
    }
    env.setStateBackend(stateBackend);
    env.enableCheckpointing(1000L);
    env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
    env.getCheckpointConfig().setMinPauseBetweenCheckpoints(0);
    env.addSource(new EmailSource()).keyBy(new KeySelector<Email, String>() {

        private static final long serialVersionUID = -1480525724620425363L;

        @Override
        public String getKey(Email value) throws Exception {
            return QsConstants.KEY;
        }
    }).flatMap(new TestFlatMap());
    env.execute();
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) RocksDBStateBackend(org.apache.flink.contrib.streaming.state.RocksDBStateBackend) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) KeySelector(org.apache.flink.api.java.functions.KeySelector) StateBackend(org.apache.flink.runtime.state.StateBackend) FsStateBackend(org.apache.flink.runtime.state.filesystem.FsStateBackend) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) RocksDBStateBackend(org.apache.flink.contrib.streaming.state.RocksDBStateBackend) FsStateBackend(org.apache.flink.runtime.state.filesystem.FsStateBackend)

Aggregations

KeySelector (org.apache.flink.api.java.functions.KeySelector)120 Test (org.junit.Test)113 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)45 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)44 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)39 Watermark (org.apache.flink.streaming.api.watermark.Watermark)30 List (java.util.List)29 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)28 InvalidProgramException (org.apache.flink.api.common.InvalidProgramException)22 JobID (org.apache.flink.api.common.JobID)22 JobGraph (org.apache.flink.runtime.jobgraph.JobGraph)22 IOException (java.io.IOException)21 Arrays (java.util.Arrays)21 AtomicLong (java.util.concurrent.atomic.AtomicLong)21 Configuration (org.apache.flink.configuration.Configuration)21 KeyedOneInputStreamOperatorTestHarness (org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness)21 ArrayList (java.util.ArrayList)18 Map (java.util.Map)18 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)18 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)16