org.apache.hadoop.hbase.ZooKeeperConnectionException Java Examples

The following examples show how to use org.apache.hadoop.hbase.ZooKeeperConnectionException. 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: Merge.java    From examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
  // tag::MERGE1[]
  Configuration conf = HBaseConfiguration.create();
  Connection connection = ConnectionFactory.createConnection(conf);
  HBaseAdmin admin = (HBaseAdmin)connection.getAdmin();
  List<HRegionInfo> regions = admin.getTableRegions(TableName.valueOf("t1")); //<1>
  LOG.info("testtable contains " + regions.size() + " regions.");
  for (int index = 0; index < regions.size() / 2; index++) {
    HRegionInfo region1 = regions.get(index*2);
    HRegionInfo region2 = regions.get(index*2+1);
    LOG.info("Merging regions " + region1 + " and " + region2);
    admin.mergeRegions(region1.getEncodedNameAsBytes(), 
                       region2.getEncodedNameAsBytes(), false); //<2>
  }
  admin.close();
  // end::MERGE1[]
}
 
Example #2
Source File: ZooKeeperHelper.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Ensure passed zookeeper is connected.
 * @param timeout Time to wait on established Connection
 */
public static ZooKeeper ensureConnectedZooKeeper(ZooKeeper zookeeper, int timeout)
    throws ZooKeeperConnectionException {
  if (zookeeper.getState().isConnected()) {
    return zookeeper;
  }
  Stopwatch stopWatch = Stopwatch.createStarted();
  // Make sure we are connected before we hand it back.
  while(!zookeeper.getState().isConnected()) {
    Threads.sleep(1);
    if (stopWatch.elapsed(TimeUnit.MILLISECONDS) > timeout) {
      throw new ZooKeeperConnectionException("Failed connect after waiting " +
          stopWatch.elapsed(TimeUnit.MILLISECONDS) + "ms (zk session timeout); " +
          zookeeper);
    }
  }
  return zookeeper;
}
 
Example #3
Source File: Create2.java    From examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
  Configuration conf = HBaseConfiguration.create();
  HBaseAdmin admin = new HBaseAdmin(conf);
  // tag::CREATE2[]
  HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("pages"));
  byte[][] splits = {Bytes.toBytes("b"), Bytes.toBytes("f"),
    Bytes.toBytes("k"), Bytes.toBytes("n"), Bytes.toBytes("t")};
  desc.setValue(Bytes.toBytes("comment"), Bytes.toBytes("Create 10012014"));
  HColumnDescriptor family = new HColumnDescriptor("c");
  family.setCompressionType(Algorithm.GZ);
  family.setMaxVersions(52);
  family.setBloomFilterType(BloomType.ROW);
  desc.addFamily(family);
  admin.createTable(desc, splits);
  // end::CREATE2[]
  admin.close();
}
 
Example #4
Source File: TestMasterNoCluster.java    From hbase with Apache License 2.0 6 votes vote down vote up
@After
public void tearDown()
throws KeeperException, ZooKeeperConnectionException, IOException {
  // Make sure zk is clean before we run the next test.
  ZKWatcher zkw = new ZKWatcher(TESTUTIL.getConfiguration(),
      "@Before", new Abortable() {
    @Override
    public void abort(String why, Throwable e) {
      throw new RuntimeException(why, e);
    }

    @Override
    public boolean isAborted() {
      return false;
    }
  });
  ZKUtil.deleteNodeRecursively(zkw, zkw.getZNodePaths().baseZNode);
  zkw.close();
}
 
Example #5
Source File: Describe.java    From examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
  // Instantiate default HBase configuration object.
  // Configuration file must be in the classpath
  Configuration conf = HBaseConfiguration.create();
  // tag::DESCRIBE
  HBaseAdmin admin = new HBaseAdmin(conf);
  HTableDescriptor desc = admin.getTableDescriptor(TableName.valueOf("crc"));
  Collection<HColumnDescriptor> families = desc.getFamilies();
  System.out.println("Table " + desc.getTableName() + " has " + families.size() + " family(ies)");
  for (Iterator<HColumnDescriptor> iterator = families.iterator(); iterator.hasNext();) {
    HColumnDescriptor family = iterator.next();
    System.out.println("Family details: " + family);
  }
  // end::DESCRIBE
  admin.close();
}
 
Example #6
Source File: CreateTable.java    From examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MasterNotRunningException,
    ZooKeeperConnectionException, IOException {
  try (Connection connection = ConnectionFactory.createConnection();
      Admin admin = connection.getAdmin();) {
    LOG.info("Starting table creation");
    // tag::CREATE[]
    TableName documents = TableName.valueOf("documents");
    HTableDescriptor desc = new HTableDescriptor(documents);
    HColumnDescriptor family = new HColumnDescriptor("c");
    family.setCompressionType(Algorithm.GZ);
    family.setBloomFilterType(BloomType.NONE);
    desc.addFamily(family);
    UniformSplit uniformSplit = new UniformSplit();
    admin.createTable(desc, uniformSplit.split(8));
    // end::CREATE[]
    LOG.info("Table successfuly created");
  }
}
 
Example #7
Source File: HBaseAdminMultiCluster.java    From HBase.MCC with Apache License 2.0 6 votes vote down vote up
public HBaseAdminMultiCluster(Configuration c)
    throws MasterNotRunningException, ZooKeeperConnectionException,
    IOException {
  super(HBaseMultiClusterConfigUtil.splitMultiConfigFile(c).get(
      HBaseMultiClusterConfigUtil.PRIMARY_NAME));

  Map<String, Configuration> configs = HBaseMultiClusterConfigUtil
      .splitMultiConfigFile(c);

  for (Entry<String, Configuration> entry : configs.entrySet()) {

    if (!entry.getKey().equals(HBaseMultiClusterConfigUtil.PRIMARY_NAME)) {
      HBaseAdmin admin = new HBaseAdmin(entry.getValue());
      LOG.info("creating HBaseAdmin for : " + entry.getKey());
      failoverAdminMap.put(entry.getKey(), admin);
      LOG.info(" - successfully creating HBaseAdmin for : " + entry.getKey());
    }
  }
  LOG.info("Successful loaded all HBaseAdmins");

}
 
Example #8
Source File: HBasePool.java    From DistributedCrawler with Apache License 2.0 5 votes vote down vote up
private HBasePool() throws ZooKeeperConnectionException {
	Configuration conf = new Configuration();
	conf.set("hbase.zookeeper.property.clientPort", "2181");
	conf.set("hbase.zookeeper.quorum", "gs-pc");
	conf.set("hbase.master", "gs-pc:60000");
	this.conf = conf;
	connection = HConnectionManager.createConnection(conf);
}
 
Example #9
Source File: HBasePool.java    From DistributedCrawler with Apache License 2.0 5 votes vote down vote up
protected synchronized static HBasePool getInstance()
		throws ZooKeeperConnectionException {
	if (instance == null) {
		instance = new HBasePool();
	}
	return instance;
}
 
Example #10
Source File: HBaseClient.java    From hbase-tools with Apache License 2.0 5 votes vote down vote up
private static void validateAuthentication() throws ZooKeeperConnectionException {
    try {
        // Is there something better?
        admin.isMasterRunning();
    } catch (MasterNotRunningException e) {
        System.out.println("Maybe you are connecting to the secured cluster without kerberos config.\n");
    }
}
 
Example #11
Source File: TestHBaseDAO.java    From DistributedCrawler with Apache License 2.0 5 votes vote down vote up
@Test
public void testSave() throws ZooKeeperConnectionException, IOException, SQLException{
	PagePOJO pojo = new PagePOJO("http://haha",3,"XIXIX","ppppp");
	PageDAO dao = new PageDAOHBaseImpl("page");
	dao.save(pojo);
	dao.close();
}
 
Example #12
Source File: Create4.java    From examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
  Configuration conf = HBaseConfiguration.create();
  HBaseAdmin admin = new HBaseAdmin(conf);
  // tag::CREATE4[]
  HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("access"));
  HColumnDescriptor family = new HColumnDescriptor("d");
  family.setValue("comment", "Last user access date");
  family.setMaxVersions(10);
  family.setMinVersions(2);
  family.setTimeToLive(2678400);
  desc.addFamily(family);
  admin.createTable(desc);
  // end::CREATE4[]
  admin.close();
}
 
Example #13
Source File: ZkUtils.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Gets a direct interface to a ZooKeeper instance.
 *
 * @return a direct interface to ZooKeeper.
 */
public static RecoverableZooKeeper getRecoverableZooKeeper(){
    try{
        return zkManager.getRecoverableZooKeeper();
    }catch(ZooKeeperConnectionException e){
        LOG.error("Unable to connect to zookeeper, aborting",e);
        throw new RuntimeException(e);
    }
}
 
Example #14
Source File: Create3.java    From examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
  Configuration conf = HBaseConfiguration.create();
  HBaseAdmin admin = new HBaseAdmin(conf);
  // tag::CREATE3[]
  HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("crc"));
  desc.setMaxFileSize((long)20*1024*1024*1024);
  desc.setConfiguration("hbase.hstore.compaction.min", "5");
  HColumnDescriptor family = new HColumnDescriptor("c");
  family.setInMemory(true);
  desc.addFamily(family);
  UniformSplit uniformSplit = new UniformSplit();
  admin.createTable(desc, uniformSplit.split(64));
  // end::CREATE3[]
  admin.close();
}
 
Example #15
Source File: Create1.java    From examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)
  throws MasterNotRunningException, ZooKeeperConnectionException,
         IOException {
  // tag::CREATE1[]
  Configuration conf = HBaseConfiguration.create();
  HBaseAdmin admin = new HBaseAdmin(conf);
  HTableDescriptor desc = 
    new HTableDescriptor(TableName.valueOf("testtable_create1"));
  HColumnDescriptor family = new HColumnDescriptor("f1");
  desc.addFamily(family);
  admin.createTable(desc);
  // end::CREATE1[]
  admin.close();
}
 
Example #16
Source File: ZkUtils.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @return direct interface to a ZooKeeperWatcher
 */
public static ZKWatcher getZooKeeperWatcher(){
    try{
        return zkManager.getZooKeeperWatcher();
    }catch(ZooKeeperConnectionException e){
        LOG.error("Unable to connect to zookeeper, aborting",e);
        throw new RuntimeException(e);
    }
}
 
Example #17
Source File: ZKWatcher.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void createBaseZNodes() throws ZooKeeperConnectionException {
  try {
    // Create all the necessary "directories" of znodes
    ZKUtil.createWithParents(this, znodePaths.baseZNode);
    ZKUtil.createAndFailSilent(this, znodePaths.rsZNode);
    ZKUtil.createAndFailSilent(this, znodePaths.drainingZNode);
    ZKUtil.createAndFailSilent(this, znodePaths.tableZNode);
    ZKUtil.createAndFailSilent(this, znodePaths.splitLogZNode);
    ZKUtil.createAndFailSilent(this, znodePaths.backupMasterAddressesZNode);
    ZKUtil.createAndFailSilent(this, znodePaths.masterMaintZNode);
  } catch (KeeperException e) {
    throw new ZooKeeperConnectionException(
        prefix("Unexpected KeeperException creating base node"), e);
  }
}
 
Example #18
Source File: SpliceZooKeeperManager.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public <T> T execute(Command<T> command) throws InterruptedException, KeeperException{
    try{
        return command.execute(getRecoverableZooKeeper());
    }catch(ZooKeeperConnectionException e){
        throw new KeeperException.SessionExpiredException();
    }
}
 
Example #19
Source File: MockServer.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @param htu Testing utility to use
 * @param zkw If true, create a zkw.
 * @throws ZooKeeperConnectionException
 * @throws IOException
 */
public MockServer(final HBaseTestingUtility htu, final boolean zkw)
throws ZooKeeperConnectionException, IOException {
  this.htu = htu;
  this.zk = zkw?
    new ZKWatcher(htu.getConfiguration(), NAME.toString(), this, true):
    null;
}
 
Example #20
Source File: HBaseClient.java    From hbase-tools with Apache License 2.0 5 votes vote down vote up
private static void validateAuthentication() throws ZooKeeperConnectionException {
    try {
        // Is there something better?
        admin.isMasterRunning();
    } catch (MasterNotRunningException e) {
        System.out.println("Maybe you are connecting to the secured cluster without kerberos config.\n");
    }
}
 
Example #21
Source File: HBaseClient.java    From hbase-tools with Apache License 2.0 5 votes vote down vote up
private static void validateAuthentication() throws ZooKeeperConnectionException {
    try {
        // Is there something better?
        admin.isMasterRunning();
    } catch (MasterNotRunningException e) {
        System.out.println("Maybe you are connecting to the secured cluster without kerberos config.\n");
    }
}
 
Example #22
Source File: MockRegionServer.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @param sn Name of this mock regionserver
 * @throws IOException
 * @throws org.apache.hadoop.hbase.ZooKeeperConnectionException
 */
MockRegionServer(final Configuration conf, final ServerName sn)
throws ZooKeeperConnectionException, IOException {
  this.sn = sn;
  this.conf = conf;
  this.zkw = new ZKWatcher(conf, sn.toString(), this, true);
}
 
Example #23
Source File: HBaseClient.java    From hbase-tools with Apache License 2.0 5 votes vote down vote up
private static void validateAuthentication() throws ZooKeeperConnectionException {
    try {
        // Is there something better?
        admin.isMasterRunning();
    } catch (MasterNotRunningException e) {
        System.out.println("Maybe you are connecting to the secured cluster without kerberos config.\n");
    }
}
 
Example #24
Source File: HBaseClient.java    From hbase-tools with Apache License 2.0 5 votes vote down vote up
private static void validateAuthentication() throws ZooKeeperConnectionException {
    try {
        // Is there something better?
        admin.isMasterRunning();
    } catch (MasterNotRunningException e) {
        System.out.println("Maybe you are connecting to the secured cluster without kerberos config.\n");
    }
}
 
Example #25
Source File: HFileArchiveManager.java    From hbase with Apache License 2.0 5 votes vote down vote up
public HFileArchiveManager(Connection connection, Configuration conf)
    throws ZooKeeperConnectionException, IOException {
  this.zooKeeper = new ZKWatcher(conf, "hfileArchiveManager-on-" + connection.toString(),
      connection);
  this.archiveZnode = ZKTableArchiveClient.getArchiveZNode(this.zooKeeper.getConfiguration(),
    this.zooKeeper);
}
 
Example #26
Source File: PcapScannerHBaseImpl.java    From opensoc-streaming with Apache License 2.0 4 votes vote down vote up
public byte[] getPcaps(String startKey, String endKey, long maxResultSize,
    long startTime, long endTime) throws IOException {
  Assert.hasText(startKey, "startKey must no be null or empty");
  byte[] cf = Bytes.toBytes(ConfigurationUtil.getConfiguration()
      .getString("hbase.table.column.family"));
  byte[] cq = Bytes.toBytes(ConfigurationUtil.getConfiguration()
      .getString("hbase.table.column.qualifier"));
  // create scan request
  Scan scan = createScanRequest(cf, cq, startKey, endKey, maxResultSize,
      startTime, endTime);
  List<byte[]> pcaps = new ArrayList<byte[]>();
  HTable table = null;
  try {
    pcaps = scanPcaps(pcaps, table, scan, cf, cq);
  } catch (IOException e) {
    LOGGER.error(
        "Exception occurred while fetching Pcaps for the key range : startKey="
            + startKey + ", endKey=" + endKey, e);
    if (e instanceof ZooKeeperConnectionException
        || e instanceof MasterNotRunningException
        || e instanceof NoServerForRegionException) {
      int maxRetryLimit = getConnectionRetryLimit();
      for (int attempt = 1; attempt <= maxRetryLimit; attempt++) {
        try {
          HBaseConfigurationUtil.closeConnection(); // closing the existing
                                                    // connection and retry,
                                                    // it will create a new
                                                    // HConnection
          pcaps = scanPcaps(pcaps, table, scan, cf, cq);
          break;
        } catch (IOException ie) {
          if (attempt == maxRetryLimit) {
            System.out.println("Throwing the exception after retrying "
                + maxRetryLimit + " times.");
            throw e;
          }
        }
      }
    } else {
      throw e;
    }
  } finally {
    if (table != null) {
      table.close();
    }
  }
  if (pcaps.size() == 1) {
    return pcaps.get(0);
  }
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  PcapMerger.merge(baos, pcaps);
  byte[] response = baos.toByteArray();
  return response;
}
 
Example #27
Source File: TransactionManager.java    From hbase-secondary-index with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param transactionLogger
 * @param conf
 * @throws ZooKeeperConnectionException
 */
public TransactionManager(final TransactionLogger transactionLogger, final Configuration conf)
        throws ZooKeeperConnectionException {
    this.transactionLogger = transactionLogger;
    connection = HConnectionManager.getConnection(conf);
}
 
Example #28
Source File: SpliceZooKeeperManager.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
public <T> T executeUnlessExpired(Command<T> command) throws InterruptedException, KeeperException{
    /*
     * What actually happens is that, in the event of a long network partition, ZooKeeper will throw
     * ConnectionLoss exceptions, but it will NOT throw a SessionExpired exception until it reconnects, even
     * if it's been disconnected for CLEARLY longer than the session timeout.
     *
     * To deal with this, we have to basically loop through our command repeatedly until we either
     *
     * 1. Succeed.
     * 2. Get a SessionExpired event from ZooKeeper
     * 3. Spent more than 2*sessionTimeout ms attempting the request
     * 4. Get some other kind of Zk error (NoNode, etc).
     */
    RecoverableZooKeeper rzk;
    try{
        rzk=getRecoverableZooKeeper();
    }catch(ZooKeeperConnectionException e){
        throw new KeeperException.SessionExpiredException();
    }
    //multiple by 2 to make absolutely certain we're timed out.
    int sessionTimeout=2*rzk.getZooKeeper().getSessionTimeout();
    long nextTime=System.currentTimeMillis();
    long startTime=System.currentTimeMillis();
    while((int)(nextTime-startTime)<sessionTimeout){
        try{
            return command.execute(rzk);
        }catch(KeeperException ke){
            switch(ke.code()){
                case CONNECTIONLOSS:
                case OPERATIONTIMEOUT:
                    LOG.warn("Detected a Connection issue("+ke.code()+") with ZooKeeper, retrying");
                    nextTime=System.currentTimeMillis();
                    break;
                default:
                    throw ke;
            }
        }
    }

    //we've run out of time, our session has almost certainly expired. Give up and explode
    throw new KeeperException.SessionExpiredException();
}
 
Example #29
Source File: SpliceZooKeeperManager.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
public RecoverableZooKeeper getRecoverableZooKeeper() throws ZooKeeperConnectionException{
    return rzk;
}
 
Example #30
Source File: SpliceZooKeeperManager.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
public ZKWatcher getZooKeeperWatcher() throws ZooKeeperConnectionException{
    return watcher;
}