Java Code Examples for org.apache.hadoop.hbase.TableName

The following are top voted examples for showing how to use org.apache.hadoop.hbase.TableName. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: ditb   File: TestCreateTableHandler.java   Source Code and License 6 votes vote down vote up
@Test (timeout=300000)
public void testCreateTableCalledTwiceAndFirstOneInProgress() throws Exception {
  final TableName tableName = TableName.valueOf("testCreateTableCalledTwiceAndFirstOneInProgress");
  final MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
  final HMaster m = cluster.getMaster();
  final HTableDescriptor desc = new HTableDescriptor(tableName);
  desc.addFamily(new HColumnDescriptor(FAMILYNAME));
  final HRegionInfo[] hRegionInfos = new HRegionInfo[] { new HRegionInfo(desc.getTableName(), null,
      null) };
  CustomCreateTableHandler handler = new CustomCreateTableHandler(m, m.getMasterFileSystem(),
      desc, cluster.getConfiguration(), hRegionInfos, m);
  handler.prepare();
  throwException = true;
  handler.process();
  throwException = false;
  CustomCreateTableHandler handler1 = new CustomCreateTableHandler(m, m.getMasterFileSystem(),
      desc, cluster.getConfiguration(), hRegionInfos, m);
  handler1.prepare();
  handler1.process();
  for (int i = 0; i < 100; i++) {
    if (!TEST_UTIL.getHBaseAdmin().isTableAvailable(tableName)) {
      Thread.sleep(200);
    }
  }
  assertTrue(TEST_UTIL.getHBaseAdmin().isTableEnabled(tableName));
}
 
Example 2
Project: hbase-tutorials   File: HBaseCrudDemo.java   Source Code and License 6 votes vote down vote up
public void disableTable(Connection connection, TableName tableName) throws IOException {
    Admin admin = null;
    try {
        admin = connection.getAdmin();
        if(admin.tableExists(tableName)){
            admin.disableTable(tableName);
        }
    } finally {
        if(admin!=null) {
            admin.close();
        }
    }
}
 
Example 3
Project: ditb   File: TestCoprocessorScanPolicy.java   Source Code and License 6 votes vote down vote up
@Override
public KeyValueScanner preStoreScannerOpen(
    final ObserverContext<RegionCoprocessorEnvironment> c, Store store, final Scan scan,
    final NavigableSet<byte[]> targetCols, KeyValueScanner s) throws IOException {
  TableName tn = store.getTableName();
  if (!tn.isSystemTable()) {
    Long newTtl = ttls.get(store.getTableName());
    Integer newVersions = versions.get(store.getTableName());
    ScanInfo oldSI = store.getScanInfo();
    HColumnDescriptor family = store.getFamily();
    ScanInfo scanInfo = new ScanInfo(TEST_UTIL.getConfiguration(),
        family.getName(), family.getMinVersions(),
        newVersions == null ? family.getMaxVersions() : newVersions,
        newTtl == null ? oldSI.getTtl() : newTtl, family.getKeepDeletedCells(),
        oldSI.getTimeToPurgeDeletes(), oldSI.getComparator());
    return new StoreScanner(store, scanInfo, scan, targetCols,
        ((HStore) store).getHRegion().getReadpoint(IsolationLevel.READ_COMMITTED));
  } else {
    return s;
  }
}
 
Example 4
Project: ditb   File: AccessController.java   Source Code and License 6 votes vote down vote up
@Override
public void postModifyTable(ObserverContext<MasterCoprocessorEnvironment> c,
    TableName tableName, final HTableDescriptor htd) throws IOException {
  final Configuration conf = c.getEnvironment().getConfiguration();
  // default the table owner to current user, if not specified.
  final String owner = (htd.getOwnerString() != null) ? htd.getOwnerString() :
    getActiveUser().getShortName();
  User.runAsLoginUser(new PrivilegedExceptionAction<Void>() {
    @Override
    public Void run() throws Exception {
      UserPermission userperm = new UserPermission(Bytes.toBytes(owner),
        htd.getTableName(), null, Action.values());
      AccessControlLists.addUserPermission(conf, userperm);
      return null;
    }
  });
}
 
Example 5
Project: ignite-hbase   File: AdminContext.java   Source Code and License 6 votes vote down vote up
private void createColumnFamily(HColumnDescriptor family, TableName table)
    throws IOException {
  try {
    admin.addColumn(table, family);
  } catch (InvalidFamilyOperationException e) {
    if (!hasFamily(family, table)) {
      //Schroedinger's cat: InvalidFamilyOperationException (cf exists) but does not exist at the same time
      throw new IllegalStateException("Column family should exist but does not", e);
    }
    //columnFamily was created in the meantime
    return;
  }
  waitForColumnFamilyCreation(family, table);
  log.info("Created column family '{}' in HBase table '{}'", family.getNameAsString(),
      table.getNameAsString());
}
 
Example 6
Project: ditb   File: LCStatInfo2.java   Source Code and License 6 votes vote down vote up
/**
 * create map based on statDesc
 * 1. for set, "family qualifier DataType set [v1] [v2] [...]"
 * 2. for array, "family qualifier DataType min max parts"
 */
public static Map<TableName, LCStatInfo2> parseStatString(IndexTableRelation indexTableRelation,
    String statDesc) throws IOException {
  Map<TableName, LCStatInfo2> map = new HashMap<>();
  String[] lines = statDesc.split(LC_TABLE_DESC_RANGE_DELIMITER);
  for (String line : lines) {
    String[] parts = line.split("\t");
    byte[] family = Bytes.toBytes(parts[0]);
    byte[] qualifier = Bytes.toBytes(parts[1]);
    TableName tableName = indexTableRelation.getIndexTableName(family, qualifier);
    LCStatInfo2 statInfo;
    try {
      if ("set".equalsIgnoreCase(parts[3])) {
        statInfo = new LCStatInfo2(family, qualifier, DataType.valueOf(parts[2]), parts, 4);
      } else {
        statInfo = new LCStatInfo2(family, qualifier, DataType.valueOf(parts[2]),
            Integer.valueOf(parts[5]), parts[3], parts[4]);
      }
    } catch (IOException e) {
      throw new IOException("exception for parsing line: " + line, e);
    }
    map.put(tableName, statInfo);
  }
  return map;
}
 
Example 7
Project: stroom-stats   File: HBaseEventStoreTable.java   Source Code and License 6 votes vote down vote up
/**
 * Private constructor
 */
private HBaseEventStoreTable(final EventStoreTimeIntervalEnum timeInterval,
                             final StroomPropertyService propertyService,
                             final HBaseConnection hBaseConnection,
                             final UniqueIdCache uniqueIdCache,
                             final StatisticDataPointAdapterFactory statisticDataPointAdapterFactory) {
    super(hBaseConnection);
    this.displayName = timeInterval.longName() + DISPLAY_NAME_POSTFIX;
    this.tableName = TableName.valueOf(Bytes.toBytes(timeInterval.shortName() + TABLE_NAME_POSTFIX));
    this.timeInterval = timeInterval;
    this.propertyService = propertyService;
    this.rowKeyBuilder = new SimpleRowKeyBuilder(uniqueIdCache, timeInterval);
    this.statisticDataPointAdapterFactory = statisticDataPointAdapterFactory;

    for (StatisticType statisticType : StatisticType.values()) {
        putCounterMap.put(statisticType, new LongAdder());
    }

    init();
}
 
Example 8
Project: ditb   File: IndexPutParser.java   Source Code and License 6 votes vote down vote up
@Override protected Map<TableName, Put> parsePut(Put put, boolean serverSide) {
  Map<TableName, Put> map = new HashMap<>();
  byte[] row = put.getRow();
  for (Map.Entry<byte[], List<Cell>> entry : put.getFamilyCellMap().entrySet()) {
    byte[] family = entry.getKey();
    for (Cell cell : entry.getValue()) {
      byte[] q = CellUtil.cloneQualifier(cell);
      if (tableRelation.isIndexColumn(family, q)) {
        TableName indexTableName = tableRelation.getIndexTableName(family, q);
        Put newPut = new Put(getIndexRow(row, CellUtil.cloneValue(cell)));
        if (serverSide) newPut
            .addColumn(IndexType.SEDONDARY_FAMILY_BYTES, (byte[]) null, cell.getTimestamp(),
                null);
        else newPut.addColumn(IndexType.SEDONDARY_FAMILY_BYTES, null, null);
        map.put(indexTableName, newPut);
      }
    }
  }
  tableRelation.getIndexFamilyMap();
  return map;
}
 
Example 9
Project: ditb   File: TestVisibilityLabelsWithACL.java   Source Code and License 6 votes vote down vote up
private static Table createTableAndWriteDataWithLabels(TableName tableName, String... labelExps)
    throws Exception {
  Table table = null;
  try {
    table = TEST_UTIL.createTable(tableName, fam);
    int i = 1;
    List<Put> puts = new ArrayList<Put>();
    for (String labelExp : labelExps) {
      Put put = new Put(Bytes.toBytes("row" + i));
      put.add(fam, qual, HConstants.LATEST_TIMESTAMP, value);
      put.setCellVisibility(new CellVisibility(labelExp));
      puts.add(put);
      i++;
    }
    table.put(puts);
  } finally {
    if (table != null) {
      table.close();
    }
  }
  return table;
}
 
Example 10
Project: ditb   File: IntegrationTestLoadAndVerify.java   Source Code and License 6 votes vote down vote up
@Override
public void setup(Context context) throws IOException {
  conf = context.getConfiguration();
  recordsToWrite = conf.getLong(NUM_TO_WRITE_KEY, NUM_TO_WRITE_DEFAULT);
  String tableName = conf.get(TABLE_NAME_KEY, TABLE_NAME_DEFAULT);
  numBackReferencesPerRow = conf.getInt(NUM_BACKREFS_KEY, NUM_BACKREFS_DEFAULT);
  this.connection = ConnectionFactory.createConnection(conf);
  mutator = connection.getBufferedMutator(
      new BufferedMutatorParams(TableName.valueOf(tableName))
          .writeBufferSize(4 * 1024 * 1024));

  String taskId = conf.get("mapreduce.task.attempt.id");
  Matcher matcher = Pattern.compile(".+_m_(\\d+_\\d+)").matcher(taskId);
  if (!matcher.matches()) {
    throw new RuntimeException("Strange task ID: " + taskId);
  }
  shortTaskId = matcher.group(1);

  rowsWritten = context.getCounter(Counters.ROWS_WRITTEN);
  refsWritten = context.getCounter(Counters.REFERENCES_WRITTEN);
}
 
Example 11
Project: ditb   File: TestFromClientSide.java   Source Code and License 6 votes vote down vote up
@Test
public void testMaxKeyValueSize() throws Exception {
  byte [] TABLE = Bytes.toBytes("testMaxKeyValueSize");
  Configuration conf = TEST_UTIL.getConfiguration();
  String oldMaxSize = conf.get(ConnectionConfiguration.MAX_KEYVALUE_SIZE_KEY);
  Table ht = TEST_UTIL.createTable(TABLE, FAMILY);
  byte[] value = new byte[4 * 1024 * 1024];
  Put put = new Put(ROW);
  put.add(FAMILY, QUALIFIER, value);
  ht.put(put);
  try {
    TEST_UTIL.getConfiguration().setInt(
        ConnectionConfiguration.MAX_KEYVALUE_SIZE_KEY, 2 * 1024 * 1024);
    // Create new table so we pick up the change in Configuration.
    try (Connection connection =
        ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
      try (Table t = connection.getTable(TableName.valueOf(FAMILY))) {
        put = new Put(ROW);
        put.add(FAMILY, QUALIFIER, value);
        t.put(put);
      }
    }
    fail("Inserting a too large KeyValue worked, should throw exception");
  } catch(Exception e) {}
  conf.set(ConnectionConfiguration.MAX_KEYVALUE_SIZE_KEY, oldMaxSize);
}
 
Example 12
Project: ditb   File: SecureTestUtil.java   Source Code and License 6 votes vote down vote up
/**
 * Grant permissions on a table to the given user using AccessControlClient. Will wait until all
 * active AccessController instances have updated their permissions caches or will
 * throw an exception upon timeout (10 seconds).
 */
public static void grantOnTableUsingAccessControlClient(final HBaseTestingUtility util,
    final Connection connection, final String user, final TableName table, final byte[] family,
    final byte[] qualifier, final Permission.Action... actions) throws Exception {
  SecureTestUtil.updateACLs(util, new Callable<Void>() {
    @Override
    public Void call() throws Exception {
      try {
        AccessControlClient.grant(connection, table, user, family, qualifier, actions);
      } catch (Throwable t) {
        t.printStackTrace();
      }
      return null;
    }
  });
}
 
Example 13
Project: ditb   File: CreateTableProcedure.java   Source Code and License 6 votes vote down vote up
protected static void assignRegions(final MasterProcedureEnv env,
    final TableName tableName, final List<HRegionInfo> regions)
    throws HBaseException, IOException {
  ProcedureSyncWait.waitRegionServers(env);

  final AssignmentManager assignmentManager = env.getMasterServices().getAssignmentManager();

  // Mark the table as Enabling
  assignmentManager.getTableStateManager().setTableState(tableName,
      ZooKeeperProtos.Table.State.ENABLING);

  // Trigger immediate assignment of the regions in round-robin fashion
  ModifyRegionUtils.assignRegions(assignmentManager, regions);

  // Enable table
  assignmentManager.getTableStateManager()
    .setTableState(tableName, ZooKeeperProtos.Table.State.ENABLED);
}
 
Example 14
Project: ditb   File: IntegrationTestBigLinkedListWithVisibility.java   Source Code and License 6 votes vote down vote up
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);
      }
    }
  }
}
 
Example 15
Project: easyhbase   File: HbaseTemplate2.java   Source Code and License 6 votes vote down vote up
@Override
public <T> List<T> find(TableName tableName, final List<Scan> scanList, final
ResultsExtractor<T> action) {
    assertAccessAvailable();
    return execute(tableName, new TableCallback<List<T>>() {
        @Override
        public List<T> doInTable(Table table) throws Throwable {
            List<T> result = new ArrayList<>(scanList.size());
            for (Scan scan : scanList) {
                final ResultScanner scanner = table.getScanner(scan);
                try {
                    T t = action.extractData(scanner);
                    result.add(t);
                } finally {
                    scanner.close();
                }
            }
            return result;
        }
    });
}
 
Example 16
Project: ditb   File: TestRegionSplitter.java   Source Code and License 6 votes vote down vote up
/**
 * Test creating a pre-split table using the UniformSplit algorithm.
 */
@Test
public void testCreatePresplitTableUniform() throws Exception {
  List<byte[]> expectedBounds = new ArrayList<byte[]>();
  expectedBounds.add(ArrayUtils.EMPTY_BYTE_ARRAY);
  expectedBounds.add(new byte[] {      0x10, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {      0x20, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {      0x30, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {      0x40, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {      0x50, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {      0x60, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {      0x70, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {(byte)0x80, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {(byte)0x90, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {(byte)0xa0, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {(byte)0xb0, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {(byte)0xc0, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {(byte)0xd0, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {(byte)0xe0, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(new byte[] {(byte)0xf0, 0, 0, 0, 0, 0, 0, 0});
  expectedBounds.add(ArrayUtils.EMPTY_BYTE_ARRAY);

  // Do table creation/pre-splitting and verification of region boundaries
  preSplitTableAndVerify(expectedBounds, UniformSplit.class.getSimpleName(),
    TableName.valueOf("NewUniformPresplitTable"));
}
 
Example 17
Project: ditb   File: SnapshotManager.java   Source Code and License 6 votes vote down vote up
/**
 * Take a snapshot using the specified handler.
 * On failure the snapshot temporary working directory is removed.
 * NOTE: prepareToTakeSnapshot() called before this one takes care of the rejecting the
 *       snapshot request if the table is busy with another snapshot/restore operation.
 * @param snapshot the snapshot description
 * @param handler the snapshot handler
 */
private synchronized void snapshotTable(SnapshotDescription snapshot,
    final TakeSnapshotHandler handler) throws HBaseSnapshotException {
  try {
    handler.prepare();
    this.executorService.submit(handler);
    this.snapshotHandlers.put(TableName.valueOf(snapshot.getTable()), handler);
  } catch (Exception e) {
    // cleanup the working directory by trying to delete it from the fs.
    Path workingDir = SnapshotDescriptionUtils.getWorkingSnapshotDir(snapshot, rootDir);
    try {
      if (!this.master.getMasterFileSystem().getFileSystem().delete(workingDir, true)) {
        LOG.error("Couldn't delete working directory (" + workingDir + " for snapshot:" +
            ClientSnapshotDescriptionUtils.toString(snapshot));
      }
    } catch (IOException e1) {
      LOG.error("Couldn't delete working directory (" + workingDir + " for snapshot:" +
          ClientSnapshotDescriptionUtils.toString(snapshot));
    }
    // fail the snapshot
    throw new SnapshotCreationException("Could not build snapshot handler", e, snapshot);
  }
}
 
Example 18
Project: ditb   File: TestCreateTableProcedure2.java   Source Code and License 6 votes vote down vote up
@Test
public void testMasterRestartAfterNameSpaceEnablingNodeIsCreated() throws Exception {
  // Step 1: start mini zk cluster.
  MiniZooKeeperCluster zkCluster;
  zkCluster = TEST_UTIL.startMiniZKCluster();
  // Step 2: add an orphaned system table ZNODE
  TableName tableName = TableName.valueOf("hbase:namespace");
  ZooKeeperWatcher zkw = TEST_UTIL.getZooKeeperWatcher();
  String znode = ZKUtil.joinZNode(zkw.tableZNode, tableName.getNameAsString());
  ZooKeeperProtos.Table.Builder builder = ZooKeeperProtos.Table.newBuilder();
  builder.setState(ZooKeeperProtos.Table.State.ENABLED);
  byte [] data = ProtobufUtil.prependPBMagic(builder.build().toByteArray());
  ZKUtil.createSetData(zkw, znode, data);
  LOG.info("Create an orphaned Znode " + znode + " with data " + data);
  // Step 3: link the zk cluster to hbase cluster
  TEST_UTIL.setZkCluster(zkCluster);
  // Step 4: start hbase cluster and expect master to start successfully.
  TEST_UTIL.startMiniCluster();
  assertTrue(TEST_UTIL.getHBaseCluster().getLiveMasterThreads().size() == 1);
}
 
Example 19
Project: ditb   File: TestRegionObserverScannerOpenHook.java   Source Code and License 6 votes vote down vote up
Region initHRegion(byte[] tableName, String callingMethod, Configuration conf,
    byte[]... families) throws IOException {
  HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
  for (byte[] family : families) {
    htd.addFamily(new HColumnDescriptor(family));
  }
  HRegionInfo info = new HRegionInfo(htd.getTableName(), null, null, false);
  Path path = new Path(DIR + callingMethod);
  HRegion r = HRegion.createHRegion(info, path, conf, htd);
  // this following piece is a hack. currently a coprocessorHost
  // is secretly loaded at OpenRegionHandler. we don't really
  // start a region server here, so just manually create cphost
  // and set it to region.
  RegionCoprocessorHost host = new RegionCoprocessorHost(r, null, conf);
  r.setCoprocessorHost(host);
  return r;
}
 
Example 20
Project: ditb   File: MetaScanner.java   Source Code and License 6 votes vote down vote up
/**
 * Lists all of the table regions currently in META.
 * @param connection
 * @param tableName
 * @return Map of all user-space regions to servers
 * @throws IOException
 */
public static NavigableMap<HRegionInfo, ServerName> allTableRegions(
    Connection connection, final TableName tableName) throws IOException {
  final NavigableMap<HRegionInfo, ServerName> regions =
    new TreeMap<HRegionInfo, ServerName>();
  MetaScannerVisitor visitor = new TableMetaScannerVisitor(tableName) {
    @Override
    public boolean processRowInternal(Result result) throws IOException {
      RegionLocations locations = MetaTableAccessor.getRegionLocations(result);
      if (locations == null) return true;
      for (HRegionLocation loc : locations.getRegionLocations()) {
        if (loc != null) {
          HRegionInfo regionInfo = loc.getRegionInfo();
          regions.put(new UnmodifyableHRegionInfo(regionInfo), loc.getServerName());
        }
      }
      return true;
    }
  };
  metaScan(connection, visitor, tableName);
  return regions;
}
 
Example 21
Project: ditb   File: TestZKBasedOpenCloseRegion.java   Source Code and License 6 votes vote down vote up
private static void waitUntilAllRegionsAssigned()
throws IOException {
  HTable meta = new HTable(TEST_UTIL.getConfiguration(), TableName.META_TABLE_NAME);
  while (true) {
    int rows = 0;
    Scan scan = new Scan();
    scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER);
    ResultScanner s = meta.getScanner(scan);
    for (Result r = null; (r = s.next()) != null;) {
      byte [] b =
        r.getValue(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER);
      if (b == null || b.length <= 0) {
        break;
      }
      rows++;
    }
    s.close();
    // If I get to here and all rows have a Server, then all have been assigned.
    if (rows >= countOfRegions) {
      break;
    }
    LOG.info("Found=" + rows);
    Threads.sleep(1000);
  }
  meta.close();
}
 
Example 22
Project: ditb   File: TestLoadIncrementalHFiles.java   Source Code and License 5 votes vote down vote up
private void runTest(String testName, BloomType bloomType,
    byte[][] tableSplitKeys, byte[][][] hfileRanges) throws Exception {
  final byte[] TABLE_NAME = Bytes.toBytes("mytable_"+testName);
  final boolean preCreateTable = tableSplitKeys != null;

  // Run the test bulkloading the table to the default namespace
  final TableName TABLE_WITHOUT_NS = TableName.valueOf(TABLE_NAME);
  runTest(testName, TABLE_WITHOUT_NS, bloomType, preCreateTable, tableSplitKeys, hfileRanges);

  // Run the test bulkloading the table to the specified namespace
  final TableName TABLE_WITH_NS = TableName.valueOf(Bytes.toBytes(NAMESPACE), TABLE_NAME);
  runTest(testName, TABLE_WITH_NS, bloomType, preCreateTable, tableSplitKeys, hfileRanges);
}
 
Example 23
Project: ditb   File: TestFSTableDescriptors.java   Source Code and License 5 votes vote down vote up
@Test
public void testNoSuchTable() throws IOException {
  final String name = "testNoSuchTable";
  FileSystem fs = FileSystem.get(UTIL.getConfiguration());
  // Cleanup old tests if any detrius laying around.
  Path rootdir = new Path(UTIL.getDataTestDir(), name);
  TableDescriptors htds = new FSTableDescriptors(UTIL.getConfiguration(), fs, rootdir);
  assertNull("There shouldn't be any HTD for this table", htds.get(TableName.valueOf("NoSuchTable")));
}
 
Example 24
Project: ditb   File: MultiTableOutputFormat.java   Source Code and License 5 votes vote down vote up
/**
 * @param tableName
 *          the name of the table, as a string
 * @return the named mutator
 * @throws IOException
 *           if there is a problem opening a table
 */
BufferedMutator getBufferedMutator(ImmutableBytesWritable tableName) throws IOException {
  if(this.connection == null){
    this.connection = ConnectionFactory.createConnection(conf);
  }
  if (!mutatorMap.containsKey(tableName)) {
    LOG.debug("Opening HTable \"" + Bytes.toString(tableName.get())+ "\" for writing");

    BufferedMutator mutator =
        connection.getBufferedMutator(TableName.valueOf(tableName.get()));
    mutatorMap.put(tableName, mutator);
  }
  return mutatorMap.get(tableName);
}
 
Example 25
Project: ditb   File: FSTableDescriptors.java   Source Code and License 5 votes vote down vote up
/**
 * Adds (or updates) the table descriptor to the FileSystem
 * and updates the local cache with it.
 */
@Override
public void add(HTableDescriptor htd) throws IOException {
  if (fsreadonly) {
    throw new NotImplementedException("Cannot add a table descriptor - in read only mode");
  }
  if (TableName.META_TABLE_NAME.equals(htd.getTableName())) {
    throw new NotImplementedException();
  }
  if (HConstants.HBASE_NON_USER_TABLE_DIRS.contains(htd.getTableName().getNameAsString())) {
    throw new NotImplementedException(
      "Cannot add a table descriptor for a reserved subdirectory name: " + htd.getNameAsString());
  }
  updateTableDescriptor(htd);
}
 
Example 26
Project: easyhbase   File: HBaseAdminTemplate.java   Source Code and License 5 votes vote down vote up
public void dropTable(TableName tableName) {
    try {
        this.admin.disableTable(tableName);
        this.admin.deleteTable(tableName);
    } catch (IOException e) {
        throw new HbaseSystemException(e);
    }
}
 
Example 27
Project: ditb   File: SecureTestUtil.java   Source Code and License 5 votes vote down vote up
@Override
public void postDeleteTableHandler(
    final ObserverContext<MasterCoprocessorEnvironment> ctx, TableName tableName)
    throws IOException {
  // the AccessController test, some times calls only and directly the postDeleteTableHandler()
  if (tableDeletionLatch != null) {
    tableDeletionLatch.countDown();
  }
}
 
Example 28
Project: ditb   File: TestScannerResource.java   Source Code and License 5 votes vote down vote up
static int insertData(Configuration conf, TableName tableName, String column, double prob)
    throws IOException {
  Random rng = new Random();
  byte[] k = new byte[3];
  byte [][] famAndQf = KeyValue.parseColumn(Bytes.toBytes(column));
  List<Put> puts = new ArrayList<>();
  for (byte b1 = 'a'; b1 < 'z'; b1++) {
    for (byte b2 = 'a'; b2 < 'z'; b2++) {
      for (byte b3 = 'a'; b3 < 'z'; b3++) {
        if (rng.nextDouble() < prob) {
          k[0] = b1;
          k[1] = b2;
          k[2] = b3;
          Put put = new Put(k);
          put.setDurability(Durability.SKIP_WAL);
          put.add(famAndQf[0], famAndQf[1], k);
          puts.add(put);
        }
      }
    }
  }
  try (Connection conn = ConnectionFactory.createConnection(conf);
      Table table = conn.getTable(tableName)) {
    table.put(puts);
  }
  return puts.size();
}
 
Example 29
Project: ditb   File: CCIndexAdmin.java   Source Code and License 5 votes vote down vote up
public void flushAll(TableName tableName) throws IOException, InterruptedException {
  HTableDescriptor desc = admin.getTableDescriptor(tableName);
  if (isIndexTable(desc)) {
    throw new TableNotFoundException(tableName);
  }
  IndexTableDescriptor indexDesc = new IndexTableDescriptor(desc);

  if (indexDesc.hasIndex()) {
    for (IndexSpecification indexSpec : indexDesc.getIndexSpecifications()) {
      admin.flush(indexSpec.getIndexTableName());
    }
  }
  admin.flush(tableName);
}
 
Example 30
Project: ditb   File: TestHRegion.java   Source Code and License 5 votes vote down vote up
@Test
public void testDelete_CheckTimestampUpdated() throws IOException {
  TableName tableName = TableName.valueOf(name.getMethodName());
  byte[] row1 = Bytes.toBytes("row1");
  byte[] col1 = Bytes.toBytes("col1");
  byte[] col2 = Bytes.toBytes("col2");
  byte[] col3 = Bytes.toBytes("col3");

  // Setting up region
  String method = this.getName();
  this.region = initHRegion(tableName, method, CONF, fam1);
  try {
    // Building checkerList
    List<Cell> kvs = new ArrayList<Cell>();
    kvs.add(new KeyValue(row1, fam1, col1, null));
    kvs.add(new KeyValue(row1, fam1, col2, null));
    kvs.add(new KeyValue(row1, fam1, col3, null));

    NavigableMap<byte[], List<Cell>> deleteMap = new TreeMap<byte[], List<Cell>>(
        Bytes.BYTES_COMPARATOR);
    deleteMap.put(fam1, kvs);
    region.delete(deleteMap, Durability.SYNC_WAL);

    // extract the key values out the memstore:
    // This is kinda hacky, but better than nothing...
    long now = System.currentTimeMillis();
    DefaultMemStore memstore = (DefaultMemStore) ((HStore) region.getStore(fam1)).memstore;
    Cell firstCell = memstore.cellSet.first();
    assertTrue(firstCell.getTimestamp() <= now);
    now = firstCell.getTimestamp();
    for (Cell cell : memstore.cellSet) {
      assertTrue(cell.getTimestamp() <= now);
      now = cell.getTimestamp();
    }
  } finally {
    HRegion.closeHRegion(this.region);
    this.region = null;
  }
}
 
Example 31
Project: ditb   File: HMerge.java   Source Code and License 5 votes vote down vote up
/**
 * Scans the table and merges two adjacent regions if they are small. This
 * only happens when a lot of rows are deleted.
 *
 * When merging the hbase:meta region, the HBase instance must be offline.
 * When merging a normal table, the HBase instance must be online, but the
 * table must be disabled.
 *
 * @param conf        - configuration object for HBase
 * @param fs          - FileSystem where regions reside
 * @param tableName   - Table to be compacted
 * @param testMasterRunning True if we are to verify master is down before
 * running merge
 * @throws IOException
 */
public static void merge(Configuration conf, FileSystem fs,
  final TableName tableName, final boolean testMasterRunning)
throws IOException {
  boolean masterIsRunning = false;
  if (testMasterRunning) {
    masterIsRunning = HConnectionManager
        .execute(new HConnectable<Boolean>(conf) {
          @Override
          public Boolean connect(HConnection connection) throws IOException {
            return connection.isMasterRunning();
          }
        });
  }
  if (tableName.equals(TableName.META_TABLE_NAME)) {
    if (masterIsRunning) {
      throw new IllegalStateException(
          "Can not compact hbase:meta table if instance is on-line");
    }
    // TODO reenable new OfflineMerger(conf, fs).process();
  } else {
    if(!masterIsRunning) {
      throw new IllegalStateException(
          "HBase instance must be running to merge a normal table");
    }
    Admin admin = new HBaseAdmin(conf);
    try {
      if (!admin.isTableDisabled(tableName)) {
        throw new TableNotDisabledException(tableName);
      }
    } finally {
      admin.close();
    }
    new OnlineMerger(conf, fs, tableName).process();
  }
}
 
Example 32
Project: ditb   File: TestCoprocessorTableEndpoint.java   Source Code and License 5 votes vote down vote up
@Test
public void testDynamicCoprocessorTableEndpoint() throws Throwable {    
  final TableName tableName = TableName.valueOf("testDynamicCoprocessorTableEndpoint");

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

  createTable(desc);

  desc.addCoprocessor(org.apache.hadoop.hbase.coprocessor.ColumnAggregationEndpoint.class.getName());
  updateTable(desc);

  verifyTable(tableName);
}
 
Example 33
Project: ditb   File: TestNamespaceAuditor.java   Source Code and License 5 votes vote down vote up
@Test
public void testDeleteTable() throws Exception {
  String namespace = prefix + "_dummy";
  NamespaceDescriptor nspDesc =
      NamespaceDescriptor.create(namespace)
          .addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "100")
          .addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "3").build();
  ADMIN.createNamespace(nspDesc);
  assertNotNull("Namespace descriptor found null.", ADMIN.getNamespaceDescriptor(namespace));
  NamespaceTableAndRegionInfo stateInfo = getNamespaceState(nspDesc.getName());
  assertNotNull("Namespace state found null for " + namespace, stateInfo);
  HTableDescriptor tableDescOne =
      new HTableDescriptor(TableName.valueOf(namespace + TableName.NAMESPACE_DELIM + "table1"));
  HTableDescriptor tableDescTwo =
      new HTableDescriptor(TableName.valueOf(namespace + TableName.NAMESPACE_DELIM + "table2"));
  ADMIN.createTable(tableDescOne);
  ADMIN.createTable(tableDescTwo, Bytes.toBytes("AAA"), Bytes.toBytes("ZZZ"), 5);
  stateInfo = getNamespaceState(nspDesc.getName());
  assertNotNull("Namespace state found to be null.", stateInfo);
  assertEquals(2, stateInfo.getTables().size());
  assertEquals(5, stateInfo.getRegionCountOfTable(tableDescTwo.getTableName()));
  assertEquals(6, stateInfo.getRegionCount());
  ADMIN.disableTable(tableDescOne.getTableName());
  deleteTable(tableDescOne.getTableName());
  stateInfo = getNamespaceState(nspDesc.getName());
  assertNotNull("Namespace state found to be null.", stateInfo);
  assertEquals(5, stateInfo.getRegionCount());
  assertEquals(1, stateInfo.getTables().size());
  ADMIN.disableTable(tableDescTwo.getTableName());
  deleteTable(tableDescTwo.getTableName());
  ADMIN.deleteNamespace(namespace);
  stateInfo = getNamespaceState(namespace);
  assertNull("Namespace state not found to be null.", stateInfo);
}
 
Example 34
Project: ditb   File: TestSnapshotManager.java   Source Code and License 5 votes vote down vote up
@Test
public void testInProcess() throws KeeperException, IOException {
  TableName tableName = TableName.valueOf("testTable");
  SnapshotManager manager = getNewManager();
  TakeSnapshotHandler handler = Mockito.mock(TakeSnapshotHandler.class);
  assertFalse("Manager is in process when there is no current handler",
      manager.isTakingSnapshot(tableName));
  manager.setSnapshotHandlerForTesting(tableName, handler);
  Mockito.when(handler.isFinished()).thenReturn(false);
  assertTrue("Manager isn't in process when handler is running",
      manager.isTakingSnapshot(tableName));
  Mockito.when(handler.isFinished()).thenReturn(true);
  assertFalse("Manager is process when handler isn't running",
      manager.isTakingSnapshot(tableName));
}
 
Example 35
Project: ditb   File: TableRegionModel.java   Source Code and License 5 votes vote down vote up
/**
 * @return the region name
 */
@XmlAttribute
public String getName() {
  byte [] tableNameAsBytes = Bytes.toBytes(this.table);
  TableName tableName = TableName.valueOf(tableNameAsBytes);
  byte [] nameAsBytes = HRegionInfo.createRegionName(
    tableName, this.startKey, this.id, !tableName.isSystemTable());
  return Bytes.toString(nameAsBytes);
}
 
Example 36
Project: easyhbase   File: HbaseTemplate2.java   Source Code and License 5 votes vote down vote up
@Override
public <T> List<T> find(TableName tableName, final Scan scan, final AbstractRowKeyDistributor
        rowKeyDistributor, int limit, final RowMapper<T> action, final LimitEventHandler
        limitEventHandler) {
    final LimitRowMapperResultsExtractor<T> resultsExtractor = new
            LimitRowMapperResultsExtractor<>(action, limit, limitEventHandler);
    return executeDistributedScan(tableName, scan, rowKeyDistributor, resultsExtractor);
}
 
Example 37
Project: ditb   File: TestImportTSVWithOperationAttributes.java   Source Code and License 5 votes vote down vote up
@Test
public void testMROnTable() throws Exception {
  String tableName = "test-" + UUID.randomUUID();

  // Prepare the arguments required for the test.
  String[] args = new String[] {
      "-D" + ImportTsv.MAPPER_CONF_KEY
          + "=org.apache.hadoop.hbase.mapreduce.TsvImporterCustomTestMapperForOprAttr",
      "-D" + ImportTsv.COLUMNS_CONF_KEY + "=HBASE_ROW_KEY,FAM:A,FAM:B,HBASE_ATTRIBUTES_KEY",
      "-D" + ImportTsv.SEPARATOR_CONF_KEY + "=\u001b", tableName };
  String data = "KEY\u001bVALUE1\u001bVALUE2\u001btest=>myvalue\n";
  util.createTable(TableName.valueOf(tableName), FAMILY);
  doMROnTableTest(util, FAMILY, data, args, 1, true);
  util.deleteTable(tableName);
}
 
Example 38
Project: ditb   File: MasterFlushTableProcedureManager.java   Source Code and License 5 votes vote down vote up
@Override
public synchronized boolean isProcedureDone(ProcedureDescription desc) throws IOException {
  // Procedure instance name is the table name.
  TableName tableName = TableName.valueOf(desc.getInstance());
  Procedure proc = procMap.get(tableName);
  if (proc == null) {
    // The procedure has not even been started yet.
    // The client would request the procedure and call isProcedureDone().
    // The HBaseAdmin.execProcedure() wraps both request and isProcedureDone().
    return false;
  }
  // We reply on the existing Distributed Procedure framework to give us the status.
  return proc.isCompleted();
}
 
Example 39
Project: ditb   File: ConnectionManager.java   Source Code and License 5 votes vote down vote up
private RegionLocations locateMeta(final TableName tableName,
    boolean useCache, int replicaId) throws IOException {
  // HBASE-10785: We cache the location of the META itself, so that we are not overloading
  // zookeeper with one request for every region lookup. We cache the META with empty row
  // key in MetaCache.
  byte[] metaCacheKey = HConstants.EMPTY_START_ROW; // use byte[0] as the row for meta
  RegionLocations locations = null;
  if (useCache) {
    locations = getCachedLocation(tableName, metaCacheKey);
    if (locations != null && locations.getRegionLocation(replicaId) != null) {
      return locations;
    }
  }

  // only one thread should do the lookup.
  synchronized (metaRegionLock) {
    // Check the cache again for a hit in case some other thread made the
    // same query while we were waiting on the lock.
    if (useCache) {
      locations = getCachedLocation(tableName, metaCacheKey);
      if (locations != null && locations.getRegionLocation(replicaId) != null) {
        return locations;
      }
    }

    // Look up from zookeeper
    locations = this.registry.getMetaRegionLocation();
    if (locations != null) {
      cacheLocation(tableName, locations);
    }
  }
  return locations;
}
 
Example 40
Project: easyhbase   File: HbaseTemplate2.java   Source Code and License 5 votes vote down vote up
protected final <T> T executeParallelDistributedScan(TableName tableName, Scan scan,
                                                     AbstractRowKeyDistributor
                                                             rowKeyDistributor,
                                                     ResultsExtractor<T> action, int
                                                             numParallelThreads) {
    assertAccessAvailable();
    try {
        StopWatch watch = null;
        if (debugEnabled) {
            watch = new StopWatch();
            watch.start();
        }
        ParallelResultScanner scanner = new ParallelResultScanner(tableName, this, this
                .executor, scan, rowKeyDistributor, numParallelThreads);
        if (debugEnabled) {
            logger.debug("ParallelDistributedScanner createTime: {}ms", watch.stop());
            watch.start();
        }
        try {
            return action.extractData(scanner);
        } finally {
            scanner.close();
            if (debugEnabled) {
                logger.debug("ParallelDistributedScanner scanTime: {}ms", watch.stop());
            }
        }
    } catch (Throwable th) {
        Throwable throwable = th;
        if (th instanceof ScanTaskException) {
            throwable = th.getCause();
        }
        if (throwable instanceof Error) {
            throw ((Error) th);
        }
        if (throwable instanceof RuntimeException) {
            throw ((RuntimeException) th);
        }
        throw new HbaseSystemException((Exception) throwable);
    }
}
 
Example 41
Project: ditb   File: TestBulkDeleteProtocol.java   Source Code and License 5 votes vote down vote up
public void testBulkDeleteWithConditionBasedDelete() throws Throwable {
  TableName tableName = TableName.valueOf("testBulkDeleteWithConditionBasedDelete");
  Table ht = createTable(tableName);
  List<Put> puts = new ArrayList<Put>(100);
  for (int j = 0; j < 100; j++) {
    byte[] rowkey = Bytes.toBytes(j);
    String value = (j % 10 == 0) ? "v1" : "v2";
    puts.add(createPut(rowkey, value));
  }
  ht.put(puts);
  Scan scan = new Scan();
  FilterList fl = new FilterList(Operator.MUST_PASS_ALL);
  SingleColumnValueFilter scvf = new SingleColumnValueFilter(FAMILY1, QUALIFIER3,
      CompareOp.EQUAL, Bytes.toBytes("v1"));
  // fl.addFilter(new FirstKeyOnlyFilter());
  fl.addFilter(scvf);
  scan.setFilter(fl);
  // Deleting all the rows where cf1:c1=v1
  long noOfRowsDeleted = invokeBulkDeleteProtocol(tableName, scan, 500, DeleteType.ROW, null);
  assertEquals(10, noOfRowsDeleted);

  int rows = 0;
  for (Result result : ht.getScanner(new Scan())) {
    rows++;
  }
  assertEquals(90, rows);
  ht.close();
}
 
Example 42
Project: ditb   File: TestHRegionFileSystem.java   Source Code and License 5 votes vote down vote up
@Test
public void testNonIdempotentOpsWithRetries() throws IOException {
  Path rootDir = TEST_UTIL.getDataTestDirOnTestFS("testOnDiskRegionCreation");
  FileSystem fs = TEST_UTIL.getTestFileSystem();
  Configuration conf = TEST_UTIL.getConfiguration();

  // Create a Region
  HRegionInfo hri = new HRegionInfo(TableName.valueOf("TestTable"));
  HRegionFileSystem regionFs = HRegionFileSystem.createRegionOnFileSystem(conf, fs, rootDir, hri);
  assertTrue(fs.exists(regionFs.getRegionDir()));

  regionFs = new HRegionFileSystem(conf, new MockFileSystemForCreate(),
      null, null);
  // HRegionFileSystem.createRegionOnFileSystem(conf, new MockFileSystemForCreate(), rootDir,
  // hri);
  boolean result = regionFs.createDir(new Path("/foo/bar"));
  assertTrue("Couldn't create the directory", result);


  regionFs = new HRegionFileSystem(conf, new MockFileSystem(), null, null);
  result = regionFs.rename(new Path("/foo/bar"), new Path("/foo/bar2"));
  assertTrue("Couldn't rename the directory", result);

  regionFs = new HRegionFileSystem(conf, new MockFileSystem(), null, null);
  result = regionFs.deleteDir(new Path("/foo/bar"));
  assertTrue("Couldn't delete the directory", result);
  fs.delete(rootDir, true);
}
 
Example 43
Project: ditb   File: SchemaResource.java   Source Code and License 5 votes vote down vote up
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);
  }
}
 
Example 44
Project: ditb   File: TestFromClientSide.java   Source Code and License 5 votes vote down vote up
@Test
public void testKeyOnlyFilterWithReverseScan() throws Exception {
  TableName TABLE = TableName.valueOf("testKeyOnlyFilterWithReverseScan");
  Table ht = TEST_UTIL.createTable(TABLE, FAMILY);
  byte[][] ROWS = makeN(ROW, 10);
  byte[][] QUALIFIERS = { Bytes.toBytes("col0-<d2v1>-<d3v2>"),
      Bytes.toBytes("col1-<d2v1>-<d3v2>"),
      Bytes.toBytes("col2-<d2v1>-<d3v2>"),
      Bytes.toBytes("col3-<d2v1>-<d3v2>"),
      Bytes.toBytes("col4-<d2v1>-<d3v2>"),
      Bytes.toBytes("col5-<d2v1>-<d3v2>"),
      Bytes.toBytes("col6-<d2v1>-<d3v2>"),
      Bytes.toBytes("col7-<d2v1>-<d3v2>"),
      Bytes.toBytes("col8-<d2v1>-<d3v2>"),
      Bytes.toBytes("col9-<d2v1>-<d3v2>") };
  for (int i = 0; i < 10; i++) {
    Put put = new Put(ROWS[i]);
    put.add(FAMILY, QUALIFIERS[i], VALUE);
    ht.put(put);
  }
  Scan scan = new Scan();
  scan.setReversed(true);
  scan.addFamily(FAMILY);
  Filter filter = new KeyOnlyFilter(true);
  scan.setFilter(filter);
  ResultScanner scanner = ht.getScanner(scan);
  int count = 0;
  for (Result result : ht.getScanner(scan)) {
    assertEquals(result.size(), 1);
    assertEquals(result.raw()[0].getValueLength(), Bytes.SIZEOF_INT);
    assertEquals(Bytes.toInt(result.raw()[0].getValue()), VALUE.length);
    count++;
  }
  assertEquals(count, 10);
  scanner.close();
  ht.close();
}
 
Example 45
Project: ditb   File: NamespaceStateManager.java   Source Code and License 5 votes vote down vote up
synchronized void checkAndUpdateNamespaceTableCount(TableName table, int numRegions)
    throws IOException {
  String namespace = table.getNamespaceAsString();
  NamespaceDescriptor nspdesc = getNamespaceDescriptor(namespace);
  if (nspdesc != null) {
    NamespaceTableAndRegionInfo currentStatus;
    currentStatus = getState(nspdesc.getName());
    if ((currentStatus.getTables().size()) >= TableNamespaceManager.getMaxTables(nspdesc)) {
      throw new QuotaExceededException("The table " + table.getNameAsString()
          + " cannot be created as it would exceed maximum number of tables allowed "
          + " in the namespace.  The total number of tables permitted is "
          + TableNamespaceManager.getMaxTables(nspdesc));
    }
    if ((currentStatus.getRegionCount() + numRegions) > TableNamespaceManager
        .getMaxRegions(nspdesc)) {
      throw new QuotaExceededException("The table " + table.getNameAsString()
          + " is not allowed to have " + numRegions
          + " regions. The total number of regions permitted is only "
          + TableNamespaceManager.getMaxRegions(nspdesc) + ", while current region count is "
          + currentStatus.getRegionCount()
          + ". This may be transient, please retry later if there are any"
          + " ongoing split operations in the namespace.");
    }
  } else {
    throw new IOException("Namespace Descriptor found null for " + namespace
        + " This is unexpected.");
  }
  addTable(table, numRegions);
}
 
Example 46
Project: ditb   File: TestVisibilityLabels.java   Source Code and License 5 votes vote down vote up
@Test
public void testLabelsWithIncrement() throws Throwable {
  TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  try (Table table = TEST_UTIL.createTable(tableName, fam)) {
    byte[] row1 = Bytes.toBytes("row1");
    byte[] val = Bytes.toBytes(1L);
    Put put = new Put(row1);
    put.add(fam, qual, HConstants.LATEST_TIMESTAMP, val);
    put.setCellVisibility(new CellVisibility(SECRET + " & " + CONFIDENTIAL));
    table.put(put);
    Get get = new Get(row1);
    get.setAuthorizations(new Authorizations(SECRET));
    Result result = table.get(get);
    assertTrue(result.isEmpty());
    table.incrementColumnValue(row1, fam, qual, 2L);
    result = table.get(get);
    assertTrue(result.isEmpty());
    Increment increment = new Increment(row1);
    increment.addColumn(fam, qual, 2L);
    increment.setCellVisibility(new CellVisibility(SECRET));
    table.increment(increment);
    result = table.get(get);
    assertTrue(!result.isEmpty());
  }
}
 
Example 47
Project: ditb   File: IntegrationTestImportTsv.java   Source Code and License 5 votes vote down vote up
@Test
public void testGenerateAndLoad() throws Exception {
  LOG.info("Running test testGenerateAndLoad.");
  TableName table = TableName.valueOf(NAME + "-" + UUID.randomUUID());
  String cf = "d";
  Path hfiles = new Path(
      util.getDataTestDirOnTestFS(table.getNameAsString()), "hfiles");

  String[] args = {
      format("-D%s=%s", ImportTsv.BULK_OUTPUT_CONF_KEY, hfiles),
      format("-D%s=HBASE_ROW_KEY,HBASE_TS_KEY,%s:c1,%s:c2",
        ImportTsv.COLUMNS_CONF_KEY, cf, cf),
      // configure the test harness to NOT delete the HFiles after they're
      // generated. We need those for doLoadIncrementalHFiles
      format("-D%s=false", TestImportTsv.DELETE_AFTER_LOAD_CONF),
      table.getNameAsString()
  };

  // run the job, complete the load.
  util.createTable(table, new String[]{cf});
  Tool t = TestImportTsv.doMROnTableTest(util, cf, simple_tsv, args);
  doLoadIncrementalHFiles(hfiles, table);

  // validate post-conditions
  validateDeletedPartitionsFile(t.getConf());

  // clean up after ourselves.
  util.deleteTable(table);
  util.cleanupDataTestDirOnTestFS(table.getNameAsString());
  LOG.info("testGenerateAndLoad completed successfully.");
}
 
Example 48
Project: ditb   File: TestThriftHBaseServiceHandlerWithLabels.java   Source Code and License 5 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  SUPERUSER = User.createUserForTesting(conf, "admin",
      new String[] { "supergroup" });
  conf = UTIL.getConfiguration();
  conf.setClass(VisibilityUtils.VISIBILITY_LABEL_GENERATOR_CLASS,
      SimpleScanLabelGenerator.class, ScanLabelGenerator.class);
  conf.set("hbase.superuser", SUPERUSER.getShortName());
  conf.set("hbase.coprocessor.master.classes",
      VisibilityController.class.getName());
  conf.set("hbase.coprocessor.region.classes",
      VisibilityController.class.getName());
  conf.setInt("hfile.format.version", 3);
  UTIL.startMiniCluster(1);
  // Wait for the labels table to become available
  UTIL.waitTableEnabled(VisibilityConstants.LABELS_TABLE_NAME.getName(), 50000);
  createLabels();
  Admin admin = new HBaseAdmin(UTIL.getConfiguration());
  HTableDescriptor tableDescriptor = new HTableDescriptor(
      TableName.valueOf(tableAname));
  for (HColumnDescriptor family : families) {
    tableDescriptor.addFamily(family);
  }
  admin.createTable(tableDescriptor);
  admin.close();
  setAuths();
}
 
Example 49
Project: ditb   File: TestReplicationWALEntryFilters.java   Source Code and License 5 votes vote down vote up
@Test
public void testTableCfWALEntryFilter() {
  ReplicationPeer peer = mock(ReplicationPeer.class);

  when(peer.getTableCFs()).thenReturn(null);
  Entry userEntry = createEntry(a, b, c);
  TableCfWALEntryFilter filter = new TableCfWALEntryFilter(peer);
  assertEquals(createEntry(a,b,c), filter.filter(userEntry));

  // empty map
  userEntry = createEntry(a, b, c);
  Map<TableName, List<String>> tableCfs = new HashMap<TableName, List<String>>();
  when(peer.getTableCFs()).thenReturn(tableCfs);
  filter = new TableCfWALEntryFilter(peer);
  assertEquals(null, filter.filter(userEntry));

  // table bar
  userEntry = createEntry(a, b, c);
  tableCfs = new HashMap<TableName, List<String>>();
  tableCfs.put(TableName.valueOf("bar"), null);
  when(peer.getTableCFs()).thenReturn(tableCfs);
  filter = new TableCfWALEntryFilter(peer);
  assertEquals(null, filter.filter(userEntry));

  // table foo:a
  userEntry = createEntry(a, b, c);
  tableCfs = new HashMap<TableName, List<String>>();
  tableCfs.put(TableName.valueOf("foo"), Lists.newArrayList("a"));
  when(peer.getTableCFs()).thenReturn(tableCfs);
  filter = new TableCfWALEntryFilter(peer);
  assertEquals(createEntry(a), filter.filter(userEntry));

  // table foo:a,c
  userEntry = createEntry(a, b, c, d);
  tableCfs = new HashMap<TableName, List<String>>();
  tableCfs.put(TableName.valueOf("foo"), Lists.newArrayList("a", "c"));
  when(peer.getTableCFs()).thenReturn(tableCfs);
  filter = new TableCfWALEntryFilter(peer);
  assertEquals(createEntry(a,c), filter.filter(userEntry));
}
 
Example 50
Project: ditb   File: TestHRegionInfo.java   Source Code and License 5 votes vote down vote up
@Test
public void testRegionNameForRegionReplicas() throws Exception {
  String tableName = "tablename";
  final TableName tn = TableName.valueOf(tableName);
  String startKey = "startkey";
  final byte[] sk = Bytes.toBytes(startKey);
  String id = "id";

  // assert with only the region name without encoding

  // primary, replicaId = 0
  byte [] name = HRegionInfo.createRegionName(tn, sk, Bytes.toBytes(id), 0, false);
  String nameStr = Bytes.toString(name);
  assertEquals(tableName + "," + startKey + "," + id, nameStr);

  // replicaId = 1
  name = HRegionInfo.createRegionName(tn, sk, Bytes.toBytes(id), 1, false);
  nameStr = Bytes.toString(name);
  assertEquals(tableName + "," + startKey + "," + id + "_" +
    String.format(HRegionInfo.REPLICA_ID_FORMAT, 1), nameStr);

  // replicaId = max
  name = HRegionInfo.createRegionName(tn, sk, Bytes.toBytes(id), 0xFFFF, false);
  nameStr = Bytes.toString(name);
  assertEquals(tableName + "," + startKey + "," + id + "_" +
      String.format(HRegionInfo.REPLICA_ID_FORMAT, 0xFFFF), nameStr);
}
 
Example 51
Project: ditb   File: TestFromClientSide3.java   Source Code and License 5 votes vote down vote up
@Test
public void testGetEmptyRow() throws Exception {
  //Create a table and put in 1 row
  Admin admin = TEST_UTIL.getHBaseAdmin();
  HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(Bytes.toBytes("test")));
  desc.addFamily(new HColumnDescriptor(FAMILY));
  admin.createTable(desc);
  Table table = new HTable(TEST_UTIL.getConfiguration(), desc.getTableName());

  Put put = new Put(ROW_BYTES);
  put.add(FAMILY, COL_QUAL, VAL_BYTES);
  table.put(put);

  //Try getting the row with an empty row key
  Result res = null;
  try {
    res = table.get(new Get(new byte[0]));
    fail();
  } catch (IllegalArgumentException e) {
    // Expected.
  }
  assertTrue(res == null);
  res = table.get(new Get(Bytes.toBytes("r1-not-exist")));
  assertTrue(res.isEmpty() == true);
  res = table.get(new Get(ROW_BYTES));
  assertTrue(Arrays.equals(res.getValue(FAMILY, COL_QUAL), VAL_BYTES));
  table.close();
}
 
Example 52
Project: ditb   File: NamespaceTableAndRegionInfo.java   Source Code and License 5 votes vote down vote up
synchronized int getRegionCountOfTable(TableName tableName) {
  if (tableAndRegionInfo.containsKey(tableName)) {
    return this.tableAndRegionInfo.get(tableName).get();
  } else {
    return -1;
  }
}
 
Example 53
Project: ditb   File: TestTableMapReduceUtil.java   Source Code and License 4 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  UTIL.startMiniCluster();
  presidentsTable = createAndFillTable(TableName.valueOf(TABLE_NAME));
  UTIL.startMiniMapReduceCluster();
}
 
Example 54
Project: aliyun-tablestore-hbase-client   File: HelloWorld.java   Source Code and License 4 votes vote down vote up
private static void helloWorld() {
    try  {
        Configuration config = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(config);
        Admin admin = connection.getAdmin();

        HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
        descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME));

        System.out.println("Create table " + descriptor.getNameAsString());
        admin.createTable(descriptor);

        Table table = connection.getTable(TableName.valueOf(TABLE_NAME));

        System.out.println("Write one row to the table");
        Put put = new Put(ROW_KEY);
        put.addColumn(COLUMN_FAMILY_NAME, COLUMN_NAME, COLUMN_VALUE);
        table.put(put);

        Result getResult = table.get(new Get(ROW_KEY));
        String value = Bytes.toString(getResult.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME));
        System.out.println("Get a one row by row key");
        System.out.printf("\t%s = %s\n", Bytes.toString(ROW_KEY), value);

        Scan scan = new Scan();

        System.out.println("Scan for all rows:");
        ResultScanner scanner = table.getScanner(scan);
        for (Result row : scanner) {
            byte[] valueBytes = row.getValue(COLUMN_FAMILY_NAME, COLUMN_NAME);
            System.out.println('\t' + Bytes.toString(valueBytes));
        }

        System.out.println("Delete the table");
        admin.disableTable(table.getName());
        admin.deleteTable(table.getName());

        table.close();
        admin.close();
        connection.close();
    } catch (IOException e) {
        System.err.println("Exception while running HelloTablestore: " + e.toString());
        System.exit(1);
    }
}
 
Example 55
Project: ditb   File: MultiThreadedClientExample.java   Source Code and License 4 votes vote down vote up
public WriteExampleCallable(Connection connection, TableName tableName) {
  this.connection = connection;
  this.tableName = tableName;
}
 
Example 56
Project: ditb   File: DeleteColumnFamilyProcedure.java   Source Code and License 4 votes vote down vote up
@Override
public TableName getTableName() {
  return tableName;
}
 
Example 57
Project: ditb   File: TestNamespaceAuditor.java   Source Code and License 4 votes vote down vote up
@Override
public void preDeleteTable(ObserverContext<MasterCoprocessorEnvironment> ctx,
    TableName tableName) throws IOException {
  tableDeletionLatch = new CountDownLatch(1);
}
 
Example 58
Project: ditb   File: TestWALReplay.java   Source Code and License 4 votes vote down vote up
private HTableDescriptor createBasic1FamilyHTD(final TableName tableName) {
  HTableDescriptor htd = new HTableDescriptor(tableName);
  HColumnDescriptor a = new HColumnDescriptor(Bytes.toBytes("a"));
  htd.addFamily(a);
  return htd;
}
 
Example 59
Project: ditb   File: TestHCM.java   Source Code and License 4 votes vote down vote up
@Test
public void testClusterConnection() throws IOException {
  ThreadPoolExecutor otherPool = new ThreadPoolExecutor(1, 1,
      5, TimeUnit.SECONDS,
      new SynchronousQueue<Runnable>(),
      Threads.newDaemonThreadFactory("test-hcm"));

  HConnection con1 = HConnectionManager.createConnection(TEST_UTIL.getConfiguration());
  HConnection con2 = HConnectionManager.createConnection(TEST_UTIL.getConfiguration(), otherPool);
  // make sure the internally created ExecutorService is the one passed
  assertTrue(otherPool == ((HConnectionImplementation)con2).getCurrentBatchPool());

  String tableName = "testClusterConnection";
  TEST_UTIL.createTable(tableName.getBytes(), FAM_NAM).close();
  HTable t = (HTable)con1.getTable(tableName, otherPool);
  // make sure passing a pool to the getTable does not trigger creation of an internal pool
  assertNull("Internal Thread pool should be null", ((HConnectionImplementation)con1).getCurrentBatchPool());
  // table should use the pool passed
  assertTrue(otherPool == t.getPool());
  t.close();

  t = (HTable)con2.getTable(tableName);
  // table should use the connectin's internal pool
  assertTrue(otherPool == t.getPool());
  t.close();

  t = (HTable)con2.getTable(Bytes.toBytes(tableName));
  // try other API too
  assertTrue(otherPool == t.getPool());
  t.close();

  t = (HTable)con2.getTable(TableName.valueOf(tableName));
  // try other API too
  assertTrue(otherPool == t.getPool());
  t.close();

  t = (HTable)con1.getTable(tableName);
  ExecutorService pool = ((HConnectionImplementation)con1).getCurrentBatchPool();
  // make sure an internal pool was created
  assertNotNull("An internal Thread pool should have been created", pool);
  // and that the table is using it
  assertTrue(t.getPool() == pool);
  t.close();

  t = (HTable)con1.getTable(tableName);
  // still using the *same* internal pool
  assertTrue(t.getPool() == pool);
  t.close();

  con1.close();
  // if the pool was created on demand it should be closed upon connection close
  assertTrue(pool.isShutdown());

  con2.close();
  // if the pool is passed, it is not closed
  assertFalse(otherPool.isShutdown());
  otherPool.shutdownNow();
}
 
Example 60
Project: ditb   File: HBaseAdmin.java   Source Code and License 4 votes vote down vote up
/**
 * @param tableName name of table to check
 * @return true if table is off-line
 * @throws IOException if a remote or network exception occurs
 */
@Override
public boolean isTableDisabled(TableName tableName) throws IOException {
  checkTableExistence(tableName);
  return connection.isTableDisabled(tableName);
}