Java Code Examples for org.I0Itec.zkclient.ZkClient#close()

The following examples show how to use org.I0Itec.zkclient.ZkClient#close() . 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: KafkaStoreUtils.java    From data-highway with Apache License 2.0 6 votes vote down vote up
public static void checkAndCreateTopic(String zkConnect, String topic, int replicas) {
  ZkClient zkClient = new ZkClient(zkConnect, SESSION_TIMEOUT_MS, CONNECTION_TIMEOUT_MS, ZKStringSerializer$.MODULE$);
  ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkConnect), IS_SECURE_KAFKA_CLUSTER);

  if (AdminUtils.topicExists(zkUtils, topic)) {
    verifyTopic(zkUtils, topic);
    return;
  }

  int partitions = 1;
  Properties topicConfig = new Properties();
  topicConfig.put(LogConfig.CleanupPolicyProp(), "compact");

  AdminUtils.createTopic(zkUtils, topic, partitions, replicas, topicConfig, RackAwareMode.Enforced$.MODULE$);

  zkClient.close();
  zkUtils.close();
}
 
Example 2
Source File: PinotHelixResourceManagerTest.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetInstanceConfigs()
    throws Exception {
  Set<String> servers = _helixResourceManager.getAllInstancesForServerTenant(SERVER_TENANT_NAME);
  for (String server : servers) {
    InstanceConfig cachedInstanceConfig = _helixResourceManager.getHelixInstanceConfig(server);
    InstanceConfig realInstanceConfig = _helixAdmin.getInstanceConfig(getHelixClusterName(), server);
    Assert.assertEquals(cachedInstanceConfig, realInstanceConfig);
  }

  ZkClient zkClient = new ZkClient(_helixResourceManager.getHelixZkURL(), CONNECTION_TIMEOUT_IN_MILLISECOND,
      CONNECTION_TIMEOUT_IN_MILLISECOND, new ZNRecordSerializer());

  modifyExistingInstanceConfig(zkClient);
  addAndRemoveNewInstanceConfig(zkClient);

  zkClient.close();
}
 
Example 3
Source File: KafkaEmbedded.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 6 votes vote down vote up
/**
 * Create a Kafka topic with the given parameters.
 *
 * @param topic       The name of the topic.
 * @param partitions  The number of partitions for this topic.
 * @param replication The replication factor for (partitions of) this topic.
 * @param topicConfig Additional topic-level configuration settings.
 */
public void createTopic(String topic,
                        int partitions,
                        int replication,
                        Properties topicConfig) {
  log.debug("Creating topic { name: {}, partitions: {}, replication: {}, config: {} }",
      topic, partitions, replication, topicConfig);
  // Note: You must initialize the ZkClient with ZKStringSerializer.  If you don't, then
  // registerTopic() 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(),
      DEFAULT_ZK_SESSION_TIMEOUT_MS,
      DEFAULT_ZK_CONNECTION_TIMEOUT_MS,
      ZKStringSerializer$.MODULE$);
  ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zookeeperConnect()), false);
  AdminUtils.createTopic(zkUtils, topic, partitions, replication, topicConfig, RackAwareMode.Enforced$.MODULE$);
  zkClient.close();
}
 
Example 4
Source File: KafkaTestEnvironmentImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteTestTopic(String topic) {
	ZkUtils zkUtils = getZkUtils();
	try {
		LOG.info("Deleting topic {}", topic);

		ZkClient zk = new ZkClient(zookeeperConnectionString, Integer.valueOf(standardProps.getProperty("zookeeper.session.timeout.ms")),
			Integer.valueOf(standardProps.getProperty("zookeeper.connection.timeout.ms")), new ZooKeeperStringSerializer());

		AdminUtils.deleteTopic(zkUtils, topic);

		zk.close();
	} finally {
		zkUtils.close();
	}
}
 
Example 5
Source File: KafkaTestEnvironmentImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public int getLeaderToShutDown(String topic) throws Exception {
	ZkClient zkClient = createZkClient();
	PartitionMetadata firstPart = null;
	do {
		if (firstPart != null) {
			LOG.info("Unable to find leader. error code {}", firstPart.errorCode());
			// not the first try. Sleep a bit
			Thread.sleep(150);
		}

		Seq<PartitionMetadata> partitionMetadata = AdminUtils.fetchTopicMetadataFromZk(topic, zkClient).partitionsMetadata();
		firstPart = partitionMetadata.head();
	}
	while (firstPart.errorCode() != 0);
	zkClient.close();

	return firstPart.leader().get().id();
}
 
Example 6
Source File: KafkaTool.java    From Scribengin with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Create a topic. This method will not create a topic that is currently scheduled for deletion.
 * For valid configs see https://cwiki.apache.org/confluence/display/KAFKA/Replication+tools#Replicationtools-Howtousethetool?.3
 *
 * @See https://kafka.apache.org/documentation.html#topic-config 
 * for more valid configs
 * */
public void createTopic(String[] args) throws Exception {
  int sessionTimeoutMs = 10000;
  int connectionTimeoutMs = 10000;

  TopicCommandOptions options = new TopicCommandOptions(args);
  ZkClient client = new ZkClient(zkConnects, sessionTimeoutMs, connectionTimeoutMs, ZKStringSerializer$.MODULE$);
  if (topicExits(name)) {
    TopicCommand.deleteTopic(client, options);
  }

  TopicCommand.createTopic(client, options);
  try {
    Thread.sleep(3000);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  client.close();
}
 
Example 7
Source File: ZookeeperCheckpointManagerTest.java    From uReplicator with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() {
  ZkStarter.startLocalZkServer();
  ZkClient zkClient = ZkUtils.createZkClient(ZkStarter.DEFAULT_ZK_STR, 1000, 1000);
  zkClient.createPersistent("/" + TestUtils.SRC_CLUSTER);
  zkClient.close();
  KafkaUReplicatorMetricsReporter
      .init(new MetricsReporterConf("dca1", new ArrayList<>(), "localhost", null, null));

  for (int i = 0; i < 50; i++) {
    offsetCommitMap
        .put(new TopicPartition(TEST_TOPIC_PREFIX + String.valueOf(i / 10), i % 10), i * 10l);
  }
}
 
Example 8
Source File: TollboothApp.java    From data-highway with Apache License 2.0 5 votes vote down vote up
private void checkAndCreateTopic(String zkConnect, String topic, int partitions, int replicas) {
  ZkClient zkClient = new ZkClient(zkConnect, SESSION_TIMEOUT_MS, CONNECTION_TIMEOUT_MS, ZKStringSerializer$.MODULE$);
  ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkConnect), IS_SECURE_KAFKA_CLUSTER);

  if (!AdminUtils.topicExists(zkUtils, topic)) {
    AdminUtils.createTopic(zkUtils, topic, partitions, replicas, new Properties(), RackAwareMode.Enforced$.MODULE$);
  }

  zkUtils.close();
  zkClient.close();
}
 
Example 9
Source File: ZookeeperUtils.java    From kafka-workers with Apache License 2.0 5 votes vote down vote up
public static void createTopics(String zookeeperUrl, int partitions, int replicas, String... topicNames) throws InterruptedException {

        ZkClient zkClient = ZkUtils.createZkClient(zookeeperUrl, SESSION_TIMEOUT_MS, CONNECTION_TIMEOUT_MS);
        ZkConnection zkConnection = new ZkConnection(zookeeperUrl);
        ZkUtils zkUtils = new ZkUtils(zkClient, zkConnection, false);

        for (String topicName : topicNames) {
            AdminUtils.createTopic(zkUtils, topicName, partitions, replicas, new Properties(), RackAwareMode.Enforced$.MODULE$);
        }

        zkUtils.close();
        zkConnection.close();
        zkClient.close();
    }
 
Example 10
Source File: DubboMockServer.java    From dubbo-mock with Apache License 2.0 5 votes vote down vote up
private void assertConnect(RegistryConfig registryConfig) {
	try {
		ZkClient zk = new ZkClient(registryConfig.getRegistryAddress(), registryConfig.getRegistryTimeout());
		zk.close();
	} catch (Exception e) {
		throw new RuntimeException("注册中心 " + registryConfig.getRegistryAddress() + "无法连接", e);
	}
}
 
Example 11
Source File: KafkaProducerServiceIntegrationTest.java    From vertx-kafka-service with Apache License 2.0 5 votes vote down vote up
private void createTopic(String topic) {
    final ZkClient zkClient = new ZkClient(zookeeper.getConnectString(), 30000, 30000, ZKStringSerializer$.MODULE$);
    final ZkConnection connection = new ZkConnection(zookeeper.getConnectString());
    final ZkUtils zkUtils = new ZkUtils(zkClient, connection, false);
    AdminUtils.createTopic(zkUtils, topic, 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);
    TestUtils.waitUntilMetadataIsPropagated(JavaConversions.asScalaBuffer(Lists.newArrayList(kafkaServer)), topic, 0, 10000);
    zkClient.close();
}
 
Example 12
Source File: MockKafka.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public void createTopic(String topic, int partition, int replication) {
    ZkClient zkClient = new ZkClient(zkConnection);
    ZkUtils zkUtils = new ZkUtils(zkClient, zkConnection, false);
    zkClient.setZkSerializer(new ZKStringSerializer());
    AdminUtils.createTopic(zkUtils, topic, partition, replication, new Properties(), null);
    zkClient.close();
}
 
Example 13
Source File: EmbeddedZookeeperTest.java    From li-apache-kafka-clients with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testSimpleScenario() throws Exception {
  String connectionString;
  String host;
  int port;
  try (EmbeddedZookeeper zk = new EmbeddedZookeeper()) {
    connectionString = zk.getConnectionString();
    host = zk.getHostAddress();
    port = zk.getPort();
    Assert.assertEquals(host + ":" + port, connectionString);
    ZkClient client = new ZkClient(connectionString);
    try {
      String path = "/" + UUID.randomUUID().toString();
      client.waitUntilConnected(5, TimeUnit.SECONDS);
      client.create(path, "payload", CreateMode.PERSISTENT);
      Assert.assertEquals("payload", client.readData(path));
    } finally {
      client.close();
    }
  }
  //now verify shut down
  try {
    new Socket(host, port);
    Assert.fail("expected to fail");
  } catch (ConnectException ignored) {

  }
}
 
Example 14
Source File: KafkaTestUtil.java    From siddhi-io-kafka with Apache License 2.0 5 votes vote down vote up
public static void deleteTopic(String connectionString, String topics[]) {
    ZkClient zkClient = new ZkClient(connectionString, 30000, 30000, ZKStringSerializer$.MODULE$);
    ZkConnection zkConnection = new ZkConnection(connectionString);
    ZkUtils zkUtils = new ZkUtils(zkClient, zkConnection, false);
    for (String topic : topics) {
        AdminUtils.deleteTopic(zkUtils, topic);
    }
    zkClient.close();
}
 
Example 15
Source File: KafkaTestUtil.java    From siddhi-io-kafka with Apache License 2.0 5 votes vote down vote up
public static void createTopic(String connectionString, String topics[], int numOfPartitions) {
    ZkClient zkClient = new ZkClient(connectionString, 30000, 30000, ZKStringSerializer$.MODULE$);
    ZkConnection zkConnection = new ZkConnection(connectionString);
    ZkUtils zkUtils = new ZkUtils(zkClient, zkConnection, false);
    for (String topic : topics) {
        try {
            AdminUtils.createTopic(zkUtils, topic, numOfPartitions, 1, new Properties(),
                    RackAwareMode.Enforced$.MODULE$);
        } catch (TopicExistsException e) {
            log.warn("topic exists for: " + topic);
        }
    }
    zkClient.close();
}
 
Example 16
Source File: EmbeddedKafka.java    From tajo with Apache License 2.0 5 votes vote down vote up
public void createTopic(int partitions, int replication, String topic) {
  checkState(started.get(), "not started!");

  ZkClient zkClient = new ZkClient(getZookeeperConnectString(), 30000, 30000, ZKStringSerializer$.MODULE$);
  try {
    AdminUtils.createTopic(ZkUtils.apply(zkClient, false), topic, partitions, replication, new Properties(),
        RackAwareMode.Enforced$.MODULE$);
  } finally {
    zkClient.close();
  }
}
 
Example 17
Source File: KafkaSourceGenerator.java    From Scribengin with GNU Affero General Public License v3.0 5 votes vote down vote up
public void createTopic(String topicName, int numOfReplication, int numPartitions) throws Exception {
  // Create a ZooKeeper client
  int sessionTimeoutMs = 1000;
  int connectionTimeoutMs = 1000;
  ZkClient zkClient = new ZkClient(zkConnect, sessionTimeoutMs, connectionTimeoutMs, ZKStringSerializer$.MODULE$);
  // Create a topic named "myTopic" with 8 partitions and a replication factor of 3
  Properties topicConfig = new Properties();
  AdminUtils.createTopic(zkClient, topicName, numPartitions, numOfReplication, topicConfig);
  Thread.sleep(3000);
  zkClient.close();
}
 
Example 18
Source File: KafkaTool.java    From Scribengin with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * This method works if cluster has "delete.topic.enable" = "true".
 * It can also be implemented by TopicCommand.deleteTopic which simply calls AdminUtils.delete 
 *
 * @param topicName
 * @throws Exception
 */
public void deleteTopic(String topicName) throws Exception {
  int sessionTimeoutMs = 10000;
  int connectionTimeoutMs = 10000;
  ZkClient zkClient = new ZkClient(zkConnects, sessionTimeoutMs, connectionTimeoutMs, ZKStringSerializer$.MODULE$);
  AdminUtils.deleteTopic(zkClient, topicName);
  zkClient.close();
}
 
Example 19
Source File: PistachiosFormatter.java    From Pistachio with Apache License 2.0 5 votes vote down vote up
private static void cleanup(ZKHelixAdmin admin, ZkClient zkClient, String[] hostList, int numPartitions, int numReplicas, String kafkaTopicPrefix, String kafkaZKPath) {
  try {
      // TODO, delete not supported until 0.8.1, we'll enable it later
      for (int i =0; i<numPartitions; i++) {
          //zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer);
          zkClient.deleteRecursive(ZkUtils.getTopicPath(kafkaTopicPrefix + i));
      }
      zkClient.close();
      //ZKHelixAdmin admin = new ZKHelixAdmin(args[1]);
      admin.dropCluster("PistachiosCluster");
    } catch(Exception e) {
        logger.info("error:", e);
    }
      logger.info("cleanup finished succeessfully");
}
 
Example 20
Source File: ZkUtil.java    From java-study with Apache License 2.0 4 votes vote down vote up
public static void close(ZkClient zk){
	if(zk!=null){
		zk.close();
	}
}