首頁  >  文章  >  系統教程  >  在Linux中快速安裝Kafka並進行入門:詳細步驟指南

在Linux中快速安裝Kafka並進行入門:詳細步驟指南

WBOY
WBOY原創
2024-01-31 21:26:19667瀏覽

Linux環境下安裝Kafka的詳細步驟

1. 前提條件

  • 作業系統:Linux(建議使用Ubuntu或CentOS)
  • Java:JDK 8或更高版本
  • ZooKeeper:版本3.4或更高版本
  • Kafka:最新穩定版本

#2. 安裝Java

sudo apt-get update
sudo apt-get install default-jdk

3. 安裝ZooKeeper

wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
tar -xvf zookeeper-3.4.14.tar.gz
cd zookeeper-3.4.14
./configure
make
sudo make install

4. 設定ZooKeeper

sudo mkdir /var/lib/zookeeper
sudo chown zookeeper:zookeeper /var/lib/zookeeper

編輯/etc/zookeeper/conf/zoo.cfg文件,並新增以下內容:

dataDir=/var/lib/zookeeper
clientPort=2181

啟動ZooKeeper:

sudo service zookeeper start

#5. 安裝Kafka

wget https://archive.apache.org/dist/kafka/2.8.0/kafka_2.13-2.8.0.tgz
tar -xvf kafka_2.13-2.8.0.tgz
cd kafka_2.13-2.8.0

6. 設定Kafka

編輯/etc/kafka/server.properties文件,並新增以下內容:

broker.id=0
listeners=PLAINTEXT://:9092
zookeeper.connect=localhost:2181

啟動Kafka:

./bin/kafka-server-start.sh config/server.properties

7. 建立主題

./bin/kafka-topics.sh --create --topic test --partitions 1 --replication-factor 1

8. 生產資料

./bin/kafka-console-producer.sh --topic test

9. 消費資料

./bin/kafka-console-consumer.sh --topic test --from-beginning

快速入門指南

#1. 建立一個Producer

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

public class SimpleProducer {

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");

        KafkaProducer<String, String> producer = new KafkaProducer<>(properties);

        ProducerRecord<String, String> record = new ProducerRecord<>("test", "Hello, Kafka!");

        producer.send(record);

        producer.close();
    }
}

#2. 建立一個Consumer

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Arrays;
import java.util.Properties;

public class SimpleConsumer {

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "test-group");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");

        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);

        consumer.subscribe(Arrays.asList("test"));

        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(100);

            for (ConsumerRecord<String, String> record : records) {
                System.out.println(record.key() + ": " + record.value());
            }
        }

        consumer.close();
    }
}

以上是在Linux中快速安裝Kafka並進行入門:詳細步驟指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn