首页  >  文章  >  系统教程  >  在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文件,并添加以下内容:/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

启动ZooKeeper:

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

5. 安装Kafka
./bin/kafka-topics.sh --create --topic test --partitions 1 --replication-factor 1

6. 配置Kafka

编辑/etc/kafka/server.properties文件,并添加以下内容:

./bin/kafka-console-producer.sh --topic test
启动Kafka:
./bin/kafka-console-consumer.sh --topic test --from-beginning

7. 创建主题
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();
    }
}

8. 生产数据
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();
    }
}

9. 消费数据rrreee

🎜快速入门指南🎜🎜🎜🎜1. 创建一个Producer🎜🎜rrreee🎜🎜2. 创建一个Consumer🎜🎜rrreee

以上是在Linux中快速安装Kafka并进行入门:详细步骤指南的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn