【问题标题】:Update TTL for a particular topic in kafka using Java使用 Java 更新 kafka 中特定主题的 TTL
【发布时间】:2017-12-25 15:14:10
【问题描述】:

为主题更新 TTL,以便记录在主题中保留 10 天。我必须通过让所有其他主题 TTL 保持相同的当前配置来为特定主题执行此操作,我必须使用 java 执行此操作,因为我通过 Java 将主题推送到 kafka。我正在设置以下属性以将主题推送到kafka

Properties props = new Properties();
props.put("bootstrap.servers", KAFKA_SERVERS);
props.put("acks", ACKS);
props.put("retries", RETRIES);
props.put("linger.ms", new Integer(LINGER_MS));
props.put("buffer.memory", new Integer(BUFFER_MEMORY));
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

【问题讨论】:

    标签: java apache-kafka kafka-producer-api


    【解决方案1】:

    您可以使用AdminClient 执行此操作,然后执行获取当前配置的代码(仅用于测试),然后在名为“test”的主题上更新“retention.ms”配置。

    Properties props = new Properties();
    props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    
    AdminClient adminClient = AdminClient.create(props);
    
    ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, "test");
    
    // get the current topic configuration
    DescribeConfigsResult describeConfigsResult  =
            adminClient.describeConfigs(Collections.singleton(resource));
    
    Map<ConfigResource, Config> config = describeConfigsResult.all().get();
    
    System.out.println(config);
    
    // create a new entry for updating the retention.ms value on the same topic
    ConfigEntry retentionEntry = new ConfigEntry(TopicConfig.RETENTION_MS_CONFIG, "50000");
    Map<ConfigResource, Config> updateConfig = new HashMap<ConfigResource, Config>();
    updateConfig.put(resource, new Config(Collections.singleton(retentionEntry)));
    
    AlterConfigsResult alterConfigsResult = adminClient.alterConfigs(updateConfig);
    alterConfigsResult.all();
    
    describeConfigsResult  = adminClient.describeConfigs(Collections.singleton(resource));
    
    config = describeConfigsResult.all().get();
    
    System.out.println(config);
    
    adminClient.close();
    

    【讨论】:

    • 感谢您的回复,无法获取这些类信息(AdminClientDescribeConfigsResultAlterConfigsResultConfigEntry),我正在使用pom 没有得到导入语句
    • 您使用的是 0.11.0 版本吗?我正在使用这个。 AdminClient API 是在最新版本中引入的,否则您无法从 Java 代码中执行此操作。
    • &lt;dependency&gt; &lt;groupId&gt;org.apache.kafka&lt;/groupId&gt; &lt;artifactId&gt;kafka_2.10&lt;/artifactId&gt; &lt;version&gt;0.8.0&lt;/version&gt; &lt;/dependency&gt; 我也试过 0.11.0 但对我没用
    • 我已经将我的代码上传到了这个 github 仓库:github.com/ppatierno/kafka-playground
    • 属性是retention.ms ...它以毫秒为单位
    猜你喜欢
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2017-04-04
    相关资源
    最近更新 更多