是否可以在流式 flink 作业中创建批处理 flink 作业?

时间:2020-12-21 13:18:12

标签: scala apache-flink

我有一个使用 Apache Flink(flink 版本:1.8.1)使用 Scala 进行流式传输的作业。有流动工作要求如下: Kafka -> 写入 Hbase -> 使用不同的主题再次发送到 kafka

在写入Hbase的过程中,需要从另一个表中检索数据。为确保数据不为空(NULL),作业必须反复(在一定时间内)检查数据是否为空。

这可以用 Flink 实现吗?如果是,您能否帮助提供与我的需求类似的条件的示例?

编辑: 我的意思是,对于我在内容中描述的问题,我想必须在作业流中创建某种作业批处理,但我找不到适合我的案例的正确示例。那么,是否可以在流式 flink 作业中创建批处理 flink 作业?如果是,您能否帮助提供与我的需求类似的条件的示例?

2 个答案:

答案 0 :(得分:0)

使用更新版本的 Flink,您可以从 SQL/Table API 对 HBase 执行查找查询(使用可配置的缓存)。您的用例听起来很容易以这种方式实现。有关详细信息,请参阅 the docs

答案 1 :(得分:0)

为了澄清我的评论,我将根据 The Broadcast State Pattern 发布我试图建议的内容的草图。该链接提供了一个 Java 示例,因此我将遵循它。如果你想在 Scala 中,它不应该有太大的不同。您可能必须实现以下代码,如我提到的链接中所述:

DataStream<String> output = colorPartitionedStream
                 .connect(ruleBroadcastStream)
                 .process(
                     // type arguments in our KeyedBroadcastProcessFunction represent: 
                     //   1. the key of the keyed stream
                     //   2. the type of elements in the non-broadcast side
                     //   3. the type of elements in the broadcast side
                     //   4. the type of the result, here a string
                     new KeyedBroadcastProcessFunction<Color, Item, Rule, String>() {
                         // my matching logic
                     }
                 );

我建议您可以以固定的时间间隔从数据库或您的商店中收集流 ruleBroadcastStream。而不是得到:

// broadcast the rules and create the broadcast state
BroadcastStream<Rule> ruleBroadcastStream = ruleStream
                        .broadcast(ruleStateDescriptor);

就像网页上说的那样。您需要添加一个源,您可以在其中安排它每 X 分钟运行一次。

StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
BroadcastStream<Rule> ruleBroadcastStream = env
             .addSource(new YourStreamSource())
             .broadcast(ruleStateDescriptor);
 
public class YourStreamSource extends RichSourceFunction<YourType> {
    private volatile boolean running = true;
    @Override
    public void run(SourceContext<YourType> ctx) throws Exception {
        while (running) {
            // TODO: yourData = FETCH DATA;
            ctx.collect(yourData);

            Thread.sleep("sleep for X minutes");
        }
    }
    @Override
    public void cancel() {
        this.running = false;
    }
}
相关问题