The sample scripts in this article demonstrate how to connect to your Aiven for Apache Kafka service and pass a few messages.
Note: The examples given here provide different options for the authentication method. For more information on the supported methods, see our article on Kafka authentication types.
Requirements:
Keystore (
client.keystore.p12
) and truststore (client.truststore.jks
) are properly created. For details, see our article on configuring Java SSL to access Kafka.
To test your Aiven for Apache Kafka service:
Log in to the Aiven web console and select your Kafka service.
If you want to use SASL authentication, turn on kafka_authentication_methods.sasl under Advanced configuration on the Overview page of your service.
Go to the Topics page for your service and add a new topic named
demo-topic
.Create and run the producer script.
Using SSL authentication:
package io.aiven.kafkaExample;
import java.io.IOException;
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
public class Producer {
public static void main(String[] args) throws IOException {
Properties props = new Properties();
props.put("bootstrap.servers", "getting-started-with-kafka.htn-aiven-demo.aivencloud.com:17705");
props.put("security.protocol", "SSL");
props.put("ssl.truststore.location", "client.truststore.jks");
props.put("ssl.truststore.password", "secret");
props.put("ssl.keystore.type", "PKCS12");
props.put("ssl.keystore.location", "client.keystore.p12");
props.put("ssl.keystore.password", "secret");
props.put("ssl.key.password", "secret");
props.put("key.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
producer.send(new ProducerRecord<String, String>("demo-topic", "test-message"));
producer.close();
}
}Using SASL-SSL authentication:
package io.aiven.kafkaExample;
import java.io.IOException;
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
public class Producer {
public static void main(String[] args) throws IOException {
Properties props = new Properties();
String sasl_username = "avnadmin";
String sasl_password = "<avnadmin-pw>";
String jaasTemplate = "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"%s\" password=\"%s\";";
String jaasConfig = String.format(jaasTemplate, sasl_username, sasl_password);
props.put("bootstrap.servers", "getting-started-with-kafka.htn-aiven-demo.aivencloud.com:<SASL_PORT>");
props.put("security.protocol", "SASL_SSL");
props.put("sasl.mechanism", "SCRAM-SHA-256");
props.put("sasl.jaas.config", jaasConfig);
props.put("ssl.endpoint.identification.algorithm", "");
props.put("ssl.truststore.type", "jks");
props.put("ssl.truststore.location", "client.truststore.jks");
props.put("ssl.truststore.password", "secret");
props.put("key.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
producer.send(new ProducerRecord<String, String>("demo-topic", "test-message"));
producer.close();
}
}
Create the consumer script.
Using SSL authentication:
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
public class Consumer {
public static void main(String[] args) throws IOException {
Properties props = new Properties();
props.put("bootstrap.servers", "getting-started-with-kafka.htn-aiven-demo.aivencloud.com:17705");
props.put("security.protocol", "SSL");
props.put("ssl.truststore.location", "client.truststore.jks");
props.put("ssl.truststore.password", "secret");
props.put("ssl.keystore.type", "PKCS12");
props.put("ssl.keystore.location", "client.keystore.p12");
props.put("ssl.keystore.password", "secret");
props.put("ssl.key.password", "secret");
props.put("group.id", "demo-group");
props.put("key.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("demo-topic"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(1000);
for (ConsumerRecord<String, String> record : records) {
System.out.printf("offset = %d, key = %s, value = %s",
record.offset(), record.key(), record.value());
}
}
}
}Using SASL-SSL authentication:
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
public class Consumer {
public static void main(String[] args) throws IOException {
Properties props = new Properties();
String sasl_username = "avnadmin";
String sasl_password = "<avnadmin-pw>";
String jaasTemplate = "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"%s\" password=\"%s\";";
String jaasConfig = String.format(jaasTemplate, sasl_username, sasl_password);
props.put("bootstrap.servers", "getting-started-with-kafka.htn-aiven-demo.aivencloud.com:<SASL_PORT>");
props.put("security.protocol", "SASL_SSL");
props.put("sasl.mechanism", "SCRAM-SHA-256");
props.put("sasl.jaas.config", jaasConfig);
props.put("ssl.truststore.type", "jks");
props.put("ssl.truststore.location", "client.truststore.jks");
props.put("ssl.truststore.password", "secret");
props.put("group.id", "demo-group");
props.put("key.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer",
"org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("demo-topic"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(1000);
for (ConsumerRecord<String, String> record : records) {
System.out.printf("offset = %d, key = %s, value = %s",
record.offset(), record.key(), record.value());
}
}
}
}