如何在Flink中支持多个KeyBy

时间:2017-09-19 08:42:16

标签: apache-kafka apache-flink

在下面的代码示例中,我正在尝试获取员工记录流{Country,Employer,Name,Salary,Age}并在每个国家/地区倾销最高薪员工。不幸的是,多个KEY并不起作用。

只有KeyBy(雇主)反映,因此我没有得到正确的结果。 我错过了什么?

    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

    DataStream<Employee> streamEmployee = env.addSource(
            new FlinkKafkaConsumer010<ObjectNode>("flink-demo", new JSONDeserializationSchema(), properties))
            .map(new MapFunction<ObjectNode, Employee>() {

                private static final long serialVersionUID = 6111226274068863916L;

                @Override
                public Employee map(ObjectNode value) throws Exception {
                    final Gson gson = new GsonBuilder().create();
                    Employee uMsg = gson.fromJson(value.toString(), Employee.class);
                    return uMsg;
                }
            });

    KeyedStream<Employee, String> employeesKeyedByCountryndEmployer = streamEmployee
            .keyBy(new KeySelector<Employee, String>() {
                private static final long serialVersionUID = -6867736771747690202L;

                @Override
                public String getKey(Employee value) throws Exception {
                    // TODO Auto-generated method stub
                    return value.getCountry();
                }
            }).keyBy(new KeySelector<Employee, String>() {
                private static final long serialVersionUID = -6867736771747690202L;

                @Override
                public String getKey(Employee value) throws Exception {
                    // TODO Auto-generated method stub
                    return value.getEmployer();
                }
            });
    // This should display employees highly paid in a given country , for a
    // given employer
    DataStream<Employee> uHighlyPaidEmployee = employeesKeyedByCountryndEmployer.timeWindow(Time.seconds(5))
            .maxBy("salary");

    // Assume toString() is overridden , so print works well.
    uHighlyPaidEmployee.print();

    env.execute("Employee-employer log processor");

2 个答案:

答案 0 :(得分:6)

您可以定义一个返回复合键的KeySelector

KeyedStream<Employee, Tuple2<String, String>> employeesKeyedByCountryndEmployer = 
  streamEmployee.keyBy(
    new KeySelector<Employee, Tuple2<String, String>>() {

      @Override
      public Tuple2<String, String> getKey(Employee value) throws Exception {
        return Tuple2.of(value.getCountry(), value.getEmployer());
      }
    }
  );

答案 1 :(得分:0)

如果尝试用lambda表达式替换代码,则会遇到以下问题:https://ci.apache.org/projects/flink/flink-docs-stable/dev/java_lambdas.html