// 将数据解析为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));
}
});
Flink计算Windowall算子
最新推荐文章于 2024-01-14 11:04:28 发布