0
votes

I have a DataStream have different fields that I want to keyBy to do aggregated computation on (count, average, ..)

stream.keyBy("field1").window().aggregate(AggFunc, WindowFunc)...

stream.keyBy("field2").window().aggregate(AggFunc, WindowFunc)...

stream.keyBy("field3").window().aggregate(AggFunc, WindowFunc)...

Is there a way to get name of the keyed field in later WindowFunc ("field1", "field2", "field3")? Note that I want the field name "field1" not the field possible values (this I already have in key in apply function of WindowFunction).

The reason: I want to use the same WindowFunc for 3 aggregations - here I add window_start_time, key_field_name, key_value to the result.

Wanted result example:

-stream keyed on field1

("field1", "field1-val1", 3, window1)
("field1", "field1-val2", 5, window1)

-stream keyed on field2

("field2", "field2-val1", 6, window1)
("field2", "field2-val2", 7, window1)
1

1 Answers

1
votes

No, the WindowFunction does not give access to the name of a key field. However, you can add a parameter to the constructor of your WindowFunction and pass the field name there.

This would look similar to the following example

// define window function

public static class MyWindowFunc implements WindowFunction<...> { 
    private final String keyname;

    public MyWindowFunc(String keyname) {
        this.keyname = keyname;
    }    

  ...
}

// use window function

stream.keyBy("field1").window().aggregate(AggFunc, new MyWindowFunc("field1"))...
stream.keyBy("field2").window().aggregate(AggFunc, new MyWindowFunc("field2"))...
stream.keyBy("field3").window().aggregate(AggFunc, new MyWindowFunc("field3"))...