Java Code Examples for org.apache.hadoop.hbase.client.Get#readVersions()

The following examples show how to use org.apache.hadoop.hbase.client.Get#readVersions() . 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: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Note: this public interface is slightly different from public Java APIs in regard to
 * handling of the qualifier. Here we differ from the public Java API in that null != byte[0].
 * Rather, we respect qual == null as a request for the entire column family. If you want to
 * access the entire column family, use
 * {@link #getVer(ByteBuffer, ByteBuffer, ByteBuffer, int, Map)} with a {@code column} value
 * that lacks a {@code ':'}.
 */
public List<TCell> getVer(ByteBuffer tableName, ByteBuffer row, byte[] family,
    byte[] qualifier, int numVersions, Map<ByteBuffer, ByteBuffer> attributes) throws IOError {

  Table table = null;
  try {
    table = getTable(tableName);
    Get get = new Get(getBytes(row));
    addAttributes(get, attributes);
    if (null == qualifier) {
      get.addFamily(family);
    } else {
      get.addColumn(family, qualifier);
    }
    get.readVersions(numVersions);
    Result result = table.get(get);
    return ThriftUtilities.cellFromHBase(result.rawCells());
  } catch (IOException e) {
    LOG.warn(e.getMessage(), e);
    throw getIOError(e);
  } finally{
    closeTable(table);
  }
}
 
Example 2
Source File: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Note: this internal interface is slightly different from public APIs in regard to handling
 * of the qualifier. Here we differ from the public Java API in that null != byte[0]. Rather,
 * we respect qual == null as a request for the entire column family. The caller (
 * {@link #getVerTs(ByteBuffer, ByteBuffer, ByteBuffer, long, int, Map)}) interface IS
 * consistent in that the column is parse like normal.
 */
protected List<TCell> getVerTs(ByteBuffer tableName, ByteBuffer row, byte[] family,
    byte[] qualifier, long timestamp, int numVersions, Map<ByteBuffer, ByteBuffer> attributes)
    throws IOError {

  Table table = null;
  try {
    table = getTable(tableName);
    Get get = new Get(getBytes(row));
    addAttributes(get, attributes);
    if (null == qualifier) {
      get.addFamily(family);
    } else {
      get.addColumn(family, qualifier);
    }
    get.setTimeRange(0, timestamp);
    get.readVersions(numVersions);
    Result result = table.get(get);
    return ThriftUtilities.cellFromHBase(result.rawCells());
  } catch (IOException e) {
    LOG.warn(e.getMessage(), e);
    throw getIOError(e);
  } finally{
    closeTable(table);
  }
}
 
Example 3
Source File: BackupSystemTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Creates Get operation for a given backup id
 * @param backupId backup's ID
 * @return get operation
 * @throws IOException exception
 */
private Get createGetForBackupInfo(String backupId) throws IOException {
  Get get = new Get(rowkey(BACKUP_INFO_PREFIX, backupId));
  get.addFamily(BackupSystemTable.SESSIONS_FAMILY);
  get.readVersions(1);
  return get;
}
 
Example 4
Source File: BackupSystemTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Creates Get operation to retrieve start code from backup system table
 * @return get operation
 * @throws IOException exception
 */
private Get createGetForStartCode(String rootPath) throws IOException {
  Get get = new Get(rowkey(START_CODE_ROW, rootPath));
  get.addFamily(BackupSystemTable.META_FAMILY);
  get.readVersions(1);
  return get;
}
 
Example 5
Source File: BackupSystemTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Creates Get to retrieve incremental backup table set from backup system table
 * @return get operation
 * @throws IOException exception
 */
private Get createGetForIncrBackupTableSet(String backupRoot) throws IOException {
  Get get = new Get(rowkey(INCR_BACKUP_SET, backupRoot));
  get.addFamily(BackupSystemTable.META_FAMILY);
  get.readVersions(1);
  return get;
}
 
Example 6
Source File: TestCoprocessorScanPolicy.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public void preGetOp(ObserverContext<RegionCoprocessorEnvironment> c, Get get,
    List<Cell> result) throws IOException {
  TableName tableName = c.getEnvironment().getRegion().getTableDescriptor().getTableName();
  Long ttl = this.ttls.get(tableName);
  if (ttl != null) {
    get.setTimeRange(EnvironmentEdgeManager.currentTime() - ttl, get.getTimeRange().getMax());
  }
  Integer version = this.versions.get(tableName);
  if (version != null) {
    get.readVersions(version);
  }
}
 
Example 7
Source File: TestSerialization.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testGet() throws Exception {
  byte[] row = Bytes.toBytes("row");
  byte[] fam = Bytes.toBytes("fam");
  byte[] qf1 = Bytes.toBytes("qf1");

  long ts = System.currentTimeMillis();
  int maxVersions = 2;

  Get get = new Get(row);
  get.addColumn(fam, qf1);
  get.setTimeRange(ts, ts + 1);
  get.readVersions(maxVersions);

  ClientProtos.Get getProto = ProtobufUtil.toGet(get);
  Get desGet = ProtobufUtil.toGet(getProto);

  assertTrue(Bytes.equals(get.getRow(), desGet.getRow()));
  Set<byte[]> set = null;
  Set<byte[]> desSet = null;

  for (Map.Entry<byte[], NavigableSet<byte[]>> entry : get.getFamilyMap().entrySet()) {
    assertTrue(desGet.getFamilyMap().containsKey(entry.getKey()));
    set = entry.getValue();
    desSet = desGet.getFamilyMap().get(entry.getKey());
    for (byte[] qualifier : set) {
      assertTrue(desSet.contains(qualifier));
    }
  }

  assertEquals(get.getMaxVersions(), desGet.getMaxVersions());
  TimeRange tr = get.getTimeRange();
  TimeRange desTr = desGet.getTimeRange();
  assertEquals(tr.getMax(), desTr.getMax());
  assertEquals(tr.getMin(), desTr.getMin());
}
 
Example 8
Source File: TestRemoteTable.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiGet() throws Exception {
  ArrayList<Get> gets = new ArrayList<>(2);
  gets.add(new Get(ROW_1));
  gets.add(new Get(ROW_2));
  Result[] results = remoteTable.get(gets);
  assertNotNull(results);
  assertEquals(2, results.length);
  assertEquals(1, results[0].size());
  assertEquals(2, results[1].size());

  //Test Versions
  gets = new ArrayList<>(2);
  Get g = new Get(ROW_1);
  g.readVersions(3);
  gets.add(g);
  gets.add(new Get(ROW_2));
  results = remoteTable.get(gets);
  assertNotNull(results);
  assertEquals(2, results.length);
  assertEquals(1, results[0].size());
  assertEquals(3, results[1].size());

  //404
  gets = new ArrayList<>(1);
  gets.add(new Get(Bytes.toBytes("RESALLYREALLYNOTTHERE")));
  results = remoteTable.get(gets);
  assertNotNull(results);
  assertEquals(0, results.length);

  gets = new ArrayList<>(3);
  gets.add(new Get(Bytes.toBytes("RESALLYREALLYNOTTHERE")));
  gets.add(new Get(ROW_1));
  gets.add(new Get(ROW_2));
  results = remoteTable.get(gets);
  assertNotNull(results);
  assertEquals(2, results.length);
}
 
Example 9
Source File: ThriftUtilities.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a {@link Get} (HBase) from a {@link TGet} (Thrift).
 *
 * This ignores any timestamps set on {@link TColumn} objects.
 *
 * @param in the <code>TGet</code> to convert
 *
 * @return <code>Get</code> object
 *
 * @throws IOException if an invalid time range or max version parameter is given
 */
public static Get getFromThrift(TGet in) throws IOException {
  Get out = new Get(in.getRow());

  // Timestamp overwrites time range if both are set
  if (in.isSetTimestamp()) {
    out.setTimestamp(in.getTimestamp());
  } else if (in.isSetTimeRange()) {
    out.setTimeRange(in.getTimeRange().getMinStamp(), in.getTimeRange().getMaxStamp());
  }

  if (in.isSetMaxVersions()) {
    out.readVersions(in.getMaxVersions());
  }

  if (in.isSetFilterString()) {
    ParseFilter parseFilter = new ParseFilter();
    out.setFilter(parseFilter.parseFilterString(in.getFilterString()));
  }

  if (in.isSetAttributes()) {
    addAttributes(out,in.getAttributes());
  }

  if (in.isSetAuthorizations()) {
    out.setAuthorizations(new Authorizations(in.getAuthorizations().getLabels()));
  }

  if (in.isSetConsistency()) {
    out.setConsistency(consistencyFromThrift(in.getConsistency()));
  }

  if (in.isSetTargetReplicaId()) {
    out.setReplicaId(in.getTargetReplicaId());
  }

  if (in.isSetCacheBlocks()) {
    out.setCacheBlocks(in.isCacheBlocks());
  }
  if (in.isSetStoreLimit()) {
    out.setMaxResultsPerColumnFamily(in.getStoreLimit());
  }
  if (in.isSetStoreOffset()) {
    out.setRowOffsetPerColumnFamily(in.getStoreOffset());
  }
  if (in.isSetExistence_only()) {
    out.setCheckExistenceOnly(in.isExistence_only());
  }

  if (in.isSetColumns()) {
    for (TColumn column : in.getColumns()) {
      if (column.isSetQualifier()) {
        out.addColumn(column.getFamily(), column.getQualifier());
      } else {
        out.addFamily(column.getFamily());
      }
    }
  }

  if (in.isSetFilterBytes()) {
    out.setFilter(filterFromThrift(in.getFilterBytes()));
  }
  return out;
}
 
Example 10
Source File: TestThriftConnection.java    From hbase with Apache License 2.0 4 votes vote down vote up
public void testMultiGet(Connection connection, String tableName) throws Exception {
  createTable(thriftAdmin, tableName);
  try (Table table = connection.getTable(TableName.valueOf(tableName))){
    ArrayList<Get> gets = new ArrayList<>(2);
    gets.add(new Get(ROW_1));
    gets.add(new Get(ROW_2));
    Result[] results = table.get(gets);
    assertNotNull(results);
    assertEquals(2, results.length);
    assertEquals(1, results[0].size());
    assertEquals(2, results[1].size());

    //Test Versions
    gets = new ArrayList<>(2);
    Get g = new Get(ROW_1);
    g.readVersions(3);
    gets.add(g);
    Get get2 = new Get(ROW_2);
    get2.readVersions(3);
    gets.add(get2);
    results = table.get(gets);
    assertNotNull(results);
    assertEquals(2, results.length);
    assertEquals(1, results[0].size());
    assertEquals(3, results[1].size());

    gets = new ArrayList<>(1);
    gets.add(new Get(Bytes.toBytes("RESALLYREALLYNOTTHERE")));
    results = table.get(gets);
    assertNotNull(results);
    assertTrue(results[0].isEmpty());

    gets = new ArrayList<>(3);
    gets.add(new Get(Bytes.toBytes("RESALLYREALLYNOTTHERE")));
    gets.add(new Get(ROW_1));
    gets.add(new Get(ROW_2));
    results = table.get(gets);
    assertNotNull(results);
    assertEquals(3, results.length);
    assertTrue(results[0].isEmpty());
  }

}
 
Example 11
Source File: RowResultGenerator.java    From hbase with Apache License 2.0 4 votes vote down vote up
public RowResultGenerator(final String tableName, final RowSpec rowspec,
    final Filter filter, final boolean cacheBlocks)
    throws IllegalArgumentException, IOException {
  try (Table table = RESTServlet.getInstance().getTable(tableName)) {
    Get get = new Get(rowspec.getRow());
    if (rowspec.hasColumns()) {
      for (byte[] col : rowspec.getColumns()) {
        byte[][] split = CellUtil.parseColumn(col);
        if (split.length == 1) {
          get.addFamily(split[0]);
        } else if (split.length == 2) {
          get.addColumn(split[0], split[1]);
        } else {
          throw new IllegalArgumentException("Invalid column specifier.");
        }
      }
    }
    get.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime());
    get.readVersions(rowspec.getMaxVersions());
    if (filter != null) {
      get.setFilter(filter);
    }
    get.setCacheBlocks(cacheBlocks);
    Result result = table.get(get);
    if (result != null && !result.isEmpty()) {
      valuesI = result.listCells().iterator();
    }
  } catch (DoNotRetryIOException e) {
    // Warn here because Stargate will return 404 in the case if multiple
    // column families were specified but one did not exist -- currently
    // HBase will fail the whole Get.
    // Specifying multiple columns in a URI should be uncommon usage but
    // help to avoid confusion by leaving a record of what happened here in
    // the log.
    LOG.warn(StringUtils.stringifyException(e));
    // Lets get the exception rethrown to get a more meaningful error message than 404
    if (e instanceof AccessDeniedException) {
      throw e;
    }
  }
}