Search in sources :

Example 11 with NewTopic

use of org.apache.kafka.clients.admin.NewTopic in project strimzi by strimzi.

the class ControllerIT method createTopic.

private String createTopic(TestContext context, String topicName) throws InterruptedException, ExecutionException {
    LOGGER.info("Creating topic {}", topicName);
    // Create a topic
    String configMapName = new TopicName(topicName).asMapName().toString();
    CreateTopicsResult crt = adminClient.createTopics(singletonList(new NewTopic(topicName, 1, (short) 1)));
    crt.all().get();
    // Wait for the configmap to be created
    waitFor(context, () -> {
        ConfigMap cm = kubeClient.configMaps().inNamespace(NAMESPACE).withName(configMapName).get();
        LOGGER.info("Polled configmap {} waiting for creation", configMapName);
        return cm != null;
    }, timeout, "Expected the configmap to have been created by now");
    LOGGER.info("configmap {} has been created", configMapName);
    return configMapName;
}
Also used : CreateTopicsResult(org.apache.kafka.clients.admin.CreateTopicsResult) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) NewTopic(org.apache.kafka.clients.admin.NewTopic)

Example 12 with NewTopic

use of org.apache.kafka.clients.admin.NewTopic in project samza by apache.

the class TestZkLocalApplicationRunner method setUp.

@Override
public void setUp() {
    super.setUp();
    String uniqueTestId = UUID.randomUUID().toString();
    testStreamAppName = String.format("test-app-name-%s", uniqueTestId);
    testStreamAppId = String.format("test-app-id-%s", uniqueTestId);
    inputKafkaTopic = String.format("test-input-topic-%s", uniqueTestId);
    outputKafkaTopic = String.format("test-output-topic-%s", uniqueTestId);
    inputSinglePartitionKafkaTopic = String.format("test-input-single-partition-topic-%s", uniqueTestId);
    outputSinglePartitionKafkaTopic = String.format("test-output-single-partition-topic-%s", uniqueTestId);
    // Set up stream application config map with the given testStreamAppName, testStreamAppId and test kafka system
    // TODO: processorId should typically come up from a processorID generator as processor.id will be deprecated in 0.14.0+
    Map<String, String> configMap = buildStreamApplicationConfigMap(testStreamAppName, testStreamAppId, false, Optional.empty());
    configMap.put(JobConfig.PROCESSOR_ID, PROCESSOR_IDS[0]);
    applicationConfig1 = new ApplicationConfig(new MapConfig(configMap));
    configMap.put(JobConfig.PROCESSOR_ID, PROCESSOR_IDS[1]);
    applicationConfig2 = new ApplicationConfig(new MapConfig(configMap));
    configMap.put(JobConfig.PROCESSOR_ID, PROCESSOR_IDS[2]);
    applicationConfig3 = new ApplicationConfig(new MapConfig(configMap));
    ZkClient zkClient = new ZkClient(zkConnect(), ZK_CONNECTION_TIMEOUT_MS, ZK_CONNECTION_TIMEOUT_MS, new ZkStringSerializer());
    ZkKeyBuilder zkKeyBuilder = new ZkKeyBuilder(ZkJobCoordinatorFactory.getJobCoordinationZkPath(applicationConfig1));
    zkUtils = new ZkUtils(zkKeyBuilder, zkClient, ZK_CONNECTION_TIMEOUT_MS, ZK_SESSION_TIMEOUT_MS, new NoOpMetricsRegistry());
    zkUtils.connect();
    topicToPartitionCount = ImmutableMap.of(inputSinglePartitionKafkaTopic, 1, outputSinglePartitionKafkaTopic, 1, inputKafkaTopic, NUM_PARTITION, outputKafkaTopic, NUM_PARTITION);
    List<NewTopic> newTopics = ImmutableList.of(inputKafkaTopic, outputKafkaTopic, inputSinglePartitionKafkaTopic, outputSinglePartitionKafkaTopic).stream().map(topic -> new NewTopic(topic, topicToPartitionCount.get(topic), (short) 1)).collect(Collectors.toList());
    assertTrue("Encountered errors during test setup. Failed to create topics.", createTopics(newTopics));
    zkMetadataStore = new ZkMetadataStore(zkUtils.getKeyBuilder().getRootPath(), new MapConfig(configMap), new NoOpMetricsRegistry());
}
Also used : ZkClient(org.I0Itec.zkclient.ZkClient) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) TaskModel(org.apache.samza.job.model.TaskModel) StoreProperties(org.apache.samza.storage.StoreProperties) Map(java.util.Map) StorageEngine(org.apache.samza.storage.StorageEngine) StandaloneTestUtils(org.apache.samza.test.StandaloneTestUtils) MapConfig(org.apache.samza.config.MapConfig) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) StorageConfig(org.apache.samza.config.StorageConfig) ImmutableSet(com.google.common.collect.ImmutableSet) TaskName(org.apache.samza.container.TaskName) ZkStringSerializer(org.apache.samza.zk.ZkStringSerializer) ImmutableMap(com.google.common.collect.ImmutableMap) TaskApplication(org.apache.samza.application.TaskApplication) ZkClient(org.I0Itec.zkclient.ZkClient) Set(java.util.Set) UUID(java.util.UUID) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) NoOpMetricsRegistry(org.apache.samza.util.NoOpMetricsRegistry) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MetadataStoreFactory(org.apache.samza.metadatastore.MetadataStoreFactory) Assert.assertFalse(org.junit.Assert.assertFalse) IntegrationTestHarness(org.apache.samza.test.harness.IntegrationTestHarness) Optional(java.util.Optional) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) ApplicationStatus(org.apache.samza.job.ApplicationStatus) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) JobModelUtil(org.apache.samza.job.model.JobModelUtil) ZkKeyBuilder(org.apache.samza.zk.ZkKeyBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JobConfig(org.apache.samza.config.JobConfig) HashMap(java.util.HashMap) ClusterManagerConfig(org.apache.samza.config.ClusterManagerConfig) Serde(org.apache.samza.serializers.Serde) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MetadataStore(org.apache.samza.metadatastore.MetadataStore) ImmutableList(com.google.common.collect.ImmutableList) MessageCollector(org.apache.samza.task.MessageCollector) ZkUtils(org.apache.samza.zk.ZkUtils) Timeout(org.junit.rules.Timeout) ApplicationConfig(org.apache.samza.config.ApplicationConfig) ApplicationRunners(org.apache.samza.runtime.ApplicationRunners) ExpectedException(org.junit.rules.ExpectedException) JobModel(org.apache.samza.job.model.JobModel) LocalApplicationRunner(org.apache.samza.runtime.LocalApplicationRunner) ZkJobCoordinatorFactory(org.apache.samza.zk.ZkJobCoordinatorFactory) Logger(org.slf4j.Logger) TaskConfig(org.apache.samza.config.TaskConfig) JobContext(org.apache.samza.context.JobContext) ZkMetadataStore(org.apache.samza.zk.ZkMetadataStore) NewTopic(org.apache.kafka.clients.admin.NewTopic) Partition(org.apache.samza.Partition) ContainerContext(org.apache.samza.context.ContainerContext) Assert.assertTrue(org.junit.Assert.assertTrue) CoordinatorStreamValueSerde(org.apache.samza.coordinator.stream.CoordinatorStreamValueSerde) Test(org.junit.Test) ZkConfig(org.apache.samza.config.ZkConfig) Maps(com.google.common.collect.Maps) File(java.io.File) SamzaException(org.apache.samza.SamzaException) TestKafkaEvent(org.apache.samza.test.util.TestKafkaEvent) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TaskMode(org.apache.samza.job.model.TaskMode) MockStorageEngine(org.apache.samza.storage.MockStorageEngine) Rule(org.junit.Rule) ReflectionUtil(org.apache.samza.util.ReflectionUtil) ContainerModel(org.apache.samza.job.model.ContainerModel) StorageEngineFactory(org.apache.samza.storage.StorageEngineFactory) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) ZkUtils(org.apache.samza.zk.ZkUtils) ZkKeyBuilder(org.apache.samza.zk.ZkKeyBuilder) NoOpMetricsRegistry(org.apache.samza.util.NoOpMetricsRegistry) ApplicationConfig(org.apache.samza.config.ApplicationConfig) ZkMetadataStore(org.apache.samza.zk.ZkMetadataStore) NewTopic(org.apache.kafka.clients.admin.NewTopic) MapConfig(org.apache.samza.config.MapConfig) ZkStringSerializer(org.apache.samza.zk.ZkStringSerializer)

Example 13 with NewTopic

use of org.apache.kafka.clients.admin.NewTopic in project samza by apache.

the class TestStartpoint method setUp.

@Override
public void setUp() {
    super.setUp();
    String uniqueTestId = UUID.randomUUID().toString();
    testStreamAppName = String.format("test-app-name-%s", uniqueTestId);
    testStreamAppId = String.format("test-app-id-%s", uniqueTestId);
    inputKafkaTopic1 = String.format("test-input-topic1-%s", uniqueTestId);
    inputKafkaTopic2 = String.format("test-input-topic2-%s", uniqueTestId);
    inputKafkaTopic3 = String.format("test-input-topic3-%s", uniqueTestId);
    inputKafkaTopic4 = String.format("test-input-topic4-%s", uniqueTestId);
    outputKafkaTopic = String.format("test-output-topic-%s", uniqueTestId);
    // Set up stream application config map with the given testStreamAppName, testStreamAppId and test kafka system
    // TODO: processorId should typically come up from a processorID generator as processor.id will be deprecated in 0.14.0+
    Map<String, String> configMap = buildStreamApplicationConfigMap(testStreamAppName, testStreamAppId);
    configMap.put(JobConfig.PROCESSOR_ID, PROCESSOR_IDS[0]);
    applicationConfig1 = new ApplicationConfig(new MapConfig(configMap));
    configMap.put(JobConfig.PROCESSOR_ID, PROCESSOR_IDS[1]);
    applicationConfig2 = new ApplicationConfig(new MapConfig(configMap));
    configMap.put(JobConfig.PROCESSOR_ID, PROCESSOR_IDS[2]);
    applicationConfig3 = new ApplicationConfig(new MapConfig(configMap));
    configMap.put(JobConfig.PROCESSOR_ID, PROCESSOR_IDS[3]);
    applicationConfig4 = new ApplicationConfig(new MapConfig(configMap));
    ImmutableMap<String, Integer> topicToPartitionCount = ImmutableMap.<String, Integer>builder().put(inputKafkaTopic1, ZK_TEST_PARTITION_COUNT).put(inputKafkaTopic2, ZK_TEST_PARTITION_COUNT).put(inputKafkaTopic3, ZK_TEST_PARTITION_COUNT).put(inputKafkaTopic4, ZK_TEST_PARTITION_COUNT).put(outputKafkaTopic, ZK_TEST_PARTITION_COUNT).build();
    List<NewTopic> newTopics = topicToPartitionCount.keySet().stream().map(topic -> new NewTopic(topic, topicToPartitionCount.get(topic), (short) 1)).collect(Collectors.toList());
    assertTrue("Encountered errors during test setup. Failed to create topics.", createTopics(newTopics));
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) StartpointTimestamp(org.apache.samza.startpoint.StartpointTimestamp) Startpoint(org.apache.samza.startpoint.Startpoint) Future(java.util.concurrent.Future) StartpointSpecific(org.apache.samza.startpoint.StartpointSpecific) TestTaskApplication(org.apache.samza.test.processor.TestTaskApplication) Duration(java.time.Duration) Map(java.util.Map) StandaloneTestUtils(org.apache.samza.test.StandaloneTestUtils) StartpointManager(org.apache.samza.startpoint.StartpointManager) MapConfig(org.apache.samza.config.MapConfig) TaskCallback(org.apache.samza.task.TaskCallback) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) NoOpMetricsRegistry(org.apache.samza.util.NoOpMetricsRegistry) Collectors(java.util.stream.Collectors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) IntegrationTestHarness(org.apache.samza.test.harness.IntegrationTestHarness) SharedContextFactories(org.apache.samza.test.processor.SharedContextFactories) StartpointUpcoming(org.apache.samza.startpoint.StartpointUpcoming) Optional(java.util.Optional) JobCoordinatorConfig(org.apache.samza.config.JobCoordinatorConfig) Config(org.apache.samza.config.Config) CoordinatorStreamUtil(org.apache.samza.util.CoordinatorStreamUtil) SystemAdmins(org.apache.samza.system.SystemAdmins) StartpointOldest(org.apache.samza.startpoint.StartpointOldest) CoordinatorStreamStore(org.apache.samza.coordinator.metadatastore.CoordinatorStreamStore) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) JobConfig(org.apache.samza.config.JobConfig) HashMap(java.util.HashMap) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) ImmutableList(com.google.common.collect.ImmutableList) SystemStream(org.apache.samza.system.SystemStream) Timeout(org.junit.rules.Timeout) ApplicationConfig(org.apache.samza.config.ApplicationConfig) ApplicationRunners(org.apache.samza.runtime.ApplicationRunners) ExpectedException(org.junit.rules.ExpectedException) Logger(org.slf4j.Logger) TaskConfig(org.apache.samza.config.TaskConfig) NewTopic(org.apache.kafka.clients.admin.NewTopic) Partition(org.apache.samza.Partition) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ZkConfig(org.apache.samza.config.ZkConfig) Maps(com.google.common.collect.Maps) SamzaException(org.apache.samza.SamzaException) TestKafkaEvent(org.apache.samza.test.util.TestKafkaEvent) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) SystemAdmin(org.apache.samza.system.SystemAdmin) Assert.assertEquals(org.junit.Assert.assertEquals) ApplicationConfig(org.apache.samza.config.ApplicationConfig) NewTopic(org.apache.kafka.clients.admin.NewTopic) MapConfig(org.apache.samza.config.MapConfig)

Example 14 with NewTopic

use of org.apache.kafka.clients.admin.NewTopic in project flink by apache.

the class KafkaSourceExternalContext method createSinglePartitionTopic.

private KafkaPartitionDataWriter createSinglePartitionTopic(int topicIndex) throws Exception {
    String newTopicName = topicName + "-" + topicIndex;
    LOG.info("Creating topic '{}'", newTopicName);
    adminClient.createTopics(Collections.singletonList(new NewTopic(newTopicName, 1, (short) 1))).all().get();
    return new KafkaPartitionDataWriter(getKafkaProducerProperties(topicIndex), new TopicPartition(newTopicName, 0));
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) NewTopic(org.apache.kafka.clients.admin.NewTopic)

Example 15 with NewTopic

use of org.apache.kafka.clients.admin.NewTopic in project flink by apache.

the class KafkaSourceReaderTest method setup.

@BeforeAll
public static void setup() throws Throwable {
    KafkaSourceTestEnv.setup();
    try (AdminClient adminClient = KafkaSourceTestEnv.getAdminClient()) {
        adminClient.createTopics(Collections.singleton(new NewTopic(TOPIC, NUM_PARTITIONS, (short) 1)));
        // Use the admin client to trigger the creation of internal __consumer_offsets topic.
        // This makes sure that we won't see unavailable coordinator in the tests.
        waitUtil(() -> {
            try {
                adminClient.listConsumerGroupOffsets("AnyGroup").partitionsToOffsetAndMetadata().get();
            } catch (Exception e) {
                return false;
            }
            return true;
        }, Duration.ofSeconds(60), "Waiting for offsets topic creation failed.");
    }
    KafkaSourceTestEnv.produceToKafka(getRecords(), StringSerializer.class, IntegerSerializer.class);
}
Also used : NewTopic(org.apache.kafka.clients.admin.NewTopic) AdminClient(org.apache.kafka.clients.admin.AdminClient) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

NewTopic (org.apache.kafka.clients.admin.NewTopic)115 Test (org.junit.Test)59 HashMap (java.util.HashMap)34 AdminClient (org.apache.kafka.clients.admin.AdminClient)26 Cluster (org.apache.kafka.common.Cluster)24 ExecutionException (java.util.concurrent.ExecutionException)21 MockAdminClient (org.apache.kafka.clients.admin.MockAdminClient)19 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)19 MockTime (org.apache.kafka.common.utils.MockTime)16 AdminClientUnitTestEnv (org.apache.kafka.clients.admin.AdminClientUnitTestEnv)15 Config (org.apache.kafka.clients.admin.Config)15 Map (java.util.Map)14 TopicConfig (org.apache.kafka.common.config.TopicConfig)13 StreamsConfig (org.apache.kafka.streams.StreamsConfig)12 ArrayList (java.util.ArrayList)11 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)11 TimeoutException (org.apache.kafka.common.errors.TimeoutException)11 TopicMetadataAndConfig (org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig)10 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)10 TopicPartitionInfo (org.apache.kafka.common.TopicPartitionInfo)10