JAVA-6390: Move kafka articles from libraries-data-3 to new module
apache-kafka
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package com.baeldung.kafka.admin;
|
||||
|
||||
import org.apache.kafka.clients.admin.Admin;
|
||||
import org.apache.kafka.clients.admin.AdminClientConfig;
|
||||
import org.apache.kafka.clients.admin.CreateTopicsOptions;
|
||||
import org.apache.kafka.clients.admin.CreateTopicsResult;
|
||||
import org.apache.kafka.clients.admin.NewTopic;
|
||||
import org.apache.kafka.common.KafkaFuture;
|
||||
import org.apache.kafka.common.config.TopicConfig;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
public class KafkaTopicApplication {
|
||||
|
||||
private final Properties properties;
|
||||
|
||||
public KafkaTopicApplication(Properties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public void createTopic(String topicName) throws Exception {
|
||||
try (Admin admin = Admin.create(properties)) {
|
||||
int partitions = 1;
|
||||
short replicationFactor = 1;
|
||||
NewTopic newTopic = new NewTopic(topicName, partitions, replicationFactor);
|
||||
|
||||
CreateTopicsResult result = admin.createTopics(Collections.singleton(newTopic));
|
||||
|
||||
// get the async result for the new topic creation
|
||||
KafkaFuture<Void> future = result.values()
|
||||
.get(topicName);
|
||||
|
||||
// call get() to block until topic creation has completed or failed
|
||||
future.get();
|
||||
}
|
||||
}
|
||||
|
||||
public void createTopicWithOptions(String topicName) throws Exception {
|
||||
Properties props = new Properties();
|
||||
props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
|
||||
|
||||
try (Admin admin = Admin.create(props)) {
|
||||
int partitions = 1;
|
||||
short replicationFactor = 1;
|
||||
NewTopic newTopic = new NewTopic(topicName, partitions, replicationFactor);
|
||||
|
||||
CreateTopicsOptions topicOptions = new CreateTopicsOptions().validateOnly(true)
|
||||
.retryOnQuotaViolation(true);
|
||||
|
||||
CreateTopicsResult result = admin.createTopics(Collections.singleton(newTopic), topicOptions);
|
||||
|
||||
KafkaFuture<Void> future = result.values()
|
||||
.get(topicName);
|
||||
future.get();
|
||||
}
|
||||
}
|
||||
|
||||
public void createCompactedTopicWithCompression(String topicName) throws Exception {
|
||||
Properties props = new Properties();
|
||||
props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
|
||||
|
||||
try (Admin admin = Admin.create(props)) {
|
||||
int partitions = 1;
|
||||
short replicationFactor = 1;
|
||||
|
||||
// Create a compacted topic with 'lz4' compression codec
|
||||
Map<String, String> newTopicConfig = new HashMap<>();
|
||||
newTopicConfig.put(TopicConfig.CLEANUP_POLICY_CONFIG, TopicConfig.CLEANUP_POLICY_COMPACT);
|
||||
newTopicConfig.put(TopicConfig.COMPRESSION_TYPE_CONFIG, "lz4");
|
||||
NewTopic newTopic = new NewTopic(topicName, partitions, replicationFactor).configs(newTopicConfig);
|
||||
|
||||
CreateTopicsResult result = admin.createTopics(Collections.singleton(newTopic));
|
||||
|
||||
KafkaFuture<Void> future = result.values()
|
||||
.get(topicName);
|
||||
future.get();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.kafka.admin;
|
||||
|
||||
import org.apache.kafka.clients.admin.AdminClientConfig;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.KafkaContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@Testcontainers
|
||||
class KafkaTopicApplicationIntegrationTest {
|
||||
|
||||
@Container
|
||||
private static final KafkaContainer KAFKA_CONTAINER = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.4.3"));
|
||||
|
||||
private KafkaTopicApplication kafkaTopicApplication;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
Properties properties = new Properties();
|
||||
properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_CONTAINER.getBootstrapServers());
|
||||
kafkaTopicApplication = new KafkaTopicApplication(properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTopicName_whenCreateNewTopic_thenTopicIsCreated() throws Exception {
|
||||
kafkaTopicApplication.createTopic("test-topic");
|
||||
|
||||
String topicCommand = "/usr/bin/kafka-topics --bootstrap-server=localhost:9092 --list";
|
||||
String stdout = KAFKA_CONTAINER.execInContainer("/bin/sh", "-c", topicCommand)
|
||||
.getStdout();
|
||||
|
||||
assertThat(stdout).contains("test-topic");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.baeldung.kafkastreams;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.kafka.clients.consumer.ConsumerConfig;
|
||||
import org.apache.kafka.common.serialization.Serdes;
|
||||
import org.apache.kafka.streams.KafkaStreams;
|
||||
import org.apache.kafka.streams.StreamsBuilder;
|
||||
import org.apache.kafka.streams.StreamsConfig;
|
||||
import org.apache.kafka.streams.Topology;
|
||||
import org.apache.kafka.streams.kstream.KStream;
|
||||
import org.apache.kafka.streams.kstream.KTable;
|
||||
import org.apache.kafka.streams.kstream.Produced;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class KafkaStreamsLiveTest {
|
||||
private String bootstrapServers = "localhost:9092";
|
||||
|
||||
@Test
|
||||
@Ignore("it needs to have kafka broker running on local")
|
||||
public void shouldTestKafkaStreams() throws InterruptedException {
|
||||
//given
|
||||
String inputTopic = "inputTopic";
|
||||
|
||||
Properties streamsConfiguration = new Properties();
|
||||
streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, "wordcount-live-test");
|
||||
streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
|
||||
streamsConfiguration.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
|
||||
streamsConfiguration.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
|
||||
streamsConfiguration.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 1000);
|
||||
streamsConfiguration.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
|
||||
// Use a temporary directory for storing state, which will be automatically removed after the test.
|
||||
// streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getAbsolutePath());
|
||||
|
||||
/*
|
||||
* final StreamsBuilder builder = new StreamsBuilder();
|
||||
KStream<String, String> textLines = builder.stream(wordCountTopic,
|
||||
Consumed.with(Serdes.String(), Serdes.String()));
|
||||
|
||||
KTable<String, Long> wordCounts = textLines
|
||||
.flatMapValues(value -> Arrays.asList(value.toLowerCase(Locale.ROOT)
|
||||
.split("\\W+")))
|
||||
.groupBy((key, word) -> word)
|
||||
.count(Materialized.<String, Long, KeyValueStore<Bytes, byte[]>> as("counts-store"));
|
||||
*/
|
||||
//when
|
||||
final StreamsBuilder builder = new StreamsBuilder();
|
||||
KStream<String, String> textLines = builder.stream(inputTopic);
|
||||
Pattern pattern = Pattern.compile("\\W+", Pattern.UNICODE_CHARACTER_CLASS);
|
||||
|
||||
KTable<String, Long> wordCounts = textLines
|
||||
.flatMapValues(value -> Arrays.asList(pattern.split(value.toLowerCase())))
|
||||
.groupBy((key, word) -> word)
|
||||
.count();
|
||||
|
||||
wordCounts.toStream().foreach((word, count) -> System.out.println("word: " + word + " -> " + count));
|
||||
|
||||
String outputTopic = "outputTopic";
|
||||
//final Serde<String> stringSerde = Serdes.String();
|
||||
//final Serde<Long> longSerde = Serdes.Long();
|
||||
//wordCounts.toStream().to(stringSerde, longSerde, outputTopic);
|
||||
|
||||
wordCounts.toStream().to("outputTopic",
|
||||
Produced.with(Serdes.String(), Serdes.Long()));
|
||||
|
||||
final Topology topology = builder.build();
|
||||
KafkaStreams streams = new KafkaStreams(topology, streamsConfiguration);
|
||||
streams.start();
|
||||
|
||||
//then
|
||||
Thread.sleep(30000);
|
||||
streams.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user