複数のApache FLINKウィンドウの検証

イエススニガ:

私はApacheのFLINKを使用してストリーム処理に始めるよ、事は、私は、JSONのストリームを受信して​​いますということである。このようになります。

{

  token_id: “tok_afgtryuo”,

  ip_address: “128.123.45.1“,

  device_fingerprint: “abcghift”,

  card_hash: “hgtyuigash”,

  “bin_number”: “424242”,

  “last4”: “4242”,

  “name”: “Seu Jorge”

}

私は、次のビジネスルールを満たすことができればと頼まれました:

  • 拒否した場合、最後の10秒でこのIPのためのトークンの数> 5

  • 拒否した場合、最後の分で、このIPのためのトークンの数> 15

  • 拒否した場合、最後の時間で、このIPのためのトークンの数> 60

私は、2クラスを作ったmain私がコールするインスタンスを作ってるんだときに、クラスをWindow重複したコードを避けるために、異なるパラメータを持つ関数を:

Main.java

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //This DataStream Would be  Converting the Json to a Token Object
        DataStream<Token> baseStream =
                env.addSource(new SocketTextStreamFunction("localhost",
                        9999,
                        "\n",
                        1))
                        .map(new MapTokens());


        // 1- First rule Decline if number of tokens > 5 for this IP in last 10 seconds
       DataStreamSink<String> response1 =  new RuleMaker().getStreamKeyCount(baseStream, "ip", Time.seconds(10),
               5, "seconds").print();

        //2 -Decline if number of tokens > 15 for this IP in last minute
        DataStreamSink<String> response2 = new RuleMaker().getStreamKeyCount(baseStream, "ip", Time.minutes(1),
                62, "minutes").print();

        //3- Decline if number of tokens > 60 for this IP in last hour
        DataStreamSink<String> response3  = new RuleMaker().getStreamKeyCount(baseStream, "ip", Time.hours(1),
                60, "Hours").print();

        env.execute("Job2");
    }

そして、私はルールのすべてのロジックをやっている別のクラスは、私はそれがより多くの時間ウィンドウ内の許容数を超える場合、私はいくつかの情報を含むメッセージを返すよ、どこIPアドレスが表示されます時間をカウントしています:

Rulemaker.java

public class RuleMaker {


    public DataStream<String> getStreamKeyCount(DataStream<Token> stream, 
                                                String tokenProp,
                                                Time time, 
                                                Integer maxPetitions, 
                                                String ruleType){

        return
               stream
                .flatMap(new FlatMapFunction<Token, Tuple3<String, Integer, String>>() {
                    @Override
                    public void flatMap(Token token, Collector<Tuple3<String, Integer, String>> collector) throws Exception {

                         String tokenSelection = "";
                        switch (tokenProp)
                        {
                            case "ip":
                                tokenSelection = token.getIpAddress();
                                break;
                            case "device":
                                tokenSelection = token.getDeviceFingerprint();
                                break;
                            case "cardHash":
                                tokenSelection = token.getCardHash();
                                break;
                        }
                        collector.collect(new Tuple3<>(tokenSelection, 1, token.get_tokenId()));
                    }
                })
                .keyBy(0)
                .timeWindow(time)
                .process(new MyProcessWindowFunction(maxPetitions, ruleType));
    }

    //Class to process the elements from the window
    private class MyProcessWindowFunction extends ProcessWindowFunction<
            Tuple3<String, Integer, String>,
            String,
            Tuple,
            TimeWindow
            > {

        private Integer _maxPetitions;
        private String  _ruleType;


        public MyProcessWindowFunction(Integer maxPetitions, String ruleType) {
            this._maxPetitions = maxPetitions;
            this._ruleType = ruleType;
        }

        @Override
        public void process(Tuple tuple, Context context, Iterable<Tuple3<String, Integer, String>> iterable, Collector<String> out) throws Exception {

            Integer counter = 0;
            for (Tuple3<String, Integer, String> element : iterable) {
                counter += element.f1++;
                if(counter > _maxPetitions){
                    out.collect("El elemeto ha sido declinado: " + element.f2 + " Num elements: " + counter + " rule type: " +  _ruleType + " token: " + element.f0 );
                    counter = 0;
                }
            }
        }
    }
}

これまでのところ、私はこのコードが動作していると思いますが、私は、Apache FLINK上begginerだし、あなたはそれの何かが間違っている場合、私はこれを仕事にしようとしている道を教えてとに私を指すことができれば、私は多くのことを感謝します右方向。

どうもありがとう。

Arvidハイゼ:

私はと思っているだろうが、非常に良い一般的なアプローチのルックス、テーブルAPIをサポートして助けます(より簡潔)に強力な十分だろうJSONを箱から出して。

あなたはデータストリームAPIに固執したい場合は、でgetStreamKeyCount、スイッチの周りには、tokenPropキー抽出を渡して交換する必要がありますgetStreamKeyCount新しいルールを追加するだけの場所を持っています。

public DataStream<String> getStreamKeyCount(DataStream<Token> stream, 
                                            KeySelector<Token, String> keyExtractor,
                                            Time time, 
                                            Integer maxPetitions, 
                                            String ruleType){

    return stream
         .map(token -> new Tuple3<>(keyExtractor.getKey(token), 1, token.get_tokenId()))
            .keyBy(0)
            .timeWindow(time)
            .process(new MyProcessWindowFunction(maxPetitions, ruleType));
}

その後、呼び出しはなり

DataStreamSink<String> response2 = ruleMaker.getStreamKeyCount(baseStream, 
    Token::getIpAddress, Time.minutes(1), 62, "minutes");

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=6352&siteId=1