Java Code Examples for org.apache.hadoop.hbase.client.RegionInfo#createRegionName()

The following examples show how to use org.apache.hadoop.hbase.client.RegionInfo#createRegionName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetaTableAccessor.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Performs a scan of META table for given table starting from given row.
 * @param connection connection we're using
 * @param visitor visitor to call
 * @param tableName table withing we scan
 * @param row start scan from this row
 * @param rowLimit max number of rows to return
 */
public static void scanMeta(Connection connection, final ClientMetaTableAccessor.Visitor visitor,
  final TableName tableName, final byte[] row, final int rowLimit) throws IOException {
  byte[] startRow = null;
  byte[] stopRow = null;
  if (tableName != null) {
    startRow = ClientMetaTableAccessor.getTableStartRowForMeta(tableName, QueryType.REGION);
    if (row != null) {
      RegionInfo closestRi = getClosestRegionInfo(connection, tableName, row);
      startRow =
        RegionInfo.createRegionName(tableName, closestRi.getStartKey(), HConstants.ZEROES, false);
    }
    stopRow = ClientMetaTableAccessor.getTableStopRowForMeta(tableName, QueryType.REGION);
  }
  scanMeta(connection, startRow, stopRow, QueryType.REGION, rowLimit, visitor);
}
 
Example 2
Source File: MetaTableAccessor.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * @return Get closest metatable region row to passed <code>row</code>
 */
@NonNull
private static RegionInfo getClosestRegionInfo(Connection connection,
  @NonNull final TableName tableName, @NonNull final byte[] row) throws IOException {
  byte[] searchRow = RegionInfo.createRegionName(tableName, row, HConstants.NINES, false);
  Scan scan = getMetaScan(connection, 1);
  scan.setReversed(true);
  scan.withStartRow(searchRow);
  try (ResultScanner resultScanner = getMetaHTable(connection).getScanner(scan)) {
    Result result = resultScanner.next();
    if (result == null) {
      throw new TableNotFoundException("Cannot find row in META " + " for table: " + tableName +
        ", row=" + Bytes.toStringBinary(row));
    }
    RegionInfo regionInfo = CatalogFamilyFormat.getRegionInfo(result);
    if (regionInfo == null) {
      throw new IOException("RegionInfo was null or empty in Meta for " + tableName + ", row=" +
        Bytes.toStringBinary(row));
    }
    return regionInfo;
  }
}
 
Example 3
Source File: TestThriftServer.java    From hbase with Apache License 2.0 6 votes vote down vote up
public static void doTestGetRegionInfo(Hbase.Iface handler) throws Exception {
  // Create tableA and add two columns to rowA
  handler.createTable(tableAname, getColumnDescriptors());
  try {
    handler.mutateRow(tableAname, rowAname, getMutations(), null);
    byte[] searchRow = RegionInfo.createRegionName(
        TableName.valueOf(tableAname.array()), rowAname.array(),
        HConstants.NINES, false);
    TRegionInfo regionInfo = handler.getRegionInfo(ByteBuffer.wrap(searchRow));
    assertTrue(Bytes.toStringBinary(regionInfo.getName()).startsWith(
          Bytes.toStringBinary(tableAname)));
  } finally {
    handler.disableTable(tableAname);
    handler.deleteTable(tableAname);
  }
}
 
Example 4
Source File: TestGetClosestAtOrBefore.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * @param answer Pass -1 if we're not to find anything.
 * @return Row found.
 */
private byte[] findRow(final Region mr, final char table, final int rowToFind, final int answer)
  throws IOException {
  TableName tableb = TableName.valueOf("" + table);
  // Find the row.
  byte[] tofindBytes = Bytes.toBytes((short) rowToFind);
  byte[] metaKey = RegionInfo.createRegionName(tableb, tofindBytes, HConstants.NINES, false);
  LOG.info("find=" + new String(metaKey, StandardCharsets.UTF_8));
  Result r = UTIL.getClosestRowBefore(mr, metaKey, HConstants.CATALOG_FAMILY);
  if (answer == -1) {
    assertNull(r);
    return null;
  }
  assertTrue(
    Bytes.compareTo(Bytes.toBytes((short) answer), extractRowFromMetaRow(r.getRow())) == 0);
  return r.getRow();
}
 
Example 5
Source File: MetaTableAccessor.java    From hbase with Apache License 2.0 5 votes vote down vote up
public static ReplicationBarrierResult getReplicationBarrierResult(Connection conn,
  TableName tableName, byte[] row, byte[] encodedRegionName) throws IOException {
  byte[] metaStartKey = RegionInfo.createRegionName(tableName, row, HConstants.NINES, false);
  byte[] metaStopKey =
    RegionInfo.createRegionName(tableName, HConstants.EMPTY_START_ROW, "", false);
  Scan scan = new Scan().withStartRow(metaStartKey).withStopRow(metaStopKey)
    .addColumn(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER)
    .addFamily(HConstants.REPLICATION_BARRIER_FAMILY).readAllVersions().setReversed(true)
    .setCaching(10);
  try (Table table = getMetaHTable(conn); ResultScanner scanner = table.getScanner(scan)) {
    for (Result result;;) {
      result = scanner.next();
      if (result == null) {
        return new ReplicationBarrierResult(new long[0], null, Collections.emptyList());
      }
      byte[] regionName = result.getRow();
      // TODO: we may look up a region which has already been split or merged so we need to check
      // whether the encoded name matches. Need to find a way to quit earlier when there is no
      // record for the given region, for now it will scan to the end of the table.
      if (!Bytes.equals(encodedRegionName,
        Bytes.toBytes(RegionInfo.encodeRegionName(regionName)))) {
        continue;
      }
      return getReplicationBarrierResult(result);
    }
  }
}
 
Example 6
Source File: SnapshotManifest.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Extract the region encoded name from the region manifest
 */
static String getRegionNameFromManifest(final SnapshotRegionManifest manifest) {
  byte[] regionName = RegionInfo.createRegionName(
          ProtobufUtil.toTableName(manifest.getRegionInfo().getTableName()),
          manifest.getRegionInfo().getStartKey().toByteArray(),
          manifest.getRegionInfo().getRegionId(), true);
  return RegionInfo.encodeRegionName(regionName);
}
 
Example 7
Source File: TestRegionInfo.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseName() throws IOException {
  final TableName tableName = TableName.valueOf(name.getMethodName());
  byte[] startKey = Bytes.toBytes("startKey");
  long regionId = System.currentTimeMillis();
  int replicaId = 42;

  // test without replicaId
  byte[] regionName = RegionInfo.createRegionName(tableName, startKey, regionId, false);

  byte[][] fields = RegionInfo.parseRegionName(regionName);
  assertArrayEquals(Bytes.toString(fields[0]),tableName.getName(), fields[0]);
  assertArrayEquals(Bytes.toString(fields[1]),startKey, fields[1]);
  assertArrayEquals(Bytes.toString(fields[2]), Bytes.toBytes(Long.toString(regionId)),fields[2]);
  assertEquals(3, fields.length);

  // test with replicaId
  regionName = RegionInfo.createRegionName(tableName, startKey, regionId,
    replicaId, false);

  fields = RegionInfo.parseRegionName(regionName);
  assertArrayEquals(Bytes.toString(fields[0]),tableName.getName(), fields[0]);
  assertArrayEquals(Bytes.toString(fields[1]),startKey, fields[1]);
  assertArrayEquals(Bytes.toString(fields[2]), Bytes.toBytes(Long.toString(regionId)),fields[2]);
  assertArrayEquals(Bytes.toString(fields[3]), Bytes.toBytes(
    String.format(RegionInfo.REPLICA_ID_FORMAT, replicaId)), fields[3]);
}
 
Example 8
Source File: TableRegionModel.java    From hbase with Apache License 2.0 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 = RegionInfo.createRegionName(
    tableName, this.startKey, this.id, !tableName.isSystemTable());
  return Bytes.toString(nameAsBytes);
}