Skip to content
Snippets Groups Projects
Commit 55c9af5c authored by Seok Won's avatar Seok Won
Browse files

Kafka Producer

Send "Hello World!" string to topic named "first-topic".

Producer sends a data to Consumer asynchronous, so we have to flush to see results right away.

Execute below command to see "Hello World!"

>> kafka-console-consumer --bootstrap-server localhost:9092 --topic first-topic --group group-one
parent 4dff66d5
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />
\ No newline at end of file
pom.xml 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>csw.kafka</groupId>
<artifactId>kafkaStudies</artifactId>
<version>1.0</version>
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
<!-- <scope>test</scope>-->
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.3</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package csw.kafka.study.lesson1;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import java.util.Properties;
public class ProducerDemo {
public static void main(String[] args) {
String myServer = "localhost:9092";
// Kafka Producer 설정
Properties properties = new Properties();
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, myServer);
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
// Kafka는 데이터를 바이트로 보냄 (0,1)
// Kafka Producer Record
ProducerRecord<String, String> record = new ProducerRecord<String, String>("first-topic", "Hello World!");
// Kafka Producer 만들기
KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);
// Consumer한테 데이터 보내기 - 비동기
producer.send(record);
producer.flush();
producer.close();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment