1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| import org.apache.flink.api.java.functions.KeySelector; import org.apache.flink.api.java.tuple.Tuple3; import org.apache.flink.streaming.api.datastream.DataStreamSource; import org.apache.flink.streaming.api.datastream.KeyedStream; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import java.util.ArrayList; import java.util.List; public class TSource { public static void main(String[] args) throws Exception { StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); List list = new ArrayList<Tuple3<Integer, Integer, String>>(); list.add(new Tuple3<>(0, 1, "a")); list.add(new Tuple3<>(0, 3, "b")); list.add(new Tuple3<>(0, 2, "c")); list.add(new Tuple3<>(0, 4, "d")); list.add(new Tuple3<>(1, 5, "a")); list.add(new Tuple3<>(1, 2, "b")); list.add(new Tuple3<>(1, 7, "c")); DataStreamSource<Tuple3<Integer, Integer, String>> stringDataStreamSource = env.fromCollection(list); KeyedStream<Tuple3<Integer, Integer, String>, Integer> result = stringDataStreamSource .keyBy(0); result.max(1).print("max最大值"); result.maxBy(1).print("maxBy元素");
// min,minBy同理 env.execute("测试"); } }
原数据:
原数据:3> (0,1,a) 原数据:3> (0,3,b) 原数据:3> (0,2,c) 原数据:3> (0,4,d) 原数据:3> (1,5,a) 原数据:3> (1,2,b) 原数据:3> (1,7,c)
返回结果: max最大值:3> (0,1,a) max最大值:3> (0,3,a) max最大值:3> (0,3,a) max最大值:3> (0,4,a) max最大值:3> (1,5,a) max最大值:3> (1,5,a) max最大值:3> (1,7,a)
maxBy元素:3> (0,1,a) maxBy元素:3> (0,3,b) maxBy元素:3> (0,3,b) maxBy元素:3> (0,4,d) maxBy元素:3> (1,5,a) maxBy元素:3> (1,5,a) maxBy元素:3> (1,7,c)
|