kafka学习总结007 --- 生产者Java API实例
2021-05-11 02:28
标签:end 查看 load rap exce final lazy ring comm 事先说明,本文的所有实例均基于kafka2.5.0开发;依赖的jar包 创建topic的方法 执行后创建topic成功: kafka生产消息有同步和异步两种方式: 1. 创建生产者的方法 2. 同步生产消息 3. 异步生产消息 相关命令: 查看topic详细信息: 查看kafka log数据: 查看某个消费组消费情况: kafka学习总结007 --- 生产者Java API实例 标签:end 查看 load rap exce final lazy ring comm 原文地址:https://www.cnblogs.com/sniffs/p/13149527.html
private static final String BOOTSTRAP_SERVER = "192.168.1.8:9091,192.168.1.8:9092,192.168.1.8:9093";
public static void createTopic(String topicName) {
Properties properties = new Properties();
properties.setProperty(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVER);
AdminClient adminClient = KafkaAdminClient.create(properties);
NewTopic newTopic = new NewTopic(topicName,2, (short) 3);
adminClient.createTopics(Collections.singletonList(newTopic));
adminClient.close();
}
public static KafkaProducer
public class MySyncProducer {
public static void main(String[] args) throws ExecutionException, InterruptedException {
KafkaProducer
ProducerRecord
RecordMetadata record = producer.send(producerRecord).get();
System.out.println("Producer msg: partition=" + record.partition() + ", offset=" + record.offset());
producer.close();
}
}public class MyAsyncProducer {
public static void main(String[] args) {
KafkaProducer
bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --topic java-api-0617-topic --describe
bin/kafka-run-class.sh kafka.tools.DumpLogSegments -print-data-log -files kafka-logs/java-api-0617-topic-0/00000000000000000000.log
bin/kafka-consumer-groups.sh --bootstrap-server 192.168.1.8:9091,192.168.1.8:9092,192.168.1.8:9093 --group group1 --describe
文章标题:kafka学习总结007 --- 生产者Java API实例
文章链接:http://soscw.com/essay/84193.html