重置Kafka Stream应用程序

时间:2019-10-16 17:52:47

标签: apache-kafka-streams

试图了解如何将重置工具用于kafka流应用程序。但是我对解释感到困惑。

据说有两个阶段,本地休息(清除),然后是使用重置工具的全局重置。

下面的示例提供了合辑:

https://docs.confluent.io/current/streams/developer-guide/app-reset-tool.html#step-2-reset-the-local-environments-of-your-application-instances


    package io.confluent.examples.streams;

import ...;

public class ResetDemo {

  public static void main(String[] args) throws Exception {
    // Kafka Streams configuration
    Properties props = new Properties();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG, "my-streams-app");
    // ...and so on...

    // Define the processing topology
    StreamsBuilder builder = new StreamsBuilder();
    builder.stream("my-input-topic")
        .selectKey(...)
        .through("rekeyed-topic")
        .groupByKey()
        .count("global-count")
        .to("my-output-topic");

    KafkaStreams app = new KafkaStreams(builder.build(), props);

    // Delete the application's local state.
    // Note: In real application you'd call `cleanUp()` only under
    // certain conditions.  See tip on `cleanUp()` below.
    app.cleanUp();

    app.start();

    // Note: In real applications you would register a shutdown hook
    // that would trigger the call to `app.close()` rather than
    // using the sleep-then-close example we show here.
    Thread.sleep(30 * 1000L);
    app.close();
  }

}

下面的示例:

提示:为避免产生相应的恢复开销,您不应无条件地调用cleanUp(),并且每次重新启动或恢复应用程序实例时都应如此。例如,在生产应用程序中,您可以根据需要使用命令行参数来启用或禁用cleanUp()调用。

然后您可以按如下所示执行运行-重置-修改周期:

    # Run your application
  bin/kafka-run-class io.confluent.examples.streams.ResetDemo

# After stopping all application instances, reset the application
  bin/kafka-streams-application-reset --application-id my-streams-app \
                                      --input-topics my-input-topic \
                                      --intermediate-topics rekeyed-topic

# Now you can modify/recompile as needed and then re-run the application again.
# You can also experiment, for example, with different input data without
# modifying the application.

我不明白。

  1. app.cleanUp()是否需要执行app.start()?
  2. 为什么在随后的情况下使用app.cleanUp()和app.start()会有用?
  3. 为什么不仅仅带有一个标志,说明它是否为cleanUp,然后才运行app.cleanUp(),而不是运行app.start();

我只是不理解示例的最后一部分

运行您的应用,然后应用重置工具。但是如果该应用程序与示例中的外观相似,则在清理之后,该应用程序将运行,并且我们将拥有本地数据。因此停止将无助于其他工具。

这非常令人困惑。可以理解的人可以解释一下吗?

1 个答案:

答案 0 :(得分:1)

运行应用程序重置工具,以确保重置应用程序的状态(在应用程序已配置的Kafka群集中进行全局跟踪)。但是,根据设计,重设工具不会修改或重设应用程序实例的本地环境,该环境包括应用程序的本地状态目录。 要完成应用程序重置,您还必须在运行了应用程序实例的任何计算机上删除应用程序的本地状态目录,然后再在同一台计算机上重新启动应用程序实例。您可以在应用程序代码中使用API​​方法KafkaStreams#cleanUp(),也可以手动删除相应的本地状态目录(state.dir配置参数)。

app.cleanup()方法删除本地状态存储。 app.startUp()启动KStream应用程序。

  
      
  1. app.cleanUp()是否需要执行app.start()?
  2.   

app.cleanup()方法删除本地状态存储。 app.startUp()是启动KStream应用程序所必需的。如果您只想清理商店,则只能致电cleanUp()。但是,如果要启动该应用程序,则也需要调用startUp()方法。

  
      
  1. 为什么在随后的情况下使用app.cleanUp()和app.start()会有用?
  2.   

当您想将应用程序重置为最早的偏移量并从头开始运行它时,您需要调用cleanUp()方法。如果要从当前状态恢复已停止的应用程序,请不要在应用程序代码中调用cleanUp()。 除非需要,否则不建议将默认清除用作应用程序的一部分。另外,您无法删除正在运行的应用程序中的商店,因此请始终在cleanUp()方法之前调用startUp()

  
      
  1. 为什么不仅仅带有一个标明是否为cleanUp的标志,而是直接运行app.cleanUp()而不运行app.start();
  2.   

是的,您可以根据用例进行操作。在配置文件中定义一个标志,并添加条件清理。示例:

if(cleanUpFlag){
   app.cleanUp();
}
app.start();

如果要自动化整个过程以进行全局和局部清理,则可以编写一个脚本,该脚本将配置文件作为输入。您可以将该配置文件中的标志值flag=true更新为true。还要确保在if条件中使用config变量。

if(properties.getProperty("cleanUp.flag") == true){
       app.cleanUp();
    }

示例:

# After stopping all application instances, reset the application
$ bin/kafka-streams-application-reset --application-id my-streams-app \
                                      --input-topics my-input-topic \
                                      --intermediate-topics rekeyed-topic \
                                       --bootstrap-servers brokerHost:9092 \
                                      --zookeeper zookeeperHost:2181


# Run your application
$ java –DApp.config.file=app.properties –jar KStreamDemo.jar