org.apache.kafka.clients.admin.AdminClient Java Examples

The following examples show how to use org.apache.kafka.clients.admin.AdminClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KafkaTestContainerManager.java    From apicurio-registry with Apache License 2.0 7 votes vote down vote up
private void createTopics(String bootstrapServers) {
    Properties properties = new Properties();
    properties.put("bootstrap.servers", bootstrapServers);
    properties.put("connections.max.idle.ms", 10000);
    properties.put("request.timeout.ms", 5000);
    try (AdminClient client = AdminClient.create(properties)) {
        CreateTopicsResult result = client.createTopics(Arrays.asList(
                new NewTopic("storage-topic", 1, (short) 1),
                new NewTopic("global-id-topic", 1, (short) 1),
                new NewTopic("snapshot-topic", 1, (short) 1)
        ));
        try {
            result.all().get();
        } catch ( InterruptedException | ExecutionException e ) {
            throw new IllegalStateException(e);
        }
    }
}
 
Example #2
Source File: TestKafkaUtils.java    From DataLink with Apache License 2.0 7 votes vote down vote up
private KafkaFactory.KafkaClientModel get(){
    Properties props = new Properties();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "10.104.156.83:9092");
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
    props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
    props.put(SaslConfigs.SASL_MECHANISM, "PLAIN");
    props.put("acks", "all");
    props.put("retries", 0);
    props.put("batch.size", 16384);
    props.put("linger.ms", 1);
    props.put("buffer.memory", 33554432);
    props.put("sasl.jaas.config",
            "org.apache.kafka.common.security.plain.PlainLoginModule required username='kafka' password='kafka';");
    KafkaProducer<String, Byte[]> producer = new KafkaProducer<>(props);
    AdminClient client = AdminClient.create(props);

    KafkaFactory.KafkaClientModel kafkaClientModel = new KafkaFactory.KafkaClientModel(producer, client);
    return kafkaClientModel;
}
 
Example #3
Source File: IntegrationTest.java    From aiven-kafka-connect-gcs with GNU Affero General Public License v3.0 6 votes vote down vote up
@BeforeEach
void setUp() throws ExecutionException, InterruptedException {
    testBucketAccessor.clear(gcsPrefix);

    final Properties adminClientConfig = new Properties();
    adminClientConfig.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers());
    adminClient = AdminClient.create(adminClientConfig);

    final Map<String, Object> producerProps = new HashMap<>();
    producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers());
    producerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
            "org.apache.kafka.common.serialization.ByteArraySerializer");
    producerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
            "org.apache.kafka.common.serialization.ByteArraySerializer");
    producer = new KafkaProducer<>(producerProps);

    final NewTopic newTopic0 = new NewTopic(TEST_TOPIC_0, 4, (short) 1);
    final NewTopic newTopic1 = new NewTopic(TEST_TOPIC_1, 4, (short) 1);
    adminClient.createTopics(Arrays.asList(newTopic0, newTopic1)).all().get();

    connectRunner = new ConnectRunner(pluginDir, kafka.getBootstrapServers(), OFFSET_FLUSH_INTERVAL_MS);
    connectRunner.start();
}
 
Example #4
Source File: AbstractSharedKafkaTestResourceTest.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Simple smoke test to ensure broker running appropriate listeners.
 */
@Test
void validateListener() throws ExecutionException, InterruptedException {
    try (final AdminClient adminClient  = getKafkaTestUtils().getAdminClient()) {
        final ConfigResource broker1Resource = new ConfigResource(ConfigResource.Type.BROKER, "1");

        // Pull broker configs
        final Config configResult = adminClient
            .describeConfigs(Collections.singletonList(broker1Resource))
            .values()
            .get(broker1Resource)
            .get();

        // Check listener
        final String actualListener = configResult.get("listeners").value();
        Assertions.assertTrue(
            actualListener.contains(getExpectedListenerProtocol() + "://"),
            "Expected " + getExpectedListenerProtocol() + ":// and found: " + actualListener);

        // Check inter broker protocol
        final String actualBrokerProtocol = configResult.get("security.inter.broker.protocol").value();
        Assertions.assertEquals(getExpectedListenerProtocol(), actualBrokerProtocol, "Unexpected inter-broker protocol");
    }
}
 
Example #5
Source File: MiniKafkaCluster.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private MiniKafkaCluster(List<String> brokerIds)
    throws IOException, InterruptedException {
  this.zkServer = new EmbeddedZooKeeper();
  this.tempDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), "mini-kafka-cluster");
  this.kafkaServer = new ArrayList<>();
  int port = 0;
  for (String id : brokerIds) {
    port = getAvailablePort();
    KafkaConfig c = new KafkaConfig(createBrokerConfig(id, port));
    Seq seq =
        scala.collection.JavaConverters.collectionAsScalaIterableConverter(Collections.emptyList()).asScala().toSeq();
    kafkaServer.add(new KafkaServer(c, Time.SYSTEM, Option.empty(), seq));
  }
  Properties props = new Properties();
  props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:" + port);
  adminClient = AdminClient.create(props);
}
 
Example #6
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
@Test(expected = TopicExistsException.class)
public void testSameTopicCannotBeProvisionedAgain() throws Throwable {
	try (AdminClient admin = AdminClient.create(
			Collections.singletonMap(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,
					embeddedKafka.getEmbeddedKafka().getBrokersAsString()))) {
		admin.createTopics(Collections
				.singletonList(new NewTopic("fooUniqueTopic", 1, (short) 1))).all()
				.get();
		try {
			admin.createTopics(Collections
					.singletonList(new NewTopic("fooUniqueTopic", 1, (short) 1)))
					.all().get();
		}
		catch (Exception ex) {
			assertThat(ex.getCause() instanceof TopicExistsException).isTrue();
			throw ex.getCause();
		}
	}
}
 
Example #7
Source File: JsonFormatTest.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 6 votes vote down vote up
@Before
public void before() throws Exception {
  Map<String, Object> configMap = new HashMap<>();
  configMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, CLUSTER.bootstrapServers());
  configMap.put("application.id", "KSQL");
  configMap.put("commit.interval.ms", 0);
  configMap.put("cache.max.bytes.buffering", 0);
  configMap.put("auto.offset.reset", "earliest");

  KsqlConfig ksqlConfig = new KsqlConfig(configMap);
  adminClient = AdminClient.create(ksqlConfig.getKsqlAdminClientConfigProps());
  topicClient = new KafkaTopicClientImpl(adminClient);
  ksqlEngine = new KsqlEngine(ksqlConfig, topicClient);
  metaStore = ksqlEngine.getMetaStore();

  createInitTopics();
  produceInitData();
  execInitCreateStreamQueries();
}
 
Example #8
Source File: DefaultCollector.java    From paraflow with Apache License 2.0 6 votes vote down vote up
public DefaultCollector()
        throws ConfigFileNotFoundException
{
    CollectorConfig config = CollectorConfig.INSTANCE();
    config.init();
    // init meta client
    metaClient = new MetaClient(config.getMetaServerHost(),
            config.getMetaServerPort());
    // init kafka admin client
    Properties properties = new Properties();
    properties.setProperty("bootstrap.servers", config.getKafkaBootstrapServers());
    properties.setProperty("client.id", "producerAdmin");
    properties.setProperty("metadata.max.age.ms", "3000");
    kafkaAdminClient = AdminClient.create(properties);
    this.collectorRuntime = new CollectorRuntime(config);
    init();
}
 
Example #9
Source File: AdminClientWrapper.java    From hdinsight-kafka-java-get-started with MIT License 6 votes vote down vote up
public static void createTopics(String brokers, String topicName) throws IOException {
    // Set properties used to configure admin client
    Properties properties = getProperties(brokers);

    try (final AdminClient adminClient = KafkaAdminClient.create(properties)) {
        int numPartitions = 8;
        short replicationFactor = (short)3;
        final NewTopic newTopic = new NewTopic(topicName, numPartitions, replicationFactor);

        final CreateTopicsResult createTopicsResult = adminClient.createTopics(Collections.singleton(newTopic));
        createTopicsResult.values().get(topicName).get();
        System.out.print("Topic " + topicName + " created");
    } catch (Exception e) {
        System.out.print("Create Topics denied\n");
        System.out.print(e.getMessage());
        //throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example #10
Source File: SerializationTutorial.java    From kafka-tutorials with Apache License 2.0 6 votes vote down vote up
private void createTopics(Properties envProps) {
  Map<String, Object> config = new HashMap<>();

  config.put("bootstrap.servers", envProps.getProperty("bootstrap.servers"));
  AdminClient client = AdminClient.create(config);

  List<NewTopic> topics = new ArrayList<>();

  topics.add(new NewTopic(
      envProps.getProperty("input.avro.movies.topic.name"),
      parseInt(envProps.getProperty("input.avro.movies.topic.partitions")),
      parseShort(envProps.getProperty("input.avro.movies.topic.replication.factor"))));

  topics.add(new NewTopic(
      envProps.getProperty("output.proto.movies.topic.name"),
      parseInt(envProps.getProperty("output.proto.movies.topic.partitions")),
      parseShort(envProps.getProperty("output.proto.movies.topic.replication.factor"))));

  client.createTopics(topics);
  client.close();
}
 
Example #11
Source File: KafkaTopicProvisioner.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
private void createTopic(AdminClient adminClient, String name, int partitionCount,
		boolean tolerateLowerPartitionsOnBroker, KafkaTopicProperties properties) {
	try {
		createTopicIfNecessary(adminClient, name, partitionCount,
				tolerateLowerPartitionsOnBroker, properties);
	}
	// TODO: Remove catching Throwable. See this thread:
	// TODO:
	// https://github.com/spring-cloud/spring-cloud-stream-binder-kafka/pull/514#discussion_r241075940
	catch (Throwable throwable) {
		if (throwable instanceof Error) {
			throw (Error) throwable;
		}
		else {
			// TODO:
			// https://github.com/spring-cloud/spring-cloud-stream-binder-kafka/pull/514#discussion_r241075940
			throw new ProvisioningException("Provisioning exception", throwable);
		}
	}
}
 
Example #12
Source File: KafkaRule.java    From kafka-pubsub-emulator with Apache License 2.0 6 votes vote down vote up
public void createTopic(String name) throws Exception {
  try (AdminClient admin = AdminClient.create(getAdminConfig())) {
    int partitions = 3;
    int replication = this.replicationFactor;
    Matcher matcher = Pattern.compile("(-\\d+[p|r])").matcher(name);
    while (matcher.find()) {
      String group = matcher.group();
      if (group.endsWith("p")) {
        partitions = getNumber(group);
      } else {
        replication = getNumber(group);
      }
    }

    NewTopic topic = new NewTopic(name, partitions, (short) replication);
    CreateTopicsResult topicsResult = admin.createTopics(singleton(topic));
    topicsResult.all().get(5, TimeUnit.SECONDS);
  } catch (Exception e) {
    LOGGER.log(Level.SEVERE, "Error on create topics " + name, e);
    throw e;
  }
}
 
Example #13
Source File: TumblingWindow.java    From kafka-tutorials with Apache License 2.0 6 votes vote down vote up
public void createTopics(Properties envProps) {
    Map<String, Object> config = new HashMap<>();
    config.put("bootstrap.servers", envProps.getProperty("bootstrap.servers"));
    AdminClient client = AdminClient.create(config);

    List<NewTopic> topics = new ArrayList<>();
    Map<String, String> topicConfigs = new HashMap<>();
    topicConfigs.put("retention.ms", Long.toString(Long.MAX_VALUE));

    NewTopic ratings = new NewTopic(envProps.getProperty("rating.topic.name"),
                                    Integer.parseInt(envProps.getProperty("rating.topic.partitions")),
                                    Short.parseShort(envProps.getProperty("rating.topic.replication.factor")));
    ratings.configs(topicConfigs);
    topics.add(ratings);

    NewTopic counts = new NewTopic(envProps.getProperty("rating.count.topic.name"),
                                   Integer.parseInt(envProps.getProperty("rating.count.topic.partitions")),
                                   Short.parseShort(envProps.getProperty("rating.count.topic.replication.factor")));
    counts.configs(topicConfigs);
    topics.add(counts);


    client.createTopics(topics);
    client.close();
}
 
Example #14
Source File: AggregatingCount.java    From kafka-tutorials with Apache License 2.0 6 votes vote down vote up
public void createTopics(Properties envProps) {
  Map<String, Object> config = new HashMap<>();
  config.put("bootstrap.servers", envProps.getProperty("bootstrap.servers"));
  AdminClient client = AdminClient.create(config);

  List<NewTopic> topics = new ArrayList<>();
  topics.add(new NewTopic(
      envProps.getProperty("input.topic.name"),
      Integer.parseInt(envProps.getProperty("input.topic.partitions")),
      Short.parseShort(envProps.getProperty("input.topic.replication.factor"))));
  topics.add(new NewTopic(
      envProps.getProperty("output.topic.name"),
      Integer.parseInt(envProps.getProperty("output.topic.partitions")),
      Short.parseShort(envProps.getProperty("output.topic.replication.factor"))));

  client.createTopics(topics);
  client.close();
}
 
Example #15
Source File: RunningAverage.java    From kafka-tutorials with Apache License 2.0 6 votes vote down vote up
/**
 * Create topics using AdminClient API
 */
private void createTopics(Properties envProps) {
  Map<String, Object> config = new HashMap<>();

  config.put("bootstrap.servers", envProps.getProperty("bootstrap.servers"));
  AdminClient client = AdminClient.create(config);

  List<NewTopic> topics = new ArrayList<>();

  topics.add(new NewTopic(
      envProps.getProperty("input.ratings.topic.name"),
      parseInt(envProps.getProperty("input.ratings.topic.partitions")),
      parseShort(envProps.getProperty("input.ratings.topic.replication.factor"))));

  topics.add(new NewTopic(
      envProps.getProperty("output.rating-averages.topic.name"),
      parseInt(envProps.getProperty("output.rating-averages.topic.partitions")),
      parseShort(envProps.getProperty("output.rating-averages.topic.replication.factor"))));

  client.createTopics(topics);
  client.close();

}
 
Example #16
Source File: MergeStreams.java    From kafka-tutorials with Apache License 2.0 6 votes vote down vote up
public void createTopics(Properties envProps) {
    Map<String, Object> config = new HashMap<>();
    config.put("bootstrap.servers", envProps.getProperty("bootstrap.servers"));
    AdminClient client = AdminClient.create(config);

    List<NewTopic> topics = new ArrayList<>();

    topics.add(new NewTopic(
            envProps.getProperty("input.rock.topic.name"),
            Integer.parseInt(envProps.getProperty("input.rock.topic.partitions")),
            Short.parseShort(envProps.getProperty("input.rock.topic.replication.factor"))));

    topics.add(new NewTopic(
            envProps.getProperty("input.classical.topic.name"),
            Integer.parseInt(envProps.getProperty("input.classical.topic.partitions")),
            Short.parseShort(envProps.getProperty("input.classical.topic.replication.factor"))));

    topics.add(new NewTopic(
            envProps.getProperty("output.topic.name"),
            Integer.parseInt(envProps.getProperty("output.topic.partitions")),
            Short.parseShort(envProps.getProperty("output.topic.replication.factor"))));

    client.createTopics(topics);
    client.close();
}
 
Example #17
Source File: ClusterTopicManipulationService.java    From kafka-monitor with Apache License 2.0 6 votes vote down vote up
public ClusterTopicManipulationService(String name, AdminClient adminClient) {
  LOGGER.info("ClusterTopicManipulationService constructor initiated {}", this.getClass().getName());

  _isOngoingTopicCreationDone = true;
  _isOngoingTopicDeletionDone = true;
  _adminClient = adminClient;
  _executor = Executors.newSingleThreadScheduledExecutor();
  _reportIntervalSecond = Duration.ofSeconds(1);
  _running = new AtomicBoolean(false);
  _configDefinedServiceName = name;
  // TODO: instantiate a new instance of ClusterTopicManipulationMetrics(..) here.

  MetricConfig metricConfig = new MetricConfig().samples(60).timeWindow(1000, TimeUnit.MILLISECONDS);
  List<MetricsReporter> reporters = new ArrayList<>();
  reporters.add(new JmxReporter(Service.JMX_PREFIX));
  Metrics metrics = new Metrics(metricConfig, reporters, new SystemTime());

  Map<String, String> tags = new HashMap<>();
  tags.put("name", name);
  _clusterTopicManipulationMetrics = new ClusterTopicManipulationMetrics(metrics, tags);
}
 
Example #18
Source File: KafkaFactory.java    From DataLink with Apache License 2.0 6 votes vote down vote up
@Override
public KafkaClientModel load(LoadingKey key) throws Exception {
    KafkaMediaSrcParameter kafkaMediaSrcParameter = key.mediaSourceInfo.getParameterObj();
    PluginWriterParameter pluginWriterParameter = key.getPluginWriterParameter();
    Map<String, String> mapparamters = kafkaMediaSrcParameter.getMapparamters();
    String bootstrapServers = kafkaMediaSrcParameter.getBootstrapServers();

    Map<String, Object> conf = new HashMap<>(8);
    conf.put("bootstrap.servers", bootstrapServers);
    conf.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    conf.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
    conf.put("acks", "all");
    conf.put("batch.size", pluginWriterParameter.getBatchSize());
    conf.putAll(mapparamters);
    KafkaProducer producer = new KafkaProducer<>(conf);
    AdminClient client = AdminClient.create(conf);
    return new KafkaFactory.KafkaClientModel(producer, client);
}
 
Example #19
Source File: DynamicOutputTopic.java    From kafka-tutorials with Apache License 2.0 6 votes vote down vote up
public void createTopics(final Properties envProps) {
    final Map<String, Object> config = new HashMap<>();
    config.put("bootstrap.servers", envProps.getProperty("bootstrap.servers"));
    try (final AdminClient client = AdminClient.create(config)) {

    final List<NewTopic> topics = new ArrayList<>();

        topics.add(new NewTopic(
                envProps.getProperty("input.topic.name"),
                Integer.parseInt(envProps.getProperty("input.topic.partitions")),
                Short.parseShort(envProps.getProperty("input.topic.replication.factor"))));

        topics.add(new NewTopic(
                envProps.getProperty("output.topic.name"),
                Integer.parseInt(envProps.getProperty("output.topic.partitions")),
                Short.parseShort(envProps.getProperty("output.topic.replication.factor"))));

        topics.add(new NewTopic(
            envProps.getProperty("special.order.topic.name"),
            Integer.parseInt(envProps.getProperty("special.order.topic.partitions")),
            Short.parseShort(envProps.getProperty("special.order.topic.replication.factor"))));

        client.createTopics(topics);
    }
}
 
Example #20
Source File: KafkaAdminFactoryTest.java    From kafka-webview with MIT License 6 votes vote down vote up
/**
 * Test that KafkaAdminFactory can create a working AdminClient when connecting to a non-ssl cluster.
 */
@Test
public void testCreateNonSslAdminClient() throws ExecutionException, InterruptedException {
    // Create Cluster config
    final ClusterConfig clusterConfig = ClusterConfig.newBuilder()
        .withBrokerHosts(sharedKafkaTestResource.getKafkaConnectString())
        .build();

    final KafkaAdminFactory kafkaAdminFactory = new KafkaAdminFactory(
        new KafkaClientConfigUtil("not/used", "MyPrefix")
    );

    // Create instance
    try (final AdminClient adminClient = kafkaAdminFactory.create(clusterConfig, "MyClientId")) {

        // Call method to validate things work as expected
        final DescribeClusterResult results = adminClient.describeCluster();
        assertNotNull("Should have a non-null result", results);

        // Request future result
        final Collection<Node> nodes = results.nodes().get();
        assertNotNull("Should have non-null node result", nodes);
        assertFalse("Should have non-empty node", nodes.isEmpty());
    }
}
 
Example #21
Source File: KafkaTestEnvironmentImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteTestTopic(String topic) {
	LOG.info("Deleting topic {}", topic);
	Properties props = getSecureProperties();
	props.putAll(getStandardProperties());
	String clientId = Long.toString(new Random().nextLong());
	props.put("client.id", clientId);
	AdminClient adminClient = AdminClient.create(props);
	// We do not use a try-catch clause here so we can apply a timeout to the admin client closure.
	try {
		tryDelete(adminClient, topic);
	} catch (Exception e) {
		e.printStackTrace();
		fail(String.format("Delete test topic : %s failed, %s", topic, e.getMessage()));
	} finally {
		adminClient.close(Duration.ofMillis(5000L));
		maybePrintDanglingThreadStacktrace(clientId);
	}
}
 
Example #22
Source File: AutomaticTopicConfiguration.java    From eventapis with Apache License 2.0 5 votes vote down vote up
private int getDefaultNumberOfPartitions(AdminClient adminClient) {
    try {
        return adminClient.describeTopics(Collections.singleton(Operation.OPERATION_EVENTS))
                .all().get().values().iterator().next()
                .partitions().size();
    } catch (InterruptedException | ExecutionException | NullPointerException e) {
        log.warn("Error while Calculating Number of Partitions from Topic: " + Operation.OPERATION_EVENTS + " Assuming " + DEFAULT_NUM_PARTITIONS);
        return DEFAULT_NUM_PARTITIONS;
    }
}
 
Example #23
Source File: KafkaMetricsServiceImpl.java    From kafka-eagle with Apache License 2.0 5 votes vote down vote up
private String addTopicConfig(String clusterAlias, AdminClient adminClient, String topic, ConfigEntry configEntry) {
	try {
		String describeTopicConfigs = describeTopicConfig(clusterAlias, topic);
		JSONObject object = JSON.parseObject(describeTopicConfigs).getJSONObject("config");
		if (object.containsKey(configEntry.name())) {
			object.remove(configEntry.name());
		}
		List<ConfigEntry> configEntrys = new ArrayList<>();
		for (String key : KConstants.Topic.getTopicConfigKeys()) {
			if (object.containsKey(key)) {
				configEntrys.add(new ConfigEntry(key, object.getString(key)));
			}
		}
		configEntrys.add(configEntry);
		Map<ConfigResource, Config> configs = new HashMap<>();
		ConfigResource configRes = new ConfigResource(Type.TOPIC, topic);
		Config config = new Config(configEntrys);
		configs.put(configRes, config);
		AlterConfigsResult alterConfig = adminClient.alterConfigs(configs);
		alterConfig.all().get();
		return KConstants.Topic.SUCCESS;
	} catch (Exception e) {
		e.printStackTrace();
		LOG.error("Add topic[" + topic + "] config has error, msg is " + e.getMessage());
		return e.getMessage();
	}
}
 
Example #24
Source File: ClusterTopicManipulationServiceFactory.java    From kafka-monitor with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Service createService() throws Exception {

  AdminClient adminClient = AdminClient.create(_properties);

  return new ClusterTopicManipulationService(_serviceName, adminClient);
}
 
Example #25
Source File: CruiseControlMetricsReporterAutoCreateTopicTest.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Setup the unit test.
 */
@Before
public void setUp() {
    super.setUp();

    // creating the "TestTopic" explicitly because the topic auto-creation is disabled on the broker
    Properties adminProps = new Properties();
    adminProps.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers());
    AdminClient adminClient = AdminClient.create(adminProps);
    NewTopic testTopic = new NewTopic(TEST_TOPIC, 1, (short) 1);
    CreateTopicsResult createTopicsResult = adminClient.createTopics(Collections.singleton(testTopic));

    AtomicInteger adminFailed = new AtomicInteger(0);
    createTopicsResult.all().whenComplete((v, e) -> {
        if (e != null) {
            adminFailed.incrementAndGet();
        }
    });
    assertEquals(0, adminFailed.get());

    // starting producer to verify that Kafka cluster is working fine
    Properties producerProps = new Properties();
    producerProps.setProperty(ProducerConfig.ACKS_CONFIG, "-1");
    AtomicInteger producerFailed = new AtomicInteger(0);
    try (Producer<String, String> producer = createProducer(producerProps)) {
        for (int i = 0; i < 10; i++) {
            producer.send(new ProducerRecord<>(TEST_TOPIC, Integer.toString(i)),
                    (m, e) -> {
                        if (e != null) {
                            producerFailed.incrementAndGet();
                        }
                    });
        }
    }
    assertEquals(0, producerFailed.get());
}
 
Example #26
Source File: AdminClientWrapper.java    From hdinsight-kafka-java-get-started with MIT License 5 votes vote down vote up
public static void deleteTopics(String brokers, String topicName) throws IOException {
    // Set properties used to configure admin client
    Properties properties = getProperties(brokers);

    try (final AdminClient adminClient = KafkaAdminClient.create(properties)) {
        final DeleteTopicsResult deleteTopicsResult = adminClient.deleteTopics(Collections.singleton(topicName));
        deleteTopicsResult.values().get(topicName).get();
        System.out.print("Topic " + topicName + " deleted");
    } catch (Exception e) {
        System.out.print("Delete Topics denied\n");
        System.out.print(e.getMessage());
        //throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example #27
Source File: KafkaTestEnvironmentImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteTestTopic(String topic) {
	LOG.info("Deleting topic {}", topic);
	try (AdminClient adminClient = AdminClient.create(getStandardProperties())) {
		tryDelete(adminClient, topic);
	} catch (Exception e) {
		e.printStackTrace();
		fail(String.format("Delete test topic : %s failed, %s", topic, e.getMessage()));
	}
}
 
Example #28
Source File: KafkaTestEnvironmentImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
private void tryDelete(AdminClient adminClient, String topic)
		throws Exception {
	try {
		adminClient.deleteTopics(Collections.singleton(topic)).all().get(DELETE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
	} catch (TimeoutException e) {
		LOG.info("Did not receive delete topic response within %d seconds. Checking if it succeeded",
			DELETE_TIMEOUT_SECONDS);
		if (adminClient.listTopics().names().get(DELETE_TIMEOUT_SECONDS, TimeUnit.SECONDS).contains(topic)) {
			throw new Exception("Topic still exists after timeout");
		}
	}
}
 
Example #29
Source File: KafkaTestEnvironmentImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void createTestTopic(String topic, int numberOfPartitions, int replicationFactor, Properties properties) {
	LOG.info("Creating topic {}", topic);
	try (AdminClient adminClient = AdminClient.create(getStandardProperties())) {
		NewTopic topicObj = new NewTopic(topic, numberOfPartitions, (short) replicationFactor);
		adminClient.createTopics(Collections.singleton(topicObj)).all().get();
	} catch (Exception e) {
		e.printStackTrace();
		fail("Create test topic : " + topic + " failed, " + e.getMessage());
	}
}
 
Example #30
Source File: KeycloakClientCredentialsWithJwtValidationAuthzTest.java    From strimzi-kafka-oauth with Apache License 2.0 5 votes vote down vote up
void cleanup() throws Exception {
    Properties bobAdminProps = buildAdminClientConfig(tokens.get(BOB));
    AdminClient admin = AdminClient.create(bobAdminProps);

    admin.deleteTopics(Arrays.asList(TOPIC_A, TOPIC_B, TOPIC_X, "non-existing-topic"));
    admin.deleteConsumerGroups(Arrays.asList(groupFor(TOPIC_A), groupFor(TOPIC_B), groupFor(TOPIC_X), groupFor("non-existing-topic")));
}