如何在其他基础上过滤Apache flink流?

时间:2017-04-20 13:12:08

标签: apache-flink flink-streaming

我有两个流一个是Int,另一个是json。在json Schema中有一个键是一些int。所以我需要通过与其他整数流的密钥比较来过滤json流所以是否可能在Flink?

1 个答案:

答案 0 :(得分:3)

是的,您可以使用Flink进行此类流处理。 Flink所需的基本构建块是连接流和有状态函数 - 这是使用RichCoFlatMap的示例:

import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.RichCoFlatMapFunction;
import org.apache.flink.util.Collector;

public class Connect {
    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<Event> control = env.fromElements(
                new Event(17),
                new Event(42))
                .keyBy("key");

        DataStream<Event> data = env.fromElements(
                new Event(2),
                new Event(42),
                new Event(6),
                new Event(17),
                new Event(8),
                new Event(42)
                )
                .keyBy("key");

        DataStream<Event> result = control
                .connect(data)
                .flatMap(new MyConnectedStreams());

        result.print();

        env.execute();
    }

    static final class MyConnectedStreams
            extends RichCoFlatMapFunction<Event, Event, Event> {

        private ValueState<Boolean> seen = null;

        @Override
        public void open(Configuration config) {
            ValueStateDescriptor<Boolean> descriptor = new ValueStateDescriptor<>(
                    // state name
                    "have-seen-key",
                    // type information of state
                    TypeInformation.of(new TypeHint<Boolean>() {
                    }));
            seen = getRuntimeContext().getState(descriptor);
        }

        @Override
        public void flatMap1(Event control, Collector<Event> out) throws Exception {
            seen.update(Boolean.TRUE);
        }

        @Override
        public void flatMap2(Event data, Collector<Event> out) throws Exception {
            if (seen.value() == Boolean.TRUE) {
                out.collect(data);
            }
        }
    }


    public static final class Event {
        public Event() {
        }

        public Event(int key) {
            this.key = key;
        }

        public int key;

        public String toString() {
            return String.valueOf(key);
        }
    }
}

在此示例中,只有在控制流上看到的那些键才会通过数据流传递 - 所有其他事件都将被过滤掉。我已利用Flink's managed keyed stateconnected streams

为了保持这一点,我忽略了您对数据流具有JSON的要求,但您可以找到如何在其他地方使用JSON和Flink的示例。

请注意,您的结果将是不确定的,因为您无法控制两个流相对于彼此的时间。您可以通过向流添加事件时间戳,然后使用RichCoProcessFunction来管理它。