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

常用命令

  • 啓動Zookeeper

 

./zkServer.sh start-foreground

可選參數:

 

./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
  • 創建topic,指定3個分區,每個分區1個副本

 

./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會從初始offset位置開始接收消息;不加該參數從當前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");
        //指定消費者組
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "testgroup");
        //關閉自動位移提交
        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();

    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章