kafka.api.TopicMetadata Java Examples
The following examples show how to use
kafka.api.TopicMetadata.
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: TopicTest.java From hermes with Apache License 2.0 | 6 votes |
@Test public void createTopicInTestEnv() { String ZOOKEEPER_CONNECT = ""; ZkClient zkClient = new ZkClient(new ZkConnection(ZOOKEEPER_CONNECT)); ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(ZOOKEEPER_CONNECT), false); zkClient.setZkSerializer(new ZKStringSerializer()); int partition = 1; int replication = 1; String topic = String.format("kafka.test_create_topic_p%s_r%s", partition, replication); if (AdminUtils.topicExists(zkUtils, topic)) { TopicMetadata topicMetadata = AdminUtils.fetchTopicMetadataFromZk(topic, zkUtils); System.out.println(topicMetadata); AdminUtils.deleteTopic(zkUtils, topic); } AdminUtils.createTopic(zkUtils, topic, partition, replication, new Properties()); }
Example #2
Source File: MockKafka.java From hermes with Apache License 2.0 | 5 votes |
public TopicMetadata fetchTopicMeta(String topic) { ZkClient zkClient = new ZkClient(zkServer.getConnection()); ZkUtils zkUtils = new ZkUtils(zkClient, zkServer.getConnection(), false); zkClient.setZkSerializer(new ZKStringSerializer()); TopicMetadata topicMetadata = AdminUtils.fetchTopicMetadataFromZk(topic, zkUtils); zkClient.close(); return topicMetadata; }
Example #3
Source File: MockKafkaClusterTest.java From hermes with Apache License 2.0 | 5 votes |
@Test public void createTopicWithMultiplePartitions() { String topic = "mytopic"; cluster.createTopic(topic, 3, 2); TopicMetadata fetchTopicMetadata = cluster.fetchTopicMetadata(topic); Assert.assertEquals(3, fetchTopicMetadata.partitionsMetadata().size()); }
Example #4
Source File: KafkaPartitionLevelConsumerTest.java From incubator-pinot with Apache License 2.0 | 5 votes |
@Override public TopicMetadataResponse send(TopicMetadataRequest request) { java.util.List<String> topics = request.topics(); TopicMetadata[] topicMetadataArray = new TopicMetadata[topics.size()]; for (int i = 0; i < topicMetadataArray.length; i++) { String topic = topics.get(i); if (!topic.equals(topicName)) { topicMetadataArray[i] = new TopicMetadata(topic, null, Errors.UNKNOWN_TOPIC_OR_PARTITION.code()); } else { PartitionMetadata[] partitionMetadataArray = new PartitionMetadata[partitionCount]; for (int j = 0; j < partitionCount; j++) { java.util.List<BrokerEndPoint> emptyJavaList = Collections.emptyList(); List<BrokerEndPoint> emptyScalaList = JavaConversions.asScalaBuffer(emptyJavaList).toList(); partitionMetadataArray[j] = new PartitionMetadata(j, Some.apply(brokerArray[partitionLeaderIndices[j]]), emptyScalaList, emptyScalaList, Errors.NONE.code()); } Seq<PartitionMetadata> partitionsMetadata = List.fromArray(partitionMetadataArray); topicMetadataArray[i] = new TopicMetadata(topic, partitionsMetadata, Errors.NONE.code()); } } Seq<BrokerEndPoint> brokers = List.fromArray(brokerArray); Seq<TopicMetadata> topicsMetadata = List.fromArray(topicMetadataArray); return new TopicMetadataResponse(new kafka.api.TopicMetadataResponse(brokers, topicsMetadata, -1)); }
Example #5
Source File: MockKafkaCluster.java From hermes with Apache License 2.0 | 4 votes |
public TopicMetadata fetchTopicMetadata(String topic) { Random random = new Random(); MockKafka kafka = kafkaCluster.get(random.nextInt(kafkaCluster.size())); return kafka.fetchTopicMeta(topic); }
Example #6
Source File: Kafka09TopicProvisionTest.java From incubator-gobblin with Apache License 2.0 | 4 votes |
@Test (enabled=false) public void testTopicPartitionCreationCount() throws IOException, InterruptedException { String topic = "topicPartition4"; int clusterCount = _kafkaTestHelper.getClusterCount(); int partionCount = clusterCount/2; int zkPort = _kafkaTestHelper.getZookeeperPort(); Properties props = new Properties(); // Setting Topic Properties props.setProperty(KafkaWriterConfigurationKeys.KAFKA_TOPIC, topic); props.setProperty(KafkaWriterConfigurationKeys.REPLICATION_COUNT, String.valueOf(clusterCount)); props.setProperty(KafkaWriterConfigurationKeys.PARTITION_COUNT, String.valueOf(partionCount)); props.setProperty(KafkaWriterConfigurationKeys.CLUSTER_ZOOKEEPER, "localhost:"+zkPort); System.out.println(_kafkaTestHelper.getBootServersList()); // Setting Producer Properties props.setProperty(KafkaWriterConfigurationKeys.KAFKA_PRODUCER_CONFIG_PREFIX+"bootstrap.servers", _kafkaTestHelper.getBootServersList()); props.setProperty(KafkaWriterConfigurationKeys.KAFKA_PRODUCER_CONFIG_PREFIX+"value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Kafka09DataWriter<String, String> kafka09DataWriter = new Kafka09DataWriter<>(props); String zookeeperConnect = "localhost:"+_kafkaTestHelper.getZookeeperPort(); int sessionTimeoutMs = 10 * 1000; int connectionTimeoutMs = 8 * 1000; // Note: You must initialize the ZkClient with ZKStringSerializer. If you don't, then // createTopic() will only seem to work (it will return without error). The topic will exist in // only ZooKeeper and will be returned when listing topics, but Kafka itself does not create the // topic. ZkClient zkClient = new ZkClient( zookeeperConnect, sessionTimeoutMs, connectionTimeoutMs, ZKStringSerializer$.MODULE$); boolean isSecureKafkaCluster = false; ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect), isSecureKafkaCluster); TopicMetadata metaData = AdminUtils.fetchTopicMetadataFromZk(topic,zkUtils); Assert.assertEquals(metaData.partitionsMetadata().size(), partionCount); }
Example #7
Source File: Kafka09TopicProvisionTest.java From incubator-gobblin with Apache License 2.0 | 4 votes |
@Test (enabled=false) public void testLiveTopicPartitionCreationCount() throws IOException, InterruptedException { String liveClusterCount = System.getProperty("live.cluster.count"); String liveZookeeper = System.getProperty("live.zookeeper"); String liveBroker = System.getProperty("live.broker"); String topic = System.getProperty("live.newtopic"); String topicReplicationCount = System.getProperty("live.newtopic.replicationCount"); String topicPartitionCount = System.getProperty("live.newtopic.partitionCount"); if(StringUtils.isEmpty(liveClusterCount)){ Assert.assertTrue(true); return; } if(StringUtils.isEmpty(topicPartitionCount)){ int clusterCount = Integer.parseInt(liveClusterCount); clusterCount--; int partionCount = clusterCount/2; topicReplicationCount = String.valueOf(clusterCount); topicPartitionCount = String.valueOf(partionCount); } Properties props = new Properties(); // Setting Topic Properties props.setProperty(KafkaWriterConfigurationKeys.KAFKA_TOPIC, topic); props.setProperty(KafkaWriterConfigurationKeys.REPLICATION_COUNT, topicReplicationCount); props.setProperty(KafkaWriterConfigurationKeys.PARTITION_COUNT, topicPartitionCount ); props.setProperty(KafkaWriterConfigurationKeys.CLUSTER_ZOOKEEPER, liveZookeeper); // Setting Producer Properties props.setProperty(KafkaWriterConfigurationKeys.KAFKA_PRODUCER_CONFIG_PREFIX+"bootstrap.servers", liveBroker); props.setProperty(KafkaWriterConfigurationKeys.KAFKA_PRODUCER_CONFIG_PREFIX+"value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); Kafka09DataWriter<String, String> kafka09DataWriter = new Kafka09DataWriter<>(props); int sessionTimeoutMs = 10 * 1000; int connectionTimeoutMs = 8 * 1000; // Note: You must initialize the ZkClient with ZKStringSerializer. If you don't, then // createTopic() will only seem to work (it will return without error). The topic will exist in // only ZooKeeper and will be returned when listing topics, but Kafka itself does not create the // topic. ZkClient zkClient = new ZkClient( liveZookeeper, sessionTimeoutMs, connectionTimeoutMs, ZKStringSerializer$.MODULE$); boolean isSecureKafkaCluster = false; ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(liveZookeeper), isSecureKafkaCluster); TopicMetadata metaData = AdminUtils.fetchTopicMetadataFromZk(topic,zkUtils); Assert.assertEquals(metaData.partitionsMetadata().size(), Integer.parseInt(topicPartitionCount)); }