Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
package org.apache.hive.jdbc;
public class ZooKeeperHiveClientHelper {
  public static final Log LOG = LogFactory.getLog(ZooKeeperHiveClientHelper.class.getName());

A no-op watcher class
  public static class DummyWatcher implements Watcher {
    public void process(org.apache.zookeeper.WatchedEvent event) {

Resolve to a host:port by connecting to ZooKeeper and picking a host randomly.

      throws ZooKeeperHiveClientException {
    String zooKeeperEnsemble = connParams.getZooKeeperEnsemble();
    String zooKeeperNamespace =
    if ((zooKeeperNamespace == null) || (zooKeeperNamespace.isEmpty())) {
      zooKeeperNamespace = .;
    Random randomizer = new Random();
    String serverNode;
    CuratorFramework zooKeeperClient =
            .retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();
    try {
      serverHosts = zooKeeperClient.getChildren().forPath("/" + zooKeeperNamespace);
      // Remove the znodes we've already tried from this list
      if (serverHosts.isEmpty()) {
        throw new ZooKeeperHiveClientException(
            "Tried all existing HiveServer2 uris from ZooKeeper.");
      // Now pick a host randomly
      serverNode = serverHosts.get(randomizer.nextInt(serverHosts.size()));
      String serverUri =
          new String(
              zooKeeperClient.getData().forPath("/" + zooKeeperNamespace + "/" + serverNode),
      .info("Selected HiveServer2 instance with uri: " + serverUri);
      return serverUri;
    } catch (Exception e) {
      throw new ZooKeeperHiveClientException("Unable to read HiveServer2 uri from ZooKeeper"e);
    } finally {
      // Close the client connection with ZooKeeper
      if (zooKeeperClient != null) {
New to GrepCode? Check out our FAQ X