Instalasi Apache Kafka

Sebelum membaca artikel ini ada baiknya membaca terlebih dahulu artikel https://enigmacamp.medium.com/mengenal-lebih-dalam-apache-kafka-ec734297e5cc agar EnigmaNians mengetahui dahulu apa itu Apache Kafka.

Lakukan instalasi ini juga ya https://enigmacamp.medium.com/instalasi-java-development-kit-jdk-5a11d8317431

zookeeper.properties
server.properties
broker
./bin/windows/zookeeper-server-start.bat config/zookeeper.properties
./bin/windows/kafka-server-start.bat config/server.properties
bin/zookeeper-server-start.sh config/zookeeper.properties
bin/kafka-server-start.sh config/server.properties
./bin/windows/kafka-topics.bat --create --topic example-topic replication-factor 1 --partitions 1 --bootstrap-server localhost:9092
bin/kafka-topics.sh --create --topic example-topic replication-factor 1 --partitions 1 --bootstrap-server localhost:9092
./bin/windows/kafka-topics.bat --list --topic --bootstrap-server localhost:9092
bin/kafka-topics.sh --list --topic --bootstrap-server localhost:9092
./bin/windows/kafka-console-producer.bat --topic example-topic --bootstrap-server localhost:9092Create your message, example Hello World [enter] dst
bin/kafka-console-producer.sh --topic example-topic --bootstrap-server localhost:9092Create your message, example Hello World [enter] dst
./bin/windows/kafka-console-consumer.bat --topic example-topic --from-beginning --bootstrap-server localhost:9092
bin/kafka-console-consumer.sh --topic example-topic --from-beginning --bootstrap-server localhost:9092
./bin/windows/kafka-topics.bat --bootstrap-server localhost:9092 replication-factor 1 --partitions 3  --create --topic example-topic-2
bin/kafka-topics.sh --bootstrap-server localhost:9092 replication-factor 1 --partitions 3  --create --topic example-topic-2
./bin/windows/kafka-console-consumer.bat --topic example-topic-2 --bootstrap-server localhost:9092 --group consum./bin/windows/kafka-console-consumer.bat --topic example-topic-2  --from-beginning --bootstrap-server localhost:9092 --group consum
bin/kafka-console-consumer.sh --topic example-topic-2 --bootstrap-server localhost:9092 --group consumbin/kafka-console-consumer.sh --topic example-topic-2  --from-beginning --bootstrap-server localhost:9092 --group consum
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.8.0</version>
</dependency>
public class KafkaProducerExample {

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, StringSerializer.class.getName());
properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
KafkaProducer<String, String> producer = new KafkaProducer<String,String>(properties);
for (int i = 0; i < 10; i++) {
ProducerRecord<String, String> record = new ProducerRecord<>("topic-java", "Data ke " + i);
producer.send(record);
}
producer.close();
}
}
public class KafkaConsumerExample {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "test");
props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");
props.setProperty(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "1000");
props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("topic-java"));
//noinspection InfiniteLoopStatement
while
(true) {
ConsumerRecords<String, String> records =
consumer.poll(Duration.ofMillis(100));
for(ConsumerRecord<String, String> record: records) {
System.out.println("Receive data : " + record.value());
}
}
}
}

Enigma IT Bootcamp

--

--

Enigma IT Bootcamp Blog

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store