Java Code Examples for kafka.zk.AdminZkClient#createTopic()

The following examples show how to use kafka.zk.AdminZkClient#createTopic() . 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: KafkaUnit.java    From SkaETL with Apache License 2.0 7 votes vote down vote up
public void createTopic(String topicName, int numPartitions) {
    // setup

    String zookeeperHost = zookeeperString;
    Boolean isSucre = false;
    int sessionTimeoutMs = 200000;
    int connectionTimeoutMs = 15000;
    int maxInFlightRequests = 10;
    Time time = Time.SYSTEM;
    String metricGroup = "myGroup";
    String metricType = "myType";
    KafkaZkClient zkClient = KafkaZkClient.apply(zookeeperHost,isSucre,sessionTimeoutMs,
            connectionTimeoutMs,maxInFlightRequests,time,metricGroup,metricType);
    AdminZkClient adminZkClient = new AdminZkClient(zkClient);
    try {
        // run
        LOGGER.info("Executing: CreateTopic " + topicName);
        adminZkClient.createTopic(topicName,numPartitions, 1,new Properties(), RackAwareMode.Disabled$.MODULE$);
    } finally {
        zkClient.close();
    }

}
 
Example 2
Source File: LoadMonitorTaskRunnerTest.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Setup the test.
 */
@Before
public void setUp() {
  super.setUp();
  KafkaZkClient kafkaZkClient = KafkaCruiseControlUtils.createKafkaZkClient(zookeeper().connectionString(),
                                                                            "LoadMonitorTaskRunnerGroup",
                                                                            "LoadMonitorTaskRunnerSetup",
                                                                            false);
  AdminZkClient adminZkClient = new AdminZkClient(kafkaZkClient);
  for (int i = 0; i < NUM_TOPICS; i++) {
    adminZkClient.createTopic("topic-" + i, NUM_PARTITIONS, 1, new Properties(), RackAwareMode.Safe$.MODULE$);
  }
  KafkaCruiseControlUtils.closeKafkaZkClientWithTimeout(kafkaZkClient);
}
 
Example 3
Source File: KafkaJunitExtensionTest.java    From kafka-junit with Apache License 2.0 5 votes vote down vote up
@Test
void testKafkaServerIsUp(KafkaHelper kafkaHelper) {
    // Setup Zookeeper client
    final String zkConnectionString = kafkaHelper.zookeeperConnectionString();
    final ZooKeeperClient zooKeeperClient = new ZooKeeperClient(zkConnectionString, 2000, 8000, Integer.MAX_VALUE, Time.SYSTEM,"kafka.server", "SessionExpireListener" );
    final KafkaZkClient zkClient = new KafkaZkClient(zooKeeperClient, JaasUtils.isZkSaslEnabled(), Time.SYSTEM);
    final AdminZkClient adminZkClient = new AdminZkClient(zkClient);

    // Create topic
    adminZkClient.createTopic(TOPIC, 1, 1, new Properties(), null);

    // Produce/consume test
    try (KafkaProducer<String, String> producer = kafkaHelper.createStringProducer()) {
        producer.send(new ProducerRecord<>(TOPIC, "keyA", "valueA"));
    }

    try (KafkaConsumer<String, String> consumer = kafkaHelper.createStringConsumer()) {
        consumer.subscribe(Lists.newArrayList(TOPIC));
        ConsumerRecords<String, String> records = consumer.poll(10000);
        Assertions.assertAll(() -> assertThat(records).isNotNull(),
                             () -> assertThat(records.isEmpty()).isFalse());

        ConsumerRecord<String, String> msg = records.iterator().next();
        Assertions.assertAll(() -> assertThat(msg).isNotNull(),
                             () -> assertThat(msg.key()).isEqualTo("keyA"),
                             () -> assertThat(msg.value()).isEqualTo("valueA"));
    }
}
 
Example 4
Source File: KafkaJunitRuleTest.java    From kafka-junit with Apache License 2.0 5 votes vote down vote up
@Test
public void testKafkaServerIsUp() {
    // Setup Zookeeper client
    final String zkConnectionString = kafkaRule.helper().zookeeperConnectionString();
    final ZooKeeperClient zooKeeperClient = new ZooKeeperClient(zkConnectionString, 2000, 8000, Integer.MAX_VALUE, Time.SYSTEM,"kafka.server", "SessionExpireListener" );
    final KafkaZkClient zkClient = new KafkaZkClient(zooKeeperClient, JaasUtils.isZkSaslEnabled(), Time.SYSTEM);
    final AdminZkClient adminZkClient = new AdminZkClient(zkClient);

    // Create topic
    adminZkClient.createTopic(TOPIC, 1, 1, new Properties(), null);

    // Produce/consume test
    try (KafkaProducer<String, String> producer = kafkaRule.helper().createStringProducer()) {
        producer.send(new ProducerRecord<>(TOPIC, "keyA", "valueA"));
    }

    try (KafkaConsumer<String, String> consumer = kafkaRule.helper().createStringConsumer()) {
        consumer.subscribe(Lists.newArrayList(TOPIC));
        ConsumerRecords<String, String> records = consumer.poll(TEN_SECONDS);
        assertThat(records).isNotNull();
        assertThat(records.isEmpty()).isFalse();

        ConsumerRecord<String, String> msg = records.iterator().next();
        assertThat(msg).isNotNull();
        assertThat(msg.key()).isEqualTo("keyA");
        assertThat(msg.value()).isEqualTo("valueA");
    }
}
 
Example 5
Source File: KafkaTestHelper.java    From nakadi with MIT License 5 votes vote down vote up
public void createTopic(final String topic, final String zkUrl) {
    try (KafkaZkClient zkClient = createZkClient(zkUrl)) {
        final AdminZkClient adminZkClient = new AdminZkClient(zkClient);
        adminZkClient.createTopic(topic, 1, 1,
                new Properties(), RackAwareMode.Safe$.MODULE$);
    }
}
 
Example 6
Source File: KafkaTestUtils.java    From ranger with Apache License 2.0 5 votes vote down vote up
static void createSomeTopics(String zkConnectString) {
	ZooKeeperClient zookeeperClient = new ZooKeeperClient(zkConnectString, 30000, 30000,
			1, Time.SYSTEM, "kafka.server", "SessionExpireListener", Option.empty());
	try (KafkaZkClient kafkaZkClient = new KafkaZkClient(zookeeperClient, false, Time.SYSTEM)) {
		AdminZkClient adminZkClient = new AdminZkClient(kafkaZkClient);
		adminZkClient.createTopic("test", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
		adminZkClient.createTopic("dev", 1, 1, new Properties(), RackAwareMode.Enforced$.MODULE$);
	}
}