本文整理汇总了Java中kafka.admin.AdminUtils.topicExists方法的典型用法代码示例。如果您正苦于以下问题:Java AdminUtils.topicExists方法的具体用法?Java AdminUtils.topicExists怎么用?Java AdminUtils.topicExists使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kafka.admin.AdminUtils
的用法示例。
在下文中一共展示了AdminUtils.topicExists方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createMonitoringTopicIfNotExists
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
/**
* Create the topic that the monitor uses to monitor the cluster. This method attempts to create a topic so that all
* the brokers in the cluster will have partitionToBrokerRatio partitions. If the topic exists, but has different parameters
* then this does nothing to update the parameters.
*
* TODO: Do we care about rack aware mode? I would think no because we want to spread the topic over all brokers.
* @param zkUrl zookeeper connection url
* @param topic topic name
* @param replicationFactor the replication factor for the topic
* @param partitionToBrokerRatio This is multiplied by the number brokers to compute the number of partitions in the topic.
* @param topicConfig additional parameters for the topic for example min.insync.replicas
* @return the number of partitions created
*/
public static int createMonitoringTopicIfNotExists(String zkUrl, String topic, int replicationFactor,
double partitionToBrokerRatio, Properties topicConfig) {
ZkUtils zkUtils = ZkUtils.apply(zkUrl, ZK_SESSION_TIMEOUT_MS, ZK_CONNECTION_TIMEOUT_MS, JaasUtils.isZkSecurityEnabled());
try {
if (AdminUtils.topicExists(zkUtils, topic)) {
return getPartitionNumForTopic(zkUrl, topic);
}
int brokerCount = zkUtils.getAllBrokersInCluster().size();
int partitionCount = (int) Math.ceil(brokerCount * partitionToBrokerRatio);
try {
AdminUtils.createTopic(zkUtils, topic, partitionCount, replicationFactor, topicConfig, RackAwareMode.Enforced$.MODULE$);
} catch (TopicExistsException e) {
//There is a race condition with the consumer.
LOG.debug("Monitoring topic " + topic + " already exists in cluster " + zkUrl, e);
return getPartitionNumForTopic(zkUrl, topic);
}
LOG.info("Created monitoring topic " + topic + " in cluster " + zkUrl + " with " + partitionCount + " partitions, min ISR of "
+ topicConfig.get(KafkaConfig.MinInSyncReplicasProp()) + " and replication factor of " + replicationFactor + ".");
return partitionCount;
} finally {
zkUtils.close();
}
}
示例2: createTopic
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
/**
* Creates a set of Kafka topics for each topic that does not already exist.
*
* @param zookeeperServers - The Zookeeper servers that are used by the Kafka Streams program. (not null)
* @param topicNames - The topics that will be created. (not null)
* @param partitions - The number of partitions that each of the topics will have.
* @param replicationFactor - The replication factor of the topics that are created.
*/
public static void createTopic(
final String zookeeperServers,
final Set<String> topicNames,
final int partitions,
final int replicationFactor) {
requireNonNull(zookeeperServers);
requireNonNull(topicNames);
ZkUtils zkUtils = null;
try {
zkUtils = ZkUtils.apply(new ZkClient(zookeeperServers, 30000, 30000, ZKStringSerializer$.MODULE$), false);
for(final String topicName : topicNames) {
if(!AdminUtils.topicExists(zkUtils, topicName)) {
AdminUtils.createTopic(zkUtils, topicName, partitions, replicationFactor, new Properties(), RackAwareMode.Disabled$.MODULE$);
}
}
}
finally {
if(zkUtils != null) {
zkUtils.close();
}
}
}
示例3: createTopic
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
public static void createTopic(AppContext context,
String topicName,
int partitions,
int replicationFactor,
Schema schema) {
// Currently the schema parameter is unused, but we have it here so it can
// be applied to a schema registry for the topic when that is available.
ZkClient client = new ZkClient(getKafkaProp(context, ZOOKEEPER_CONNECT), 1000, 1000, ZKStringSerializer$.MODULE$);
try {
if (!AdminUtils.topicExists(client, topicName)) {
AdminUtils.createTopic(client, topicName, partitions, replicationFactor, new Properties());
}
} finally {
client.close();
}
}
示例4: createTopic
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
public void createTopic(String topic) {
ZkClient zkClient = new ZkClient(
kafkaRpcConfig.getZookeeperConnect(),
kafkaRpcConfig.getSessionTimeout(),
kafkaRpcConfig.getConnectionTimeout(),
ZKStringSerializer$.MODULE$);
try {
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(kafkaRpcConfig.getZookeeperConnect()), false);
Properties topicConfig = kafkaRpcConfig.topicProps();
if (!AdminUtils.topicExists(zkUtils, topic)) {
AdminUtils.createTopic(zkUtils, topic, kafkaRpcConfig.getNumPartitions(),
kafkaRpcConfig.getReplicationFactor(), topicConfig, RackAwareMode.Enforced$.MODULE$);
}
} finally {
zkClient.close();
}
}
示例5: createKafkaTopicIfNecessary
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
public static boolean createKafkaTopicIfNecessary(String brokerUri, int replFactor, int numPartitions, String topic)
throws IOException {
URI zkUri = URI.create(brokerUri);
Preconditions.checkArgument("zk".equals(zkUri.getScheme()));
String zkServerList = zkUri.getAuthority() + zkUri.getPath();
ZkUtils zkUtils = ZkUtils.apply(zkServerList, ZK_SESSION_TIMEOUT_MS,
ZK_CONNECTION_TIMEOUT_MS, JaasUtils.isZkSecurityEnabled());
try {
if (AdminUtils.topicExists(zkUtils, topic)) {
return false;
}
try {
AdminUtils.createTopic(zkUtils, topic, numPartitions, replFactor, new Properties());
} catch (TopicExistsException ignored) {
return false;
} catch (RuntimeException e) {
throw new IOException(e);
}
} finally {
if (zkUtils != null) {
zkUtils.close();
}
}
return true;
}
示例6: checkAndCreateTopic
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
/**
* Creates Kafka topic if it does not exist.
*
* @param topic Kafka topic
*/
protected void checkAndCreateTopic(final String topic) {
String hosts = config.getZookeeperHosts();
ZkClient zkClient = new ZkClient(hosts, config.getZookeeperSessionTimeout(),
config.getZookeeperConnectTimeout(), ZKStringSerializer$.MODULE$);
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(hosts), false);
// FIXME(dbogun): race condition
if (!AdminUtils.topicExists(zkUtils, topic)) {
AdminUtils.createTopic(zkUtils, topic, 1, 1,
AdminUtils.createTopic$default$5(), AdminUtils.createTopic$default$6());
}
}
示例7: deleteTopics
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
public static void deleteTopics(final KafkaEmbedded kafkaEmbedded, final String... topics) {
ZkUtils zkUtils = new ZkUtils(kafkaEmbedded.getZkClient(), null, false);
for (String topic : topics) {
if (AdminUtils.topicExists(zkUtils, topic)) {
LOGGER.info("Deleting topic {}", topic);
kafkaEmbedded.waitUntilSynced(topic, 0);
AdminUtils.deleteTopic(zkUtils, topic);
while (AdminUtils.topicExists(zkUtils, topic)) {
//wait for topic to die
}
kafkaEmbedded.waitUntilSynced(topic, 0);
}
}
}
示例8: maybeCreateTopic
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
/**
* @param zkServers Zookeeper server string: host1:port1[,host2:port2,...]
* @param topic topic to create (if not already existing)
* @param partitions number of topic partitions
* @param topicProperties optional topic config properties
*/
public static void maybeCreateTopic(String zkServers,
String topic,
int partitions,
Properties topicProperties) {
ZkUtils zkUtils = ZkUtils.apply(zkServers, ZK_TIMEOUT_MSEC, ZK_TIMEOUT_MSEC, false);
try {
if (AdminUtils.topicExists(zkUtils, topic)) {
log.info("No need to create topic {} as it already exists", topic);
} else {
log.info("Creating topic {}", topic);
try {
AdminUtils.createTopic(
zkUtils, topic, partitions, 1, topicProperties, RackAwareMode.Enforced$.MODULE$);
log.info("Created Zookeeper topic {}", topic);
} catch (RuntimeException re) {
// Hack to deal with the fact that kafka.common.TopicExistsException became
// org.apache.kafka.common.errors.TopicExistsException from 0.10.0 to 0.10.1
if ("TopicExistsException".equals(re.getClass().getSimpleName())) {
log.info("Zookeeper topic {} already exists", topic);
} else {
throw re;
}
}
}
} finally {
zkUtils.close();
}
}
示例9: topicExists
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
/**
* @param zkServers Zookeeper server string: host1:port1[,host2:port2,...]
* @param topic topic to check for existence
* @return {@code true} if and only if the given topic exists
*/
public static boolean topicExists(String zkServers, String topic) {
ZkUtils zkUtils = ZkUtils.apply(zkServers, ZK_TIMEOUT_MSEC, ZK_TIMEOUT_MSEC, false);
try {
return AdminUtils.topicExists(zkUtils, topic);
} finally {
zkUtils.close();
}
}
示例10: deleteTopic
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
/**
* @param zkServers Zookeeper server string: host1:port1[,host2:port2,...]
* @param topic topic to delete, if it exists
*/
public static void deleteTopic(String zkServers, String topic) {
ZkUtils zkUtils = ZkUtils.apply(zkServers, ZK_TIMEOUT_MSEC, ZK_TIMEOUT_MSEC, false);
try {
if (AdminUtils.topicExists(zkUtils, topic)) {
log.info("Deleting topic {}", topic);
AdminUtils.deleteTopic(zkUtils, topic);
log.info("Deleted Zookeeper topic {}", topic);
} else {
log.info("No need to delete topic {} as it does not exist", topic);
}
} finally {
zkUtils.close();
}
}
示例11: validate
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
@Override
public void validate() throws ValidationException {
super.validate();
if (zkConnect == null || zkConnect.isEmpty())
throw new ValidationException(LOGGER.translate("ZKCONNECT_VALIDATE_ERROR"));
if (bootstrap == null || bootstrap.isEmpty())
throw new ValidationException(LOGGER.translate("BOOTSTRAP_VALIDATE_ERROR"));
if (topic == null || topic.isEmpty())
throw new ValidationException(LOGGER.translate("TOPIC_VALIDATE_ERROR"));
ZkClient zkClient = new ZkClient(zkConnect, 10000, 8000, ZKStringSerializer$.MODULE$);
// Security for Kafka was added in Kafka 0.9.0.0 -> isSecureKafkaCluster = false
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkConnect), false);
if (AdminUtils.topicExists(zkUtils, topic))
zkClient.deleteRecursive(ZkUtils.getTopicPath(topic));
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try
{
Thread.currentThread().setContextClassLoader(null);
AdminUtils.createTopic(zkUtils, topic, partitions, replicas, new Properties(), RackAwareMode.Disabled$.MODULE$);
}
catch (Throwable th) {
LOGGER.error(th.getMessage(), th);
throw new ValidationException(th.getMessage());
}
finally {
Thread.currentThread().setContextClassLoader(classLoader);
}
zkClient.close();
}
示例12: validate
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
@Override
public void validate() throws ValidationException {
super.validate();
if (zkConnect.isEmpty())
throw new ValidationException(LOGGER.translate("ZKCONNECT_VALIDATE_ERROR"));
if (topic.isEmpty())
throw new ValidationException(LOGGER.translate("TOPIC_VALIDATE_ERROR"));
if (groupId.isEmpty())
throw new ValidationException(LOGGER.translate("GROUP_ID_VALIDATE_ERROR"));
if (numThreads < 1)
throw new ValidationException(LOGGER.translate("NUM_THREADS_VALIDATE_ERROR"));
ZkClient zkClient = new ZkClient(zkConnect, 10000, 8000, ZKStringSerializer$.MODULE$);
// Security for Kafka was added in Kafka 0.9.0.0 -> isSecureKafkaCluster = false
ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkConnect), false);
Boolean topicExists = AdminUtils.topicExists(zkUtils, topic);
zkClient.close();
if (!topicExists)
throw new ValidationException(LOGGER.translate("TOPIC_VALIDATE_ERROR"));
// Init Consumer Config
Properties props = new Properties()
{
{ put("zookeeper.connect", zkConnect); }
{ put("group.id", groupId); }
{ put("zookeeper.session.timeout.ms", "400"); }
{ put("zookeeper.sync.time.ms", "200"); }
{ put("auto.commit.interval.ms", "1000"); }
};
consumerConfig = new ConsumerConfig(props);
}
示例13: topicExists
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
public static boolean topicExists(String topic, CuratorFramework curatorFramework) {
ZkClient zkClient = fromCurator(curatorFramework);
try {
zkClient = fromCurator(curatorFramework);
return AdminUtils.topicExists(zkClient, topic);
} finally {
if (zkClient != null) {
zkClient.close();
}
}
}
示例14: topicExists
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
/**
* Determines if the passed topic name represents an existing topic
* @param topicName the topic name to test for
* @return true if the passed topic name represents an existing topic, false otherwise
*/
public boolean topicExists(final String topicName) {
if(!running.get()) throw new IllegalStateException("The KafkaTestServer is not running");
if(topicName==null || topicName.trim().isEmpty()) throw new IllegalArgumentException("The passed topic name was null or empty");
final ZkUtils z = getZkUtils();
return AdminUtils.topicExists(z, topicName.trim());
}
示例15: getTopic
import kafka.admin.AdminUtils; //导入方法依赖的package包/类
@Override
public Topic getTopic(final String topicName) {
if (AdminUtils.topicExists(zkUtils, topicName)) {
final MetadataResponse.TopicMetadata topicMetadata = AdminUtils.fetchTopicMetadataFromZk(topicName, zkUtils);
final Topic topic = new Topic();
topic.setName(topicMetadata.topic());
topic.setPartitions(topicMetadata.partitionMetadata().size());
final int replicas = topicMetadata.partitionMetadata().stream().mapToInt(e -> e.replicas().size()).sum();
topic.setReplications(replicas);
topic.setProperties(getTopicProperties(topicName));
return topic;
}
throw new UnknownTopicException(topicName);
}