Kafka Java Flashcards
Configure the security protocol
props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, “SSL”);
Configure the path to the truststore file
props.put( SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, );
Configure the truststore password
props.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, “password”);
Configure bootstrap server
props.put(
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
“YOUR_KAFKA_BOOTSTRAP_HOST:YOUR_KAFKA_BOOTSTRAP_PORT”
);
set the consumer group ID
props.put(
ConsumerConfig.GROUP_ID_CONFIG,
“reportingSystem”
);
set the key deserializer
props.put(
ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
“org.apache.kafka.common.serialization.StringDeserializer”
);
set the value deserializer
props.put(
ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
“org.apache.kafka.common.serialization.IntegerDeserializer”
);
set the offset reset config
props.put(
ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,
“earliest”
);
Initialize Kafka consumer
Consumer consumer = new KafkaConsumer<>(configureProperties());
Consume events from kafka consumer
while (true) {
ConsumerRecords records = consumer.poll(Duration.ofSeconds(10));
for (ConsumerRecord record : records) { printRecord(record);
}
Subscribe consumer on topi
consumer.subscribe(Collections.singletonList(“wind-turbine-production”));
Initialize Kafka producer
Producer producer = new KafkaProducer<>(
configureProperties()
);
Final producer instruction
producer.close();
List consumer config properties
1) Bootstrap server
2) key deserializer
3) value deserializer
4) ssl location
5) security protocol
6) ssl password
7) group id
Steps for consuming events
1) Build config
2) Setup kafka consumer client
3) Subscribe to topics
4) Reapreatedly poll.