Kafka入門系列—6. Kafka 常用命令及Java API使用

常用命令

  • 啟動Zookeeper
./zkServer.sh start-foreground

可選參數(shù):

./zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
  • 啟動ZooInspector,可以查看注冊到Zookeeper的Kafka broker和topic情況:
java -jar zookeeper-dev-ZooInspector.jar
屏幕快照 2018-12-06 上午10.56.40.png
  • 啟動Kafka
./kafka-server-start.sh ../config/server.properties
  • 創(chuàng)建topic,指定3個(gè)分區(qū),每個(gè)分區(qū)1個(gè)副本
./kafka-topics.sh --create -topic testtopic -partitions 3 -replication-factor 1  -zookeeper localhost:2181
  • 列出所有topic
./kafka-topics.sh --list -zookeeper localhost:2181
  • 刪除topic
./kafka-topics.sh --delete -topic testtopic -zookeeper localhost:2181
  • 使用producer命令行工具
./kafka-console-producer.sh -topic testtopic  --broker-list localhost:9092
  • 使用consumer命令行工具

注意:--from-beginning會從初始o(jì)ffset位置開始接收消息;不加該參數(shù)從當(dāng)前offset位置開始。

./kafka-console-consumer.sh --bootstrap-server localhost:9092 -topic testtopic --from-beginning

Java API使用

  • Producer API
public class SampleProducer {

    private static final Logger LOGGER = LoggerFactory.getLogger(SampleProducer.class);

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("bootstrap.servers", "localhost:9092");
        properties.put("client.id", "DemoProducer");
        //序列化器
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);
        ProducerRecord<String, String> record = new ProducerRecord<>("testtopic", "hello world");

        Future<RecordMetadata> future = producer.send(record);
        RecordMetadata recordMetadata = null;
        try {
            recordMetadata = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(recordMetadata);

    }
}
  • Consumer API
public class SampleConsumer {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        //指定消費(fèi)者組
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "testgroup");
        //關(guān)閉自動位移提交
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
        props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "30000");
        //反序列化器
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");

        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
        //訂閱topic
        consumer.subscribe(Arrays.asList("testtopic"));

        ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(2));
        records.forEach((ConsumerRecord<String, String> record) -> {
            System.out.println(record.value());
        });

        //手動提交位移
        consumer.commitAsync();

    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容