Home > System Tutorial > LINUX > body text

Quickly install and get started with Kafka in Linux: a step-by-step guide

WBOY
Release: 2024-01-31 21:26:19
Original
640 people have browsed it

Detailed steps for installing Kafka in Linux environment

1. Prerequisites

  • Operating system: Linux (Ubuntu is recommended or CentOS)
  • Java: JDK 8 or higher
  • ZooKeeper: version 3.4 or higher
  • Kafka: latest stable version

2. Install Java

sudo apt-get update
sudo apt-get install default-jdk
Copy after login

3. Install 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
Copy after login

4. Configure ZooKeeper

sudo mkdir /var/lib/zookeeper
sudo chown zookeeper:zookeeper /var/lib/zookeeper
Copy after login

Edit/etc/zookeeper/conf/zoo.cfg file and add the following content:

dataDir=/var/lib/zookeeper
clientPort=2181
Copy after login

Start ZooKeeper:

sudo service zookeeper start
Copy after login

5. Install 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
Copy after login

6. Configure Kafka

Edit the /etc/kafka/server.properties file and add the following content:

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

Start Kafka:

./bin/kafka-server-start.sh config/server.properties
Copy after login

7. Create topic

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

8. Produce data

./bin/kafka-console-producer.sh --topic test
Copy after login

9. Consumption data

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

Quick Start Guide

1. Create a 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();
    }
}
Copy after login

2. Create a 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();
    }
}
Copy after login

The above is the detailed content of Quickly install and get started with Kafka in Linux: a step-by-step guide. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!