创建实体类 @Data @AllArgsConstructor @NoArgsConstructor public class Demo1 { private String user; private String url; private Long ts; }
//环境设置 StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); //设置并行度 env.setParallelism(1); //自定义数据源 GeneratorFunction<Long, Demo1> generatorFunction = new GeneratorFunction<Long, Demo1>(){ @Override public Demo1 map(Long aLong) throws Exception { String[]users="Mary,Alice,Bob,Cary".split(","); String[]urls="./home,./cart,./fav,./prod?id=1,./prod?id=2".split(","); //生成随机数 Random random = new Random(); Demo1 demo1 = new Demo1(); demo1.setUser(users[random.nextInt(users.length)]); demo1.setUrl(urls[random.nextInt(urls.length)]); //System.currentTimeMillis():当前时间戳 demo1.setTs(System.currentTimeMillis()); return demo1; } }; //模拟无界流 long numberOfRecords = Long.MAX_VALUE; DataGeneratorSource<Demo1> source = new DataGeneratorSource<>( generatorFunction, numberOfRecords, //一共产生多少条 RateLimiterStrategy.perSecond(1),//每一秒产生一条数据 Types.POJO(Demo1.class)); DataStreamSource<Demo1> stream = env.fromSource(source, WatermarkStrategy .<Demo1>forBoundedOutOfOrderness(Duration.ofSeconds(3)) .withTimestampAssigner((event, timestamp) -> event.getTs() ), "Generator Source"); // stream.print(); //转换为二元组 SingleOutputStreamOperator<Tuple2<String, Integer>> reduceTuple = stream.map(value -> Tuple2.of(value.getUser(), 1)) .returns(Types.TUPLE(Types.STRING, Types.INT))//二元组的数据类型 .keyBy(k -> k.f0) .reduce(new ReduceFunction<Tuple2<String, Integer>>() { public Tuple2<String, Integer> reduce(Tuple2<String, Integer> v1, Tuple2<String, Integer> v2) { return new Tuple2<>(v1.f0, v1.f1 + v2.f1); } }); reduceTuple.print(); env.execute();