Flink计算Windowall算子

// 将数据解析为Stock对象
        DataStream<Stock> stocks = stream.map(new MapFunction<String, Stock>() {
            @Override
            public Stock map(String value) throws Exception {
                try {
                    String[] fields = value.split(",");
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date timestamp = dateFormat.parse(fields[0]);
                    String id = fields[1];
                    String name = fields[2];
                    double price = Double.parseDouble(fields[3]);
                    int quantity = Integer.parseInt(fields[4]);
                    String action = fields[5];
                    String province = fields[6];
                    String broker = fields[7];
                    String industry = fields[8];

                    return new Stock(timestamp, id, name, price, quantity, action, province, broker, industry);
                }
                catch (Exception e){
                    return null;
                }
            }
        }).filter(Objects::nonNull);

        // 统计每秒的总交易金额、交易数量和已处理速度,单位为“条/秒”
        DataStream<Tuple7<Double, Integer, Integer, Integer, Map<String,Double>, Map<String,Integer>, Map<String,Integer>>> windowedBySecondData = stocks
                .windowAll(TumblingProcessingTimeWindows.of(Time.seconds(1)))
                .apply(new AllWindowFunction<Stock, Tuple7<Double, Integer, Integer, Integer, Map<String,Double>, Map<String,Integer>, Map<String,Integer>>, TimeWindow>() {
                    @Override
                    public void apply(TimeWindow window, Iterable<Stock> values, Collector<Tuple7<Double, Integer, Integer, Integer, Map<String,Double>, Map<String,Integer>, Map<String,Integer>>> out) throws Exception {
                        double perSecondAmount = 0.0;
                        int perSecondCount = 0;
                        int perSecondSaleCount = 0;
                        int perSecondBuyCount = 0;
                        Map<String,Double> perSecondAmountNameMap = new HashMap<>();
                        Map<String,Integer> perSecondCountPlatformMap = new HashMap<>();
                        Map<String,Integer> perSecondCountProvinceMap = new HashMap<>();
                        for (Stock stock : values) {
                            perSecondCount++;
                            perSecondAmount += stock.getPrice() * stock.getQuantity();
                            if(Objects.equals(stock.getAction(), "买入")) perSecondBuyCount ++;
                            else perSecondSaleCount ++;
                            perSecondAmountNameMap.compute(stock.getName(), (key, value) -> {
                                if (value == null) {
                                    // 如果键不存在,则直接放入新值
                                    return stock.getPrice() * stock.getQuantity();
                                } else {
                                    // 如果键存在,则将新值与旧值相加
                                    return value + stock.getPrice() * stock.getQuantity();
                                }
                            });
                            perSecondCountPlatformMap.compute(stock.getBroker(), (key, value) -> {
                                if (value == null) {
                                    // 如果键不存在,则直接放入新值
                                    return 1;
                                } else {
                                    // 如果键存在,则将新值与旧值相加
                                    return value + 1;
                                }
                            });
                            perSecondCountProvinceMap.compute(stock.getProvince(), (key, value) -> {
                                if (value == null) {
                                    // 如果键不存在,则直接放入新值
                                    return 1;
                                } else {
                                    // 如果键存在,则将新值与旧值相加
                                    return value + 1;
                                }
                            });
                        }
                        System.out.println("每秒处理:"+perSecondCount);
                        out.collect(new Tuple7<>(perSecondAmount, perSecondCount, perSecondSaleCount, perSecondBuyCount, perSecondAmountNameMap, perSecondCountPlatformMap,perSecondCountProvinceMap));
                    }
                });

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值