Java 类org.apache.hadoop.hbase.client.Admin 实例源码

项目:ditb    文件:TestOpenTableInCoprocessor.java   
private void runCoprocessorConnectionToRemoteTable(Class<? extends BaseRegionObserver> clazz,
    boolean[] completeCheck) throws Throwable {
  HTableDescriptor primary = new HTableDescriptor(primaryTable);
  primary.addFamily(new HColumnDescriptor(family));
  // add our coprocessor
  primary.addCoprocessor(clazz.getName());

  HTableDescriptor other = new HTableDescriptor(otherTable);
  other.addFamily(new HColumnDescriptor(family));


  Admin admin = UTIL.getHBaseAdmin();
  admin.createTable(primary);
  admin.createTable(other);

  Table table = new HTable(UTIL.getConfiguration(), TableName.valueOf("primary"));
  Put p = new Put(new byte[] { 'a' });
  p.add(family, null, new byte[] { 'a' });
  table.put(p);
  table.close();

  Table target = new HTable(UTIL.getConfiguration(), otherTable);
  assertTrue("Didn't complete update to target table!", completeCheck[0]);
  assertEquals("Didn't find inserted row", 1, getKeyValueCount(target));
  target.close();
}
项目:mumu-hbase    文件:HBaseTableOperation.java   
/**
 * 创建表
 *
 * @param tableName 表名称
 * @param columns   列族名称
 */
public void createTable(String tableName, String... columns) {
    HBaseConfiguration hBaseConfiguration = new HBaseConfiguration();
    Admin admin = hBaseConfiguration.admin();
    try {
        TableName tn = TableName.valueOf(tableName);
        if (admin.tableExists(tn)) {
            log.info("表名【" + tableName + "】已存在");
            return;
        }
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tn);
        for (String column : columns) {
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(column);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        admin.createTable(hTableDescriptor);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        hBaseConfiguration.close();
    }
}
项目:mumu-hbase    文件:HBaseTableOperation.java   
/**
 * 激活表
 *
 * @param tableName
 */
public void enableTable(String tableName) {
    HBaseConfiguration hBaseConfiguration = new HBaseConfiguration();
    Admin admin = hBaseConfiguration.admin();
    TableName tn = TableName.valueOf(tableName);
    try {
        if (admin.tableExists(tn)) {
            admin.enableTable(tn);
        } else {
            log.info("表名【" + tableName + "】不存在");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        hBaseConfiguration.close();
    }
}
项目:mumu-hbase    文件:HBaseTableOperation.java   
/**
 * 往表中添加列族
 *
 * @param tableName  表名
 * @param familyName 列族名
 */
public void addColumn(String tableName, String familyName) {
    HBaseConfiguration hBaseConfiguration = new HBaseConfiguration();
    Admin admin = hBaseConfiguration.admin();
    TableName tb = TableName.valueOf(tableName);
    try {
        if (admin.tableExists(tb)) {
            HColumnDescriptor columnDescriptor = new HColumnDescriptor(familyName);

            columnDescriptor.setMaxVersions(1);//设置列族保留的最多版本
            columnDescriptor.setCompressionType(Compression.Algorithm.GZ);//设置压缩算法
            columnDescriptor.setCompactionCompressionType(Compression.Algorithm.GZ);//合并压缩算法

            admin.addColumn(tb, columnDescriptor);
        } else {
            log.info("表名【" + tableName + "】不存在");
        }
    } catch (IOException e) {
        log.error(e);
    } finally {
        hBaseConfiguration.close();
    }
}
项目:ditb    文件:TestTableDescriptorModification.java   
@Test
public void testDeleteColumn() throws IOException {
  Admin admin = TEST_UTIL.getHBaseAdmin();
  // Create a table with two families
  HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
  baseHtd.addFamily(new HColumnDescriptor(FAMILY_0));
  baseHtd.addFamily(new HColumnDescriptor(FAMILY_1));
  admin.createTable(baseHtd);
  admin.disableTable(TABLE_NAME);
  try {
    // Verify the table descriptor
    verifyTableDescriptor(TABLE_NAME, FAMILY_0, FAMILY_1);

    // Modify the table removing one family and verify the descriptor
    admin.deleteColumn(TABLE_NAME, FAMILY_1);
    verifyTableDescriptor(TABLE_NAME, FAMILY_0);
  } finally {
    admin.deleteTable(TABLE_NAME);
  }
}
项目:ditb    文件:TestRegionSizeCalculator.java   
/**
 * When size of region in megabytes is larger than largest possible integer there could be
 * error caused by lost of precision.
 * */
@Test
public void testLargeRegion() throws Exception {

  RegionLocator regionLocator = mockRegionLocator("largeRegion");

  Admin admin = mockAdmin(
    mockServer(
      mockRegion("largeRegion", Integer.MAX_VALUE)
    )
  );

  RegionSizeCalculator calculator = new RegionSizeCalculator(regionLocator, admin);

  assertEquals(((long) Integer.MAX_VALUE) * megabyte, calculator.getRegionSize("largeRegion".getBytes()));
}
项目:ditb    文件:Canary.java   
private static List<Future<Void>> sniff(final Admin admin, final Sink sink,
    HTableDescriptor tableDesc, ExecutorService executor, TaskType taskType) throws Exception {
  Table table = null;
  try {
    table = admin.getConnection().getTable(tableDesc.getTableName());
  } catch (TableNotFoundException e) {
    return new ArrayList<Future<Void>>();
  }
  List<RegionTask> tasks = new ArrayList<RegionTask>();
  try {
    for (HRegionInfo region : admin.getTableRegions(tableDesc.getTableName())) {
      tasks.add(new RegionTask(admin.getConnection(), region, sink, taskType));
    }
  } finally {
    table.close();
  }
  return executor.invokeAll(tasks);
}
项目:ditb    文件:TestNamespacesInstanceResource.java   
@Test
public void testCannotDeleteDefaultAndHbaseNamespaces() throws IOException {
  String defaultPath = "/namespaces/default";
  String hbasePath = "/namespaces/hbase";
  Response response;

  // Check that doesn't exist via non-REST call.
  Admin admin = TEST_UTIL.getHBaseAdmin();
  assertNotNull(findNamespace(admin, "default"));
  assertNotNull(findNamespace(admin, "hbase"));

  // Try (but fail) to delete namespaces via REST.
  response = client.delete(defaultPath);
  assertEquals(503, response.getCode());
  response = client.delete(hbasePath);
  assertEquals(503, response.getCode());

  assertNotNull(findNamespace(admin, "default"));
  assertNotNull(findNamespace(admin, "hbase"));
}
项目:ditb    文件:TestClassLoading.java   
@Test
// HBASE-3516: Test CP Class loading from local file system
public void testClassLoadingFromLocalFS() throws Exception {
  File jarFile = buildCoprocessorJar(cpName3);

  // create a table that references the jar
  HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(cpName3));
  htd.addFamily(new HColumnDescriptor("test"));
  htd.setValue("COPROCESSOR$1", getLocalPath(jarFile) + "|" + cpName3 + "|" +
    Coprocessor.PRIORITY_USER);
  Admin admin = TEST_UTIL.getHBaseAdmin();
  admin.createTable(htd);
  waitForTable(htd.getTableName());

  // verify that the coprocessor was loaded
  boolean found = false;
  MiniHBaseCluster hbase = TEST_UTIL.getHBaseCluster();
  for (Region region: hbase.getRegionServer(0).getOnlineRegionsLocalContext()) {
    if (region.getRegionInfo().getRegionNameAsString().startsWith(cpName3)) {
      found = (region.getCoprocessorHost().findCoprocessor(cpName3) != null);
    }
  }
  assertTrue("Class " + cpName3 + " was missing on a region", found);
}
项目:ditb    文件:CreateSnapshot.java   
@Override
protected int doWork() throws Exception {
    Connection connection = null;
    Admin admin = null;
    try {
        connection = ConnectionFactory.createConnection(getConf());
        admin = connection.getAdmin();
        HBaseProtos.SnapshotDescription.Type type = HBaseProtos.SnapshotDescription.Type.FLUSH;
        if (snapshotType != null) {
            type = HBaseProtos.SnapshotDescription.Type.valueOf(snapshotName.toUpperCase());
        }

        admin.snapshot(snapshotName, TableName.valueOf(tableName), type);
    } catch (Exception e) {
        return -1;
    } finally {
        if (admin != null) {
            admin.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
    return 0;
}
项目:ditb    文件:SnapshotTestingUtils.java   
/**
 * Make sure that there is only one snapshot returned from the master and its
 * name and table match the passed in parameters.
 */
public static List<SnapshotDescription> assertExistsMatchingSnapshot(
    Admin admin, String snapshotName, TableName tableName)
    throws IOException {
  // list the snapshot
  List<SnapshotDescription> snapshots = admin.listSnapshots();

  List<SnapshotDescription> returnedSnapshots = new ArrayList<SnapshotDescription>();
  for (SnapshotDescription sd : snapshots) {
    if (snapshotName.equals(sd.getName()) &&
        tableName.equals(TableName.valueOf(sd.getTable()))) {
      returnedSnapshots.add(sd);
    }
  }

  Assert.assertTrue("No matching snapshots found.", returnedSnapshots.size()>0);
  return returnedSnapshots;
}
项目:ditb    文件:IntegrationTestBigLinkedListWithVisibility.java   
private void createTable(Admin admin, TableName tableName, boolean setVersion,
    boolean acl) throws IOException {
  if (!admin.tableExists(tableName)) {
    HTableDescriptor htd = new HTableDescriptor(tableName);
    HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME);
    if (setVersion) {
      family.setMaxVersions(DEFAULT_TABLES_COUNT);
    }
    htd.addFamily(family);
    admin.createTable(htd);
    if (acl) {
      LOG.info("Granting permissions for user " + USER.getShortName());
      Permission.Action[] actions = { Permission.Action.READ };
      try {
        AccessControlClient.grant(ConnectionFactory.createConnection(getConf()), tableName,
            USER.getShortName(), null, null, actions);
      } catch (Throwable e) {
        LOG.fatal("Error in granting permission for the user " + USER.getShortName(), e);
        throw new IOException(e);
      }
    }
  }
}
项目:ditb    文件:HBaseFsckRepair.java   
public static void waitUntilAssigned(Admin admin,
    HRegionInfo region) throws IOException, InterruptedException {
  long timeout = admin.getConfiguration().getLong("hbase.hbck.assign.timeout", 120000);
  long expiration = timeout + EnvironmentEdgeManager.currentTime();
  while (EnvironmentEdgeManager.currentTime() < expiration) {
    try {
      Map<String, RegionState> rits=
          admin.getClusterStatus().getRegionsInTransition();

      if (rits.keySet() != null && !rits.keySet().contains(region.getEncodedName())) {
        // yay! no longer RIT
        return;
      }
      // still in rit
      LOG.info("Region still in transition, waiting for "
          + "it to become assigned: " + region);
    } catch (IOException e) {
      LOG.warn("Exception when waiting for region to become assigned,"
          + " retrying", e);
    }
    Thread.sleep(1000);
  }
  throw new IOException("Region " + region + " failed to move out of " +
      "transition within timeout " + timeout + "ms");
}
项目:ditb    文件:SecureTestUtil.java   
public static void createTable(HBaseTestingUtility testUtil, Admin admin, HTableDescriptor htd,
    byte[][] splitKeys) throws Exception {
  // NOTE: We need a latch because admin is not sync,
  // so the postOp coprocessor method may be called after the admin operation returned.
  MasterSyncObserver observer = (MasterSyncObserver)testUtil.getHBaseCluster().getMaster()
    .getMasterCoprocessorHost().findCoprocessor(MasterSyncObserver.class.getName());
  observer.tableCreationLatch = new CountDownLatch(1);
  if (splitKeys != null) {
    admin.createTable(htd, splitKeys);
  } else {
    admin.createTable(htd);
  }
  observer.tableCreationLatch.await();
  observer.tableCreationLatch = null;
  testUtil.waitUntilAllRegionsAssigned(htd.getTableName());
}
项目:ditb    文件:SecureTestUtil.java   
public static void deleteTable(HBaseTestingUtility testUtil, Admin admin, TableName tableName)
    throws Exception {
  // NOTE: We need a latch because admin is not sync,
  // so the postOp coprocessor method may be called after the admin operation returned.
  MasterSyncObserver observer = (MasterSyncObserver)testUtil.getHBaseCluster().getMaster()
    .getMasterCoprocessorHost().findCoprocessor(MasterSyncObserver.class.getName());
  observer.tableDeletionLatch = new CountDownLatch(1);
  try {
    admin.disableTable(tableName);
  } catch (TableNotEnabledException e) {
    LOG.debug("Table: " + tableName + " already disabled, so just deleting it.");
  }
  admin.deleteTable(tableName);
  observer.tableDeletionLatch.await();
  observer.tableDeletionLatch = null;
}
项目:ditb    文件:TestTableScan.java   
@BeforeClass
public static void setUpBeforeClass() throws Exception {
  conf = TEST_UTIL.getConfiguration();
  conf.set(Constants.CUSTOM_FILTERS, "CustomFilter:" + CustomFilter.class.getName()); 
  TEST_UTIL.startMiniCluster();
  REST_TEST_UTIL.startServletContainer(conf);
  client = new Client(new Cluster().add("localhost",
    REST_TEST_UTIL.getServletPort()));
  Admin admin = TEST_UTIL.getHBaseAdmin();
  if (!admin.tableExists(TABLE)) {
  HTableDescriptor htd = new HTableDescriptor(TABLE);
  htd.addFamily(new HColumnDescriptor(CFA));
  htd.addFamily(new HColumnDescriptor(CFB));
  admin.createTable(htd);
  expectedRows1 = TestScannerResource.insertData(conf, TABLE, COLUMN_1, 1.0);
  expectedRows2 = TestScannerResource.insertData(conf, TABLE, COLUMN_2, 0.5);
  }
}
项目:ditb    文件:SnapshotTestingUtils.java   
public static void snapshot(Admin admin,
    final String snapshotName, final String tableName,
    SnapshotDescription.Type type, int numTries) throws IOException {
  int tries = 0;
  CorruptedSnapshotException lastEx = null;
  while (tries++ < numTries) {
    try {
      admin.snapshot(snapshotName, TableName.valueOf(tableName), type);
      return;
    } catch (CorruptedSnapshotException cse) {
      LOG.warn("Got CorruptedSnapshotException", cse);
      lastEx = cse;
    }
  }
  throw lastEx;
}
项目:ditb    文件:TestMiniClusterLoadSequential.java   
protected void prepareForLoadTest() throws IOException {
  LOG.info("Starting load test: dataBlockEncoding=" + dataBlockEncoding +
      ", isMultiPut=" + isMultiPut);
  numKeys = numKeys();
  Admin admin = new HBaseAdmin(conf);
  while (admin.getClusterStatus().getServers().size() < NUM_RS) {
    LOG.info("Sleeping until " + NUM_RS + " RSs are online");
    Threads.sleepWithoutInterrupt(1000);
  }
  admin.close();

  HTableDescriptor htd = new HTableDescriptor(TABLE);
  HColumnDescriptor hcd = new HColumnDescriptor(CF)
    .setCompressionType(compression)
    .setDataBlockEncoding(dataBlockEncoding);
  createPreSplitLoadTestTable(htd, hcd);

  LoadTestDataGenerator dataGen = new MultiThreadedAction.DefaultDataGenerator(CF);
  writerThreads = prepareWriterThreads(dataGen, conf, TABLE);
  readerThreads = prepareReaderThreads(dataGen, conf, TABLE, 100);
}
项目:ditb    文件:TestAccessController.java   
@Test (timeout=180000)
public void testTableNameEnumeration() throws Exception {
  AccessTestAction listTablesAction = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      Connection unmanagedConnection =
          ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
      Admin admin = unmanagedConnection.getAdmin();
      try {
        return Arrays.asList(admin.listTableNames());
      } finally {
        admin.close();
        unmanagedConnection.close();
      }
    }
  };

  verifyAllowed(listTablesAction, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_RW,
    USER_RO, USER_GROUP_CREATE, USER_GROUP_ADMIN, USER_GROUP_READ, USER_GROUP_WRITE);
  verifyIfEmptyList(listTablesAction, USER_NONE);
}
项目:ditb    文件:TestHColumnDescriptorDefaultVersions.java   
@Test
public void testCreateTableWithDefaultFromConf() throws Exception {
  TEST_UTIL.shutdownMiniCluster();
  TEST_UTIL.getConfiguration().setInt("hbase.column.max.version", 3);
  TEST_UTIL.startMiniCluster(1);

  Admin admin = TEST_UTIL.getHBaseAdmin();
  // Create a table with one family
  HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
  HColumnDescriptor hcd = new HColumnDescriptor(FAMILY);
  hcd.setMaxVersions(TEST_UTIL.getConfiguration().getInt("hbase.column.max.version", 1));
  baseHtd.addFamily(hcd);
  admin.createTable(baseHtd);
  admin.disableTable(TABLE_NAME);
  try {
    // Verify the column descriptor
    verifyHColumnDescriptor(3, TABLE_NAME, FAMILY);
  } finally {
    admin.deleteTable(TABLE_NAME);
  }
}
项目:ditb    文件:TestHColumnDescriptorDefaultVersions.java   
@Test
public void testCreateTableWithSetVersion() throws Exception {
  TEST_UTIL.shutdownMiniCluster();
  TEST_UTIL.getConfiguration().setInt("hbase.column.max.version", 3);
  TEST_UTIL.startMiniCluster(1);

  Admin admin = TEST_UTIL.getHBaseAdmin();
  // Create a table with one family
  HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
  HColumnDescriptor hcd =
      new HColumnDescriptor(FAMILY, 5, HColumnDescriptor.DEFAULT_COMPRESSION,
          HColumnDescriptor.DEFAULT_IN_MEMORY, HColumnDescriptor.DEFAULT_BLOCKCACHE,
          HColumnDescriptor.DEFAULT_TTL, HColumnDescriptor.DEFAULT_BLOOMFILTER);
  baseHtd.addFamily(hcd);
  admin.createTable(baseHtd);
  admin.disableTable(TABLE_NAME);
  try {
    // Verify the column descriptor
    verifyHColumnDescriptor(5, TABLE_NAME, FAMILY);

  } finally {
    admin.deleteTable(TABLE_NAME);
  }
}
项目:ditb    文件:TestHColumnDescriptorDefaultVersions.java   
private void verifyHColumnDescriptor(int expected, final TableName tableName,
    final byte[]... families) throws IOException {
  Admin admin = TEST_UTIL.getHBaseAdmin();

  // Verify descriptor from master
  HTableDescriptor htd = admin.getTableDescriptor(tableName);
  HColumnDescriptor[] hcds = htd.getColumnFamilies();
  verifyHColumnDescriptor(expected, hcds, tableName, families);

  // Verify descriptor from HDFS
  MasterFileSystem mfs = TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem();
  Path tableDir = FSUtils.getTableDir(mfs.getRootDir(), tableName);
  htd = FSTableDescriptors.getTableDescriptorFromFs(mfs.getFileSystem(), tableDir);
  hcds = htd.getColumnFamilies();
  verifyHColumnDescriptor(expected, hcds, tableName, families);
}
项目:ditb    文件:TestZooKeeper.java   
/**
 * Make sure we can use the cluster
 * @throws Exception
 */
private void testSanity(final String testName) throws Exception{
  String tableName = testName + "_" + System.currentTimeMillis();
  HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
  HColumnDescriptor family = new HColumnDescriptor("fam");
  desc.addFamily(family);
  LOG.info("Creating table " + tableName);
  Admin admin = TEST_UTIL.getHBaseAdmin();
  try {
    admin.createTable(desc);
  } finally {
    admin.close();
  }

  Table table =
    new HTable(new Configuration(TEST_UTIL.getConfiguration()), desc.getTableName());
  Put put = new Put(Bytes.toBytes("testrow"));
  put.add(Bytes.toBytes("fam"),
      Bytes.toBytes("col"), Bytes.toBytes("testdata"));
  LOG.info("Putting table " + tableName);
  table.put(put);
  table.close();
}
项目:ditb    文件:HBaseTestingUtility.java   
/**
 * Modify a table, synchronous. Waiting logic similar to that of {@code admin.rb#alter_status}.
 */
@SuppressWarnings("serial")
public static void modifyTableSync(Admin admin, HTableDescriptor desc)
    throws IOException, InterruptedException {
  admin.modifyTable(desc.getTableName(), desc);
  Pair<Integer, Integer> status = new Pair<Integer, Integer>() {{
    setFirst(0);
    setSecond(0);
  }};
  int i = 0;
  do {
    status = admin.getAlterStatus(desc.getTableName());
    if (status.getSecond() != 0) {
      LOG.debug(status.getSecond() - status.getFirst() + "/" + status.getSecond()
        + " regions updated.");
      Thread.sleep(1 * 1000l);
    } else {
      LOG.debug("All regions updated.");
      break;
    }
  } while (status.getFirst() != 0 && i++ < 500);
  if (status.getFirst() != 0) {
    throw new IOException("Failed to update all regions even after 500 seconds.");
  }
}
项目:ditb    文件:TestTableDescriptorModification.java   
@Test
public void testModifyTable() throws IOException {
  Admin admin = TEST_UTIL.getHBaseAdmin();
  // Create a table with one family
  HTableDescriptor baseHtd = new HTableDescriptor(TABLE_NAME);
  baseHtd.addFamily(new HColumnDescriptor(FAMILY_0));
  admin.createTable(baseHtd);
  admin.disableTable(TABLE_NAME);
  try {
    // Verify the table descriptor
    verifyTableDescriptor(TABLE_NAME, FAMILY_0);

    // Modify the table adding another family and verify the descriptor
    HTableDescriptor modifiedHtd = new HTableDescriptor(TABLE_NAME);
    modifiedHtd.addFamily(new HColumnDescriptor(FAMILY_0));
    modifiedHtd.addFamily(new HColumnDescriptor(FAMILY_1));
    admin.modifyTable(TABLE_NAME, modifiedHtd);
    verifyTableDescriptor(TABLE_NAME, FAMILY_0, FAMILY_1);
  } finally {
    admin.deleteTable(TABLE_NAME);
  }
}
项目:ditb    文件:MoveRandomRegionOfTableAction.java   
@Override
public void perform() throws Exception {
  if (sleepTime > 0) {
    Thread.sleep(sleepTime);
  }

  HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
  Admin admin = util.getHBaseAdmin();

  LOG.info("Performing action: Move random region of table " + tableName);
  List<HRegionInfo> regions = admin.getTableRegions(tableName);
  if (regions == null || regions.isEmpty()) {
    LOG.info("Table " + tableName + " doesn't have regions to move");
    return;
  }

  HRegionInfo region = PolicyBasedChaosMonkey.selectRandomItem(
    regions.toArray(new HRegionInfo[regions.size()]));
  LOG.debug("Unassigning region " + region.getRegionNameAsString());
  admin.unassign(region.getRegionName(), false);
  if (sleepTime > 0) {
    Thread.sleep(sleepTime);
  }
}
项目:ditb    文件:TestQuotaThrottle.java   
@Test(timeout = 60000)
public void testUserTableThrottle() throws Exception {
  final Admin admin = TEST_UTIL.getHBaseAdmin();
  final String userName = User.getCurrent().getShortName();

  // Add 6req/min limit
  admin.setQuota(QuotaSettingsFactory.throttleUser(userName, TABLE_NAMES[0],
    ThrottleType.REQUEST_NUMBER, 6, TimeUnit.MINUTES));
  triggerUserCacheRefresh(false, TABLE_NAMES[0]);

  // should execute at max 6 requests on tables[0] and have no limit on tables[1]
  assertEquals(6, doPuts(100, tables[0]));
  assertEquals(30, doPuts(30, tables[1]));

  // wait a minute and you should get other 6 requests executed
  waitMinuteQuota();
  assertEquals(6, doPuts(100, tables[0]));

  // Remove all the limits
  admin.setQuota(QuotaSettingsFactory.unthrottleUser(userName, TABLE_NAMES[0]));
  triggerUserCacheRefresh(true, TABLE_NAMES);
  assertEquals(60, doPuts(60, tables));
  assertEquals(60, doGets(60, tables));
}
项目:ditb    文件:TestChangingEncoding.java   
private void setEncodingConf(DataBlockEncoding encoding,
    boolean onlineChange) throws Exception {
  LOG.debug("Setting CF encoding to " + encoding + " (ordinal="
    + encoding.ordinal() + "), onlineChange=" + onlineChange);
  hcd.setDataBlockEncoding(encoding);
  try (Admin admin = TEST_UTIL.getConnection().getAdmin()) {
    if (!onlineChange) {
      admin.disableTable(tableName);
    }
    admin.modifyColumn(tableName, hcd);
    if (!onlineChange) {
      admin.enableTable(tableName);
    }
  }
  // This is a unit test, not integration test. So let's
  // wait for regions out of transition. Otherwise, for online
  // encoding change, verification phase may be flaky because
  // regions could be still in transition.
  ZKAssign.blockUntilNoRIT(TEST_UTIL.getZooKeeperWatcher());
}
项目:ditb    文件:TestLoadIncrementalHFilesSplitRecovery.java   
/**
 * Creates a table with given table name and specified number of column
 * families if the table does not already exist.
 */
private void setupTable(final Connection connection, TableName table, int cfs)
throws IOException {
  try {
    LOG.info("Creating table " + table);
    HTableDescriptor htd = new HTableDescriptor(table);
    for (int i = 0; i < cfs; i++) {
      htd.addFamily(new HColumnDescriptor(family(i)));
    }
    try (Admin admin = connection.getAdmin()) {
      admin.createTable(htd);
    }
  } catch (TableExistsException tee) {
    LOG.info("Table " + table + " already exists");
  }
}
项目:ditb    文件:ReplicationAdmin.java   
/**
 * Set the table's replication switch if the table's replication switch is already not set.
 * @param tableName name of the table
 * @param isRepEnabled is replication switch enable or disable
 * @throws IOException if a remote or network exception occurs
 */
private void setTableRep(final TableName tableName, boolean isRepEnabled) throws IOException {
  Admin admin = null;
  try {
    admin = this.connection.getAdmin();
    HTableDescriptor htd = admin.getTableDescriptor(tableName);
    if (isTableRepEnabled(htd) ^ isRepEnabled) {
      boolean isOnlineSchemaUpdateEnabled =
          this.connection.getConfiguration()
              .getBoolean("hbase.online.schema.update.enable", true);
      if (!isOnlineSchemaUpdateEnabled) {
        admin.disableTable(tableName);
      }
      for (HColumnDescriptor hcd : htd.getFamilies()) {
        hcd.setScope(isRepEnabled ? HConstants.REPLICATION_SCOPE_GLOBAL
            : HConstants.REPLICATION_SCOPE_LOCAL);
      }
      admin.modifyTable(tableName, htd);
      if (!isOnlineSchemaUpdateEnabled) {
        admin.enableTable(tableName);
      }
    }
  } finally {
    if (admin != null) {
      try {
        admin.close();
      } catch (IOException e) {
        LOG.warn("Failed to close admin connection.");
        LOG.debug("Details on failure to close admin connection.", e);
      }
    }
  }
}
项目:stroom-stats    文件:HBaseTable.java   
/**
 * Check if the table already exists, create if needed and then open.
 */
void init() {
    // HBaseAdmin admin = null;
    boolean isTableBeingCreated = false;
    try (Admin admin = hBaseConnection.getConnection().getAdmin()) {
        Preconditions.checkNotNull(admin);
        if (admin.isTableAvailable(getName())) {
            LOGGER.info("Found HBase table '{}'", getDisplayName());

        } else {
            if (getTableConfiguration().isAutoCreateTables()) {
                LOGGER.info("HBase table '{}' could not be found, so will create it", getDisplayName());
                create(admin);
                isTableBeingCreated = true;

            } else {
                final String message = "Table  '" + getDisplayName() + "' does not exist";
                LOGGER.error(message);
                throw new HBaseException(message);
            }
        }

        // table.setAutoFlush(true, true);
        // table.setWriteBufferSize(getWriteBufferSizeBytes());

        if (isTableBeingCreated) {
            tableSpecificCreationProcessing();
        }

    } catch (final Throwable t) {
        throw new HBaseException(t.getMessage(), t);
    }
}
项目:stroom-stats    文件:HBaseTable.java   
private void create(final Admin admin) {
    try {
        LOGGER.info("Creating table '{}'", getDisplayName());
        admin.createTable(getDesc());
    } catch (final Exception e) {
        throw new HBaseException(e.getMessage(), e);
    }
}
项目:ditb    文件:SchemaResource.java   
private Response update(final TableSchemaModel model, final boolean replace,
    final UriInfo uriInfo) {
  try {
    TableName name = TableName.valueOf(tableResource.getName());
    Admin admin = servlet.getAdmin();
    if (replace || !admin.tableExists(name)) {
      return replace(name, model, uriInfo, admin);
    } else {
      return update(name, model, uriInfo, admin);
    }
  } catch (Exception e) {
    servlet.getMetrics().incrementFailedPutRequests(1);
    return processException(e);
  }
}
项目:ditb    文件:CompactRandomRegionOfTableAction.java   
@Override
public void perform() throws Exception {
  HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
  Admin admin = util.getHBaseAdmin();
  boolean major = RandomUtils.nextInt(100) < majorRatio;

  LOG.info("Performing action: Compact random region of table "
    + tableName + ", major=" + major);
  List<HRegionInfo> regions = admin.getTableRegions(tableName);
  if (regions == null || regions.isEmpty()) {
    LOG.info("Table " + tableName + " doesn't have regions to compact");
    return;
  }

  HRegionInfo region = PolicyBasedChaosMonkey.selectRandomItem(
    regions.toArray(new HRegionInfo[regions.size()]));

  try {
    if (major) {
      LOG.debug("Major compacting region " + region.getRegionNameAsString());
      admin.majorCompactRegion(region.getRegionName());
    } else {
      LOG.debug("Compacting region " + region.getRegionNameAsString());
      admin.compactRegion(region.getRegionName());
    }
  } catch (Exception ex) {
    LOG.warn("Compaction failed, might be caused by other chaos: " + ex.getMessage());
  }
  if (sleepTime > 0) {
    Thread.sleep(sleepTime);
  }
}
项目:ignite-hbase    文件:HBaseCacheStoreTest.java   
@Test
public void testTableAndColumnFamilyCreation() throws IOException, ServiceException {
  try (Connection conn = getHBaseConnection()) {
    Admin admin = conn.getAdmin();
    TableName tableName = TableName.valueOf(TABLE_NAME);
    deleteTable(admin, tableName);
    assertFalse(admin.tableExists(tableName));
    IgniteConfiguration cfg = prepareConfig();
    IgniteConfiguration cfg2 = new IgniteConfiguration(cfg);
    cfg.setGridName("first");
    cfg2.setGridName("second");
    try (Ignite ignite = Ignition.getOrStart(cfg); Ignite ignite2 = Ignition
        .getOrStart(cfg2)) {
      String cacheName = "myCache";
      String otherCacheName = "myOtherCache";
      IgniteCache<String, String> cache = ignite.getOrCreateCache(cacheName);
      IgniteCache<String, String> otherCache = ignite.getOrCreateCache(otherCacheName);
      assertFalse(admin.tableExists(tableName));
      cache.put("Hello", "World");
      assertTrue(admin.tableExists(tableName));
      assertTrue(admin.getTableDescriptor(tableName).hasFamily(cacheName.getBytes()));
      assertFalse(
          admin.getTableDescriptor(tableName).hasFamily(otherCacheName.getBytes()));
      otherCache.put("Hello", "World");
      assertTrue(admin.tableExists(tableName));
      assertTrue(
          admin.getTableDescriptor(tableName).hasFamily(otherCacheName.getBytes()));
    }
  }
}
项目:ditb    文件:TestGzipFilter.java   
@BeforeClass
public static void setUpBeforeClass() throws Exception {
  TEST_UTIL.startMiniCluster();
  REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
  client = new Client(new Cluster().add("localhost",
    REST_TEST_UTIL.getServletPort()));
  Admin admin = TEST_UTIL.getHBaseAdmin();
  if (admin.tableExists(TABLE)) {
    return;
  }
  HTableDescriptor htd = new HTableDescriptor(TABLE);
  htd.addFamily(new HColumnDescriptor(CFA));
  admin.createTable(htd);
}
项目:dremio-oss    文件:TestTableGenerator.java   
public static void generateHBaseDatasetDoubleOB(Connection conn, Admin admin, TableName tableName, int numberRegions) throws Exception {
  if (admin.tableExists(tableName)) {
    admin.disableTable(tableName);
    admin.deleteTable(tableName);
  }

  HTableDescriptor desc = new HTableDescriptor(tableName);
  desc.addFamily(new HColumnDescriptor(FAMILY_F));

  if (numberRegions > 1) {
    admin.createTable(desc, Arrays.copyOfRange(SPLIT_KEYS, 0, numberRegions-1));
  } else {
    admin.createTable(desc);
  }

  BufferedMutator table = conn.getBufferedMutator(tableName);

  for (double i = 0.5; i <= 100.00; i += 0.75) {
    byte[] bytes = new byte[9];
    PositionedByteRange br = new SimplePositionedMutableByteRange(bytes, 0, 9);
    OrderedBytes.encodeFloat64(br, i, Order.ASCENDING);
    Put p = new Put(bytes);
    p.addColumn(FAMILY_F, COLUMN_C, String.format("value %03f", i).getBytes());
    table.mutate(p);
  }

  table.close();

  admin.flush(tableName);
}
项目:dremio-oss    文件:TestTableGenerator.java   
public static void generateHBaseDatasetFloatOB(Connection conn, Admin admin, TableName tableName, int numberRegions) throws Exception {
  if (admin.tableExists(tableName)) {
    admin.disableTable(tableName);
    admin.deleteTable(tableName);
  }

  HTableDescriptor desc = new HTableDescriptor(tableName);
  desc.addFamily(new HColumnDescriptor(FAMILY_F));

  if (numberRegions > 1) {
    admin.createTable(desc, Arrays.copyOfRange(SPLIT_KEYS, 0, numberRegions-1));
  } else {
    admin.createTable(desc);
  }

  BufferedMutator table = conn.getBufferedMutator(tableName);

  for (float i = (float)0.5; i <= 100.00; i += 0.75) {
    byte[] bytes = new byte[5];
    PositionedByteRange br = new SimplePositionedMutableByteRange(bytes, 0, 5);
    OrderedBytes.encodeFloat32(br, i,Order.ASCENDING);
    Put p = new Put(bytes);
    p.addColumn(FAMILY_F, COLUMN_C, String.format("value %03f", i).getBytes());
    table.mutate(p);
  }

  table.close();

  admin.flush(tableName);
}
项目:dremio-oss    文件:TestTableGenerator.java   
public static void generateHBaseDatasetIntOB(Connection conn, Admin admin, TableName tableName, int numberRegions) throws Exception {
  if (admin.tableExists(tableName)) {
    admin.disableTable(tableName);
    admin.deleteTable(tableName);
  }

  HTableDescriptor desc = new HTableDescriptor(tableName);
  desc.addFamily(new HColumnDescriptor(FAMILY_F));

  if (numberRegions > 1) {
    admin.createTable(desc, Arrays.copyOfRange(SPLIT_KEYS, 0, numberRegions-1));
  } else {
    admin.createTable(desc);
  }

  BufferedMutator table = conn.getBufferedMutator(tableName);

  for (int i = -49; i <= 100; i ++) {
    byte[] bytes = new byte[5];
    PositionedByteRange br = new SimplePositionedMutableByteRange(bytes, 0, 5);
    OrderedBytes.encodeInt32(br, i, Order.ASCENDING);
    Put p = new Put(bytes);
    p.addColumn(FAMILY_F, COLUMN_C, String.format("value %d", i).getBytes());
    table.mutate(p);
  }

  table.close();

  admin.flush(tableName);
}
项目:ditb    文件:TestNamespacesInstanceResource.java   
@Test
public void testInvalidNamespacePostsAndPuts() throws IOException, JAXBException {
  String namespacePath1 = "/namespaces/" + NAMESPACE1;
  String namespacePath2 = "/namespaces/" + NAMESPACE2;
  String namespacePath3 = "/namespaces/" + NAMESPACE3;
  NamespacesInstanceModel model1;
  NamespacesInstanceModel model2;
  NamespacesInstanceModel model3;
  Response response;

  // Check that namespaces don't exist via non-REST call.
  Admin admin = TEST_UTIL.getHBaseAdmin();
  assertNull(findNamespace(admin, NAMESPACE1));
  assertNull(findNamespace(admin, NAMESPACE2));
  assertNull(findNamespace(admin, NAMESPACE3));

  model1 = testNamespacesInstanceModel.buildTestModel(NAMESPACE1, NAMESPACE1_PROPS);
  testNamespacesInstanceModel.checkModel(model1, NAMESPACE1, NAMESPACE1_PROPS);
  model2 = testNamespacesInstanceModel.buildTestModel(NAMESPACE2, NAMESPACE2_PROPS);
  testNamespacesInstanceModel.checkModel(model2, NAMESPACE2, NAMESPACE2_PROPS);
  model3 = testNamespacesInstanceModel.buildTestModel(NAMESPACE3, NAMESPACE3_PROPS);
  testNamespacesInstanceModel.checkModel(model3, NAMESPACE3, NAMESPACE3_PROPS);

  // Try REST post and puts with invalid content.
  response = client.post(namespacePath1, Constants.MIMETYPE_JSON, toXML(model1));
  assertEquals(500, response.getCode());
  String jsonString = jsonMapper.writeValueAsString(model2);
  response = client.put(namespacePath2, Constants.MIMETYPE_XML, Bytes.toBytes(jsonString));
  assertEquals(400, response.getCode());
  response = client.post(namespacePath3, Constants.MIMETYPE_PROTOBUF, toXML(model1));
  assertEquals(500, response.getCode());

  NamespaceDescriptor nd1 = findNamespace(admin, NAMESPACE1);
  NamespaceDescriptor nd2 = findNamespace(admin, NAMESPACE2);
  NamespaceDescriptor nd3 = findNamespace(admin, NAMESPACE3);
  assertNull(nd1);
  assertNull(nd2);
  assertNull(nd3);
}