Skip to content

Instantly share code, notes, and snippets.

@tzdanows
Created November 7, 2023 08:14
Show Gist options
  • Save tzdanows/cf06119ade06d9cd7f65672a6234f72f to your computer and use it in GitHub Desktop.
Save tzdanows/cf06119ade06d9cd7f65672a6234f72f to your computer and use it in GitHub Desktop.
kafka consumer and producer files
import org.apache.kafka.clients.producer.*;
import java.util.Properties;
public class SimpleProducer {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("acks", "all");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
for (int i = 0; i < 100; i++) {
producer.send(new ProducerRecord<String, String>("my-topic", Integer.toString(i), "message-" + i));
}
producer.close();
}
}
import org.apache.kafka.clients.producer.*;
import java.util.Properties;
public class SimpleProducer {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("acks", "all");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
for (int i = 0; i < 100; i++) {
producer.send(new ProducerRecord<String, String>("my-topic", Integer.toString(i), "message-" + i));
}
producer.close();
}
}
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>your-kafka-version</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment