【问题标题】:Join two streams using a count-based window使用基于计数的窗口加入两个流
【发布时间】:2016-06-10 18:09:31
【问题描述】:

我是 Flink Streaming API 的新手,我想完成以下简单 (IMO) 任务。我有两个流,我想使用基于计数的窗口加入它们。我到目前为止的代码如下:

public class BaselineCategoryEquiJoin {

private static final String recordFile = "some_file.txt";

private static class ParseRecordFunction implements MapFunction<String, Tuple2<String[], MyRecord>> {
    public Tuple2<String[], MyRecord> map(String s) throws Exception {
        MyRecord myRecord = parse(s);
        return new Tuple2<String[], myRecord>(myRecord.attributes, myRecord);
    }
}

public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment environment = StreamExecutionEnvironment.createLocalEnvironment();
    ExecutionConfig config = environment.getConfig();
    config.setParallelism(8);
    DataStream<Tuple2<String[], MyRecord>> dataStream = environment.readTextFile(recordFile)
            .map(new ParseRecordFunction());
    DataStream<Tuple2<String[], MyRecord>> dataStream1 = environment.readTextFile(recordFile)
            .map(new ParseRecordFunction());
    DataStreamSink<Tuple2<String[], String[]>> joinedStream = dataStream1
            .join(dataStream)
            .where(new KeySelector<Tuple2<String[],MyRecord>, String[]>() {
                public String[] getKey(Tuple2<String[], MyRecord> recordTuple2) throws Exception {
                    return recordTuple2.f0;
                }
            }).equalTo(new KeySelector<Tuple2<String[], MyRecord>, String[]>() {
                public String[] getKey(Tuple2<String[], MyRecord> recordTuple2) throws Exception {
                    return recordTuple2.f0;
                }
            }).window(TumblingProcessingTimeWindows.of(Time.seconds(1)))
            .apply(new JoinFunction<Tuple2<String[],MyRecord>, Tuple2<String[],MyRecord>, Tuple2<String[], String[]>>() {
                public Tuple2<String[], String[]> join(Tuple2<String[], MyRecord> tuple1, Tuple2<String[], MyRecord> tuple2) throws Exception {
                    return new Tuple2<String[], String[]>(tuple1.f0, tuple1.f0);
                }
            }).print();
    environment.execute();
}
}

我的代码可以正常工作,但不会产生任何结果。事实上,对apply 方法的调用从未被调用(通过在调试模式下添加断点来验证)。我想,前面的主要原因是我的数据没有时间属性。因此,窗口化(通过window 实现)没有正确完成。因此,我的问题是如何表明我希望我的加入基于计数窗口进行。例如,我希望连接从每个流中实现每 100 个元组。前面的在 Flink 中可行吗?如果是,我应该在我的代码中更改什么来实现它。

此时,我必须通知您,我尝试调用countWindow() 方法,但由于某种原因,Flink 的JoinedStreams 没有提供它。

谢谢

【问题讨论】:

    标签: java apache-flink flink-streaming


    【解决方案1】:

    不支持基于计数的联接。您可以通过使用“事件时间”语义来模拟基于计数的窗口,并将唯一的 seq-id 作为时间戳应用于每条记录。因此,“5”的时间窗口实际上是一个 5 的计数窗口。

    【讨论】:

    猜你喜欢
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多