keyBy算子是将DataStream里的数据按照指定的key进行分组,具体实现可大致分为两类,1 传入的参数为下标值,这种一般只适用于DataStream里的数据类型为元组方可使用,2 传入的参数为keySelector或者其引用(java的lambda表达式),实现代码如下:
import org.apache.flink.api.common.operators.Keys;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
public class KeyByDemo4 {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//辽宁省,沈阳市,1000
//辽宁省,大连市,2000
//山东省,青岛市,3000
//山东省,烟台市,2000
//辽宁省,大连市,2000
DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);
SingleOutputStreamOperator<ProvinceCityAndMoney2> tpDataStream = lines.map(line -> {
String[] fields = line.split(",");
return ProvinceCityAndMoney2.of(fields[0], fields[1], Double.parseDouble(fields[2]));
});
//KeyedStream<ProvinceCityAndMoney, Tuple> grouped = tpDataStream.keyBy("province", "city");
// KeyedStream<ProvinceCityAndMoney, Tuple2<String, String>> grouped = tpDataStream.keyBy(new KeySelector<ProvinceCityAndMoney, Tuple2<String, String>>() {
// @Override
// public Tuple2<String, String> getKey(ProvinceCityAndMoney value) throws Exception {
// return Tuple2.of(value.getProvince(), value.getCity());
// }
// });
KeyedStream<ProvinceCityAndMoney2, Tuple2<String, String>> grouped = tpDataStream.keyBy(new KeySelector<ProvinceCityAndMoney2, Tuple2<String, String>>() {
@Override
public Tuple2<String, String> getKey(ProvinceCityAndMoney2 value) throws Exception {
return Tuple2.of(value.province, value.city);
}
});
SingleOutputStreamOperator<ProvinceCityAndMoney2> summed = grouped.sum("money");
summed.print();
env.execute();
}
//自定义的Pojo类封装数据,必须是Public
public static class ProvinceCityAndMoney2 {
public String province;
public String city;
public Double money;
public ProvinceCityAndMoney2(){}
public ProvinceCityAndMoney2(String province, String city, Double money) {
this.province = province;
this.city = city;
this.money = money;
}
public static ProvinceCityAndMoney2 of(String province, String city, Double money) {
return new ProvinceCityAndMoney2(province, city, money);
}
@Override
public String toString() {
return "ProvinceCityAndMoney{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
", money=" + money +
'}';
}
}
}