Flink算子简单测试样例

Flink算子简单测试样例

1. 创建执行环境
c 复制代码
        // 创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

2. 创建数据流
c 复制代码
        // 创建数据流
        DataStream<String> source = env.addSource(new DataGeneratorSource<>(new DataGenerator<String>() {
            final int CNT = 10000; // 模拟一万条数
            int i = 0;

            @Override
            public void open(String s, FunctionInitializationContext functionInitializationContext, RuntimeContext runtimeContext) throws Exception {}

            @Override
            public boolean hasNext() {
                return i < CNT;
            }

            @Override
            public String next() {
                i++;
                try {
                    Thread.sleep(new Random().nextInt(2000)); // 随机发生时间
                } catch (InterruptedException e) {
                }
                return "" + i;
            }
        })).returns(String.class).uid("source").name("source");

3. 数据补充
c 复制代码
        // 数据补充-添加时间戳,增加金额
        SingleOutputStreamOperator<Map<String, String>> mapOperator = source.map((MapFunction<String, Map<String, String>>) s -> {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("userid", s);
            hashMap.put("amt", new Random().nextInt(100) + "");
            hashMap.put("time", System.currentTimeMillis() + "");
            return hashMap;
        }).returns(TypeInformation.of(new TypeHint<Map<String, String>>() {
        })).uid("mapOperator").name("mapOperator");

4. 数据过滤
c 复制代码
        // 数据过滤-只取时间戳为偶数的数据
        SingleOutputStreamOperator<Map<String, String>> filterOperator = mapOperator.filter((FilterFunction<Map<String, String>>) data -> {
//                System.out.println("从mapOperator接到数据:" + data);
            long time = Long.parseLong(data.get("time"));
            return time % 2 == 0;
        }).returns(TypeInformation.of(new TypeHint<Map<String, String>>() {
        })).uid("filterOperator").name("filterOperator");

5. 数据放大
c 复制代码
        // 数据放大-时间戳是4的倍数,双倍奖励,8的倍数,三倍奖励
        SingleOutputStreamOperator<Map<String, String>> flatMapOperator = filterOperator.flatMap((FlatMapFunction<Map<String, String>, Map<String, String>>) (data, collector) -> {
            collector.collect(data);
            if (Long.parseLong(data.get("time")) % 4 == 0) {
                collector.collect(data);
            }
            if (Long.parseLong(data.get("time")) % 8 == 0) {
                collector.collect(data);
            }
        }).returns(TypeInformation.of(new TypeHint<Map<String, String>>() {
        })).uid("flatMapOperator").name("flatMapOperator");

6. 数据输出
c 复制代码
        // 数据输出
        flatMapOperator.print();

        // 执行程序
        env.execute("FlinkTest");

7. 执行结果
c 复制代码
{amt=45, time=1705048891056, userid=4}
{amt=45, time=1705048891056, userid=4}
{amt=45, time=1705048891056, userid=4}
{amt=56, time=1705048894374, userid=6}
{amt=96, time=1705048899462, userid=10}
{amt=65, time=1705048901638, userid=12}
{amt=33, time=1705048902544, userid=13}
{amt=33, time=1705048902544, userid=13}
{amt=33, time=1705048902544, userid=13}
{amt=10, time=1705048903748, userid=14}
{amt=10, time=1705048903748, userid=14}
...

Process finished with exit code 0
相关推荐
字节跳动数据平台1 小时前
5000 字技术向拆解 | 火山引擎多模态数据湖如何释放模思智能的算法生产力
大数据
武子康7 小时前
大数据-239 离线数仓 - 广告业务实战:Flume 导入日志到 HDFS,并完成 Hive ODS/DWD 分层加载
大数据·后端·apache hive
字节跳动数据平台1 天前
代码量减少 70%、GPU 利用率达 95%:火山引擎多模态数据湖如何释放模思智能的算法生产力
大数据
得物技术1 天前
深入剖析Spark UI界面:参数与界面详解|得物技术
大数据·后端·spark
大大大大晴天1 天前
Flink生产问题排障-HBase NotServingRegionException
flink·hbase
武子康1 天前
大数据-238 离线数仓 - 广告业务 Hive分析实战:ADS 点击率、购买率与 Top100 排名避坑
大数据·后端·apache hive
武子康2 天前
大数据-237 离线数仓 - Hive 广告业务实战:ODS→DWD 事件解析、广告明细与转化分析落地
大数据·后端·apache hive
大大大大晴天2 天前
Flink生产问题排障-Kryo serializer scala extensions are not available
大数据·flink
武子康4 天前
大数据-236 离线数仓 - 会员指标验证、DataX 导出与广告业务 ODS/DWD/ADS 全流程
大数据·后端·apache hive
武子康5 天前
大数据-235 离线数仓 - 实战:Flume+HDFS+Hive 搭建 ODS/DWD/DWS/ADS 会员分析链路
大数据·后端·apache hive