Java Code Examples for org.apache.hadoop.hbase.client.Table#get()

The following examples show how to use org.apache.hadoop.hbase.client.Table#get() . 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: TransactionAwareHTableTest.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
private void verifyRows(Table table, Get get, List<byte[]> expectedValues) throws Exception {
  Result result = table.get(get);
  if (expectedValues == null) {
    assertTrue(result.isEmpty());
  } else {
    assertFalse(result.isEmpty());
    byte[] family = TestBytes.family;
    byte[] col = TestBytes.qualifier;
    if (get.hasFamilies()) {
      family = get.getFamilyMap().keySet().iterator().next();
      col = get.getFamilyMap().get(family).first();
    }
    Iterator<Cell> it = result.getColumnCells(family, col).iterator();
    for (byte[] expectedValue : expectedValues) {
      Assert.assertTrue(it.hasNext());
      assertArrayEquals(expectedValue, CellUtil.cloneValue(it.next()));
    }
  }
}
 
Example 2
Source File: JobHistoryRawService.java    From hraven with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the raw job history file as a byte array stored for the given
 * cluster and job ID.
 * @param jobId the cluster and job ID to look up
 * @return the stored job history file contents or {@code null} if no
 *         corresponding record was found
 * @throws IOException
 */
public byte[] getRawJobHistoryBytes(QualifiedJobId jobId) throws IOException {
  byte[] historyData = null;
  byte[] rowKey = idConv.toBytes(jobId);
  Get get = new Get(rowKey);
  get.addColumn(Constants.RAW_FAM_BYTES, Constants.JOBHISTORY_COL_BYTES);
  Table rawTable = null;
  try {
    rawTable = hbaseConnection
        .getTable(TableName.valueOf(Constants.HISTORY_RAW_TABLE));
    Result result = rawTable.get(get);
    if (result != null && !result.isEmpty()) {
      historyData = result.getValue(Constants.RAW_FAM_BYTES,
          Constants.JOBHISTORY_COL_BYTES);
    }
  } finally {
    if (rawTable != null) {
      rawTable.close();
    }
  }
  return historyData;
}
 
Example 3
Source File: TestHelloHBase.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteRow() throws IOException {
  Admin admin = TEST_UTIL.getAdmin();
  admin.createNamespace(NamespaceDescriptor.create(HelloHBase.MY_NAMESPACE_NAME).build());
  Table table
          = TEST_UTIL.createTable(HelloHBase.MY_TABLE_NAME, HelloHBase.MY_COLUMN_FAMILY_NAME);

  table.put(new Put(HelloHBase.MY_ROW_ID).
          addColumn(HelloHBase.MY_COLUMN_FAMILY_NAME,
                  HelloHBase.MY_FIRST_COLUMN_QUALIFIER,
                  Bytes.toBytes("xyz")));
  HelloHBase.deleteRow(table);
  Result row = table.get(new Get(HelloHBase.MY_ROW_ID));
  assertEquals("#deleteRow failed to delete row.", true, row.isEmpty());

  TEST_UTIL.deleteTable(HelloHBase.MY_TABLE_NAME);
  admin.deleteNamespace(HelloHBase.MY_NAMESPACE_NAME);
}
 
Example 4
Source File: HBaseTest.java    From xxhadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetData() throws IOException {
	Connection connection = admin.getConnection();
	Table table = connection.getTable(TableName.valueOf("tbl_girls"));
	String rowKey = "0001";
	Get get = new Get(Bytes.toBytes(rowKey));
	get.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
	Result result = table.get(get);
	LOGGER.info(result.toString());
	List<Cell> cells = result.listCells();
	for (Cell cell : cells) {
		int age = Bytes.toInt(cell.getQualifierArray()); 
		LOGGER.info(String.valueOf(age));
	}
	
}
 
Example 5
Source File: SystemCatalogWALEntryFilterIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public WAL.Entry getEntry(TableName tableName, Get get) throws IOException {
  WAL.Entry entry = null;
  try(Connection conn = ConnectionFactory.createConnection(getUtility().getConfiguration())){
    Table htable = conn.getTable(tableName);
    Result result = htable.get(get);
    WALEdit edit = new WALEdit();
    if (result != null) {
      List<Cell> cellList = result.listCells();
      Assert.assertNotNull("Didn't retrieve any cells from SYSTEM.CATALOG", cellList);
      for (Cell c : cellList) {
        edit.add(c);
      }
    }
    Assert.assertTrue("Didn't retrieve any cells from SYSTEM.CATALOG",
        edit.getCells().size() > 0);
    WALKeyImpl key = new WALKeyImpl(REGION, tableName, 0, 0, uuid);
    entry = new WAL.Entry(key, edit);
  }
  return entry;
}
 
Example 6
Source File: EnrichmentLookup.java    From metron with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<LookupKV<EnrichmentKey, EnrichmentValue>> get( Iterable<KeyWithContext<EnrichmentKey, HBaseContext>> keys
                                                             , boolean logAccess
                                                             ) throws IOException
{
  if(Iterables.isEmpty(keys)) {
    return Collections.emptyList();
  }
  Table table = Iterables.getFirst(keys, null).getContext().getTable();
  List<LookupKV<EnrichmentKey, EnrichmentValue>> ret = new ArrayList<>();
  Iterator<KeyWithContext<EnrichmentKey, HBaseContext>> keyWithContextIterator = keys.iterator();
  for(Result result : table.get(keysToGets(keys))) {
    HBaseContext context = keyWithContextIterator.next().getContext();
    ret.add(converter.fromResult(result, getColumnFamily(context)));
  }
  return ret;
}
 
Example 7
Source File: JobHistoryByIdService.java    From hraven with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the JobKey for the job_history table, stored for this job ID, or
 * {@code null} if not found.
 * @param jobId the cluster and job ID combination to look up
 * @return the JobKey instance stored, or {@code null} if not found
 * @throws IOException if thrown by the HBase client
 */
public JobKey getJobKeyById(QualifiedJobId jobId) throws IOException {
  byte[] indexKey = jobIdConv.toBytes(jobId);

  Get g = new Get(indexKey);
  g.addColumn(Constants.INFO_FAM_BYTES, Constants.ROWKEY_COL_BYTES);
  Table historyByJobIdTable = null;

  try {
    historyByJobIdTable = hbaseConnection
        .getTable(TableName.valueOf(Constants.HISTORY_BY_JOBID_TABLE));
    Result r = historyByJobIdTable.get(g);
    if (r != null && !r.isEmpty()) {
      byte[] historyKey =
          r.getValue(Constants.INFO_FAM_BYTES, Constants.ROWKEY_COL_BYTES);
      if (historyKey != null && historyKey.length > 0) {
        return jobKeyConv.fromBytes(historyKey);
      }
    }
  } finally {
    if (historyByJobIdTable != null) {
      historyByJobIdTable.close();
    }
  }
  return null;
}
 
Example 8
Source File: ExtendCubeToHybridCLI.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private void copyAcl(String origCubeId, String newCubeId, String projectName) throws Exception {
    String projectResPath = ProjectInstance.concatResourcePath(projectName);
    Serializer<ProjectInstance> projectSerializer = new JsonSerializer<ProjectInstance>(ProjectInstance.class);
    ProjectInstance project = store.getResource(projectResPath, projectSerializer);
    String projUUID = project.getUuid();
    Table aclHtable = null;
    try {
        aclHtable = HBaseConnection.get(kylinConfig.getStorageUrl()).getTable(TableName.valueOf(kylinConfig.getMetadataUrlPrefix() + "_acl"));

        // cube acl
        Result result = aclHtable.get(new Get(Bytes.toBytes(origCubeId)));
        if (result.listCells() != null) {
            for (Cell cell : result.listCells()) {
                byte[] family = CellUtil.cloneFamily(cell);
                byte[] column = CellUtil.cloneQualifier(cell);
                byte[] value = CellUtil.cloneValue(cell);

                // use the target project uuid as the parent
                if (Bytes.toString(family).equals(ACL_INFO_FAMILY) && Bytes.toString(column).equals(ACL_INFO_FAMILY_PARENT_COLUMN)) {
                    String valueString = "{\"id\":\"" + projUUID + "\",\"type\":\"org.apache.kylin.metadata.project.ProjectInstance\"}";
                    value = Bytes.toBytes(valueString);
                }
                Put put = new Put(Bytes.toBytes(newCubeId));
                put.add(family, column, value);
                aclHtable.put(put);
            }
        }
    } finally {
        IOUtils.closeQuietly(aclHtable);
    }
}
 
Example 9
Source File: TestMultiSlaveReplication.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void putAndWait(byte[] row, byte[] fam, Table source, Table... targets)
throws Exception {
  Put put = new Put(row);
  put.addColumn(fam, row, row);
  source.put(put);

  Get get = new Get(row);
  for (int i = 0; i < NB_RETRIES; i++) {
    if (i==NB_RETRIES-1) {
      fail("Waited too much time for put replication");
    }
    boolean replicatedToAll = true;
    for (Table target : targets) {
      Result res = target.get(get);
      if (res.isEmpty()) {
        LOG.info("Row not available");
        replicatedToAll = false;
        break;
      } else {
        assertArrayEquals(res.value(), row);
      }
    }
    if (replicatedToAll) {
      break;
    } else {
      Thread.sleep(SLEEP_TIME);
    }
  }
}
 
Example 10
Source File: TestTxMgrFailover.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
protected void checkOperationSuccessOnCell(Table table,
                                           KeyValue.Type targetOp,
                                           @Nullable byte[] expectedValue,
                                           byte[] tableName,
                                           byte[] row,
                                           byte[] fam,
                                           byte[] col) {

    try {
        Get get = new Get(row).setMaxVersions(1);
        Result result = table.get(get);
        Cell latestCell = result.getColumnLatestCell(fam, col);

        switch (targetOp) {
            case Put:
                assertEquals(latestCell.getTypeByte(), targetOp.getCode());
                assertEquals(CellUtil.cloneValue(latestCell), expectedValue);
                LOG.trace("Value for " + Bytes.toString(tableName) + ":"
                        + Bytes.toString(row) + ":" + Bytes.toString(fam) + ":"
                        + Bytes.toString(col) + "=>" + Bytes.toString(CellUtil.cloneValue(latestCell))
                        + " (" + Bytes.toString(expectedValue) + " expected)");
                break;
            case Delete:
                LOG.trace("Value for " + Bytes.toString(tableName) + ":"
                        + Bytes.toString(row) + ":" + Bytes.toString(fam)
                        + Bytes.toString(col) + " deleted");
                assertNull(latestCell);
                break;
            default:
                fail();
        }
    } catch (IOException e) {
        LOG.error("Error reading row " + Bytes.toString(tableName) + ":"
                + Bytes.toString(row) + ":" + Bytes.toString(fam)
                + Bytes.toString(col), e);
        fail();
    }
}
 
Example 11
Source File: TestMetaTableAccessor.java    From hbase with Apache License 2.0 5 votes vote down vote up
public static void assertEmptyMetaLocation(Table meta, byte[] row, int replicaId)
  throws IOException {
  Get get = new Get(row);
  Result result = meta.get(get);
  Cell serverCell = result.getColumnLatestCell(HConstants.CATALOG_FAMILY,
    CatalogFamilyFormat.getServerColumn(replicaId));
  Cell startCodeCell = result.getColumnLatestCell(HConstants.CATALOG_FAMILY,
    CatalogFamilyFormat.getStartCodeColumn(replicaId));
  assertNotNull(serverCell);
  assertNotNull(startCodeCell);
  assertEquals(0, serverCell.getValueLength());
  assertEquals(0, startCodeCell.getValueLength());
}
 
Example 12
Source File: TestFileArchiverNotifierImpl.java    From hbase with Apache License 2.0 5 votes vote down vote up
private long extractSnapshotSize(
    Table quotaTable, TableName tn, String snapshot) throws IOException {
  Get g = QuotaTableUtil.makeGetForSnapshotSize(tn, snapshot);
  Result r = quotaTable.get(g);
  assertNotNull(r);
  CellScanner cs = r.cellScanner();
  assertTrue(cs.advance());
  Cell c = cs.current();
  assertNotNull(c);
  return QuotaTableUtil.extractSnapshotSize(
      c.getValueArray(), c.getValueOffset(), c.getValueLength());
}
 
Example 13
Source File: PermissionStorage.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Reads user permission assignments stored in the <code>l:</code> column family of the first
 * table row in <code>_acl_</code>.
 * <p>
 * See {@link PermissionStorage class documentation} for the key structure used for storage.
 * </p>
 */
static ListMultimap<String, UserPermission> getPermissions(Configuration conf, byte[] entryName,
    Table t, byte[] cf, byte[] cq, String user, boolean hasFilterUser) throws IOException {
  if (entryName == null) {
    entryName = ACL_GLOBAL_NAME;
  }
  // for normal user tables, we just read the table row from _acl_
  ListMultimap<String, UserPermission> perms = ArrayListMultimap.create();
  Get get = new Get(entryName);
  get.addFamily(ACL_LIST_FAMILY);
  Result row = null;
  if (t == null) {
    try (Connection connection = ConnectionFactory.createConnection(conf)) {
      try (Table table = connection.getTable(ACL_TABLE_NAME)) {
        row = table.get(get);
      }
    }
  } else {
    row = t.get(get);
  }
  if (!row.isEmpty()) {
    perms = parsePermissions(entryName, row, cf, cq, user, hasFilterUser);
  } else {
    LOG.info("No permissions found in " + ACL_TABLE_NAME + " for acl entry "
        + Bytes.toString(entryName));
  }

  return perms;
}
 
Example 14
Source File: TestAssignmentOnRSCrash.java    From hbase with Apache License 2.0 5 votes vote down vote up
public int testGet(final RegionInfo hri, final int nrows) throws IOException {
  int nresults = 0;
  final Table table = UTIL.getConnection().getTable(hri.getTable());
  for (int i = 0; i < nrows; ++i) {
    final byte[] row = Bytes.add(hri.getStartKey(), Bytes.toBytes(i));
    final Result result = table.get(new Get(row));
    if (result != null && !result.isEmpty() &&
        Bytes.equals(row, result.getValue(FAMILY, null))) {
      nresults++;
    }
  }
  return nresults;
}
 
Example 15
Source File: BigtableTargetIT.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 60000)
public void TestInsertWithExplicitColumnFamily() throws Exception {

  BigtableUtility btu = new BigtableUtility();
  btu.setupEnvironment();
  btu.startEmulator();

  BigtableConfigBean conf = new BigtableConfigBean();
  basicConfiguration(conf);
  conf.explicitFieldMapping = true;

  // add a rowkey
  conf.createCompositeRowKey = false;
  conf.singleColumnRowKey = "/rowkey";

  // metadata for column mapping.
  BigtableFieldMapping fMap = new BigtableFieldMapping();
  fMap.storageType = BigtableStorageType.TEXT;
  fMap.column = "aaa:aa";
  fMap.source = "/a";

  BigtableFieldMapping fMap1 = new BigtableFieldMapping();
  fMap1.storageType = BigtableStorageType.TEXT;
  fMap1.column = "bbb:bb";
  fMap1.source = "/b";

  BigtableFieldMapping fMap2 = new BigtableFieldMapping();
  fMap2.storageType = BigtableStorageType.TEXT;
  fMap2.column = "ccc:cc";
  fMap2.source = "/c";

  BigtableFieldMapping fMap3 = new BigtableFieldMapping();
  fMap3.storageType = BigtableStorageType.TEXT;
  fMap3.column = "d";
  fMap3.source = "/d";
  conf.fieldColumnMapping = ImmutableList.of(fMap, fMap1, fMap2, fMap3);

  BigtableTarget target = new BigtableTarget(conf);

  TargetRunner targetRunner = new TargetRunner.Builder(BigtableDTarget.class,
      target
  ).setOnRecordError(OnRecordError.DISCARD).build();

  assertTrue(targetRunner.runValidateConfigs().isEmpty());

  String rowkey = "row_aaa";
  Map<String, Field> map = new LinkedHashMap<>();
  map.put("rowkey", Field.create(rowkey));
  map.put("a", Field.create(20));
  map.put("b", Field.create(30));
  map.put("c", Field.create(40));
  map.put("d", Field.create(50));
  Record record = RecordCreator.create("s", "s:1");
  record.set(Field.create(map));

  //    assertTrue(targetRunner.runValidateConfigs().isEmpty());
  List<Record> singleRecord = ImmutableList.of(record);
  targetRunner.runInit();
  targetRunner.runWrite(singleRecord);

  // retrieve it.
  Table hTable = getTable();
  Get g = new Get(Bytes.toBytes(rowkey));
  g.addFamily(Bytes.toBytes("aaa"));
  g.addFamily(Bytes.toBytes("bbb"));
  g.addFamily(Bytes.toBytes("ccc"));
  g.addFamily(Bytes.toBytes(defaultColumnFamily));
  Result r = hTable.get(g);

  assertEquals("20", Bytes.toString(r.getValue(Bytes.toBytes("aaa"), Bytes.toBytes("aa"))));
  assertEquals("30", Bytes.toString(r.getValue(Bytes.toBytes("bbb"), Bytes.toBytes("bb"))));
  assertEquals("40", Bytes.toString(r.getValue(Bytes.toBytes("ccc"), Bytes.toBytes("cc"))));
  assertEquals("50", Bytes.toString(r.getValue(Bytes.toBytes(defaultColumnFamily), Bytes.toBytes("d"))));

  targetRunner.runDestroy();
  dropTable();
  btu.stopEmulator();
}
 
Example 16
Source File: TestGzipFilter.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testGzipFilter() throws Exception {
  String path = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;

  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  GZIPOutputStream os = new GZIPOutputStream(bos);
  os.write(VALUE_1);
  os.close();
  byte[] value_1_gzip = bos.toByteArray();

  // input side filter

  Header[] headers = new Header[2];
  headers[0] = new BasicHeader("Content-Type", Constants.MIMETYPE_BINARY);
  headers[1] = new BasicHeader("Content-Encoding", "gzip");
  Response response = client.put(path, headers, value_1_gzip);
  assertEquals(200, response.getCode());

  Table table = TEST_UTIL.getConnection().getTable(TABLE);
  Get get = new Get(Bytes.toBytes(ROW_1));
  get.addColumn(Bytes.toBytes(CFA), Bytes.toBytes("1"));
  Result result = table.get(get);
  byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
  assertNotNull(value);
  assertTrue(Bytes.equals(value, VALUE_1));

  // output side filter

  headers[0] = new BasicHeader("Accept", Constants.MIMETYPE_BINARY);
  headers[1] = new BasicHeader("Accept-Encoding", "gzip");
  response = client.get(path, headers);
  assertEquals(200, response.getCode());
  ByteArrayInputStream bis = new ByteArrayInputStream(response.getBody());
  GZIPInputStream is = new GZIPInputStream(bis);
  value = new byte[VALUE_1.length];
  is.read(value, 0, VALUE_1.length);
  assertTrue(Bytes.equals(value, VALUE_1));
  is.close();
  table.close();

  testScannerResultCodes();
}
 
Example 17
Source File: TestRegionObserverInterface.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
// HBase-3583
public void testHBase3583() throws IOException {
  final TableName tableName = TableName.valueOf(name.getMethodName());
  util.createTable(tableName, new byte[][] { A, B, C });
  util.waitUntilAllRegionsAssigned(tableName);

  verifyMethodResult(SimpleRegionObserver.class,
    new String[] { "hadPreGet", "hadPostGet", "wasScannerNextCalled", "wasScannerCloseCalled" },
    tableName, new Boolean[] { false, false, false, false });

  Table table = util.getConnection().getTable(tableName);
  Put put = new Put(ROW);
  put.addColumn(A, A, A);
  table.put(put);

  Get get = new Get(ROW);
  get.addColumn(A, A);
  table.get(get);

  // verify that scannerNext and scannerClose upcalls won't be invoked
  // when we perform get().
  verifyMethodResult(SimpleRegionObserver.class,
    new String[] { "hadPreGet", "hadPostGet", "wasScannerNextCalled", "wasScannerCloseCalled" },
    tableName, new Boolean[] { true, true, false, false });

  Scan s = new Scan();
  ResultScanner scanner = table.getScanner(s);
  try {
    for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
    }
  } finally {
    scanner.close();
  }

  // now scanner hooks should be invoked.
  verifyMethodResult(SimpleRegionObserver.class,
    new String[] { "wasScannerNextCalled", "wasScannerCloseCalled" }, tableName,
    new Boolean[] { true, true });
  util.deleteTable(tableName);
  table.close();
}
 
Example 18
Source File: TransactionAwareHTableTest.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
private Cell[] getRow(Table table, Get get) throws Exception {
  Result result = table.get(get);
  return result.rawCells();
}
 
Example 19
Source File: TransactionAwareHTableTest.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
private Cell[] getRow(Table table, Get get) throws Exception {
  Result result = table.get(get);
  return result.rawCells();
}
 
Example 20
Source File: TestMutateRowsRecovery.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void MutateRowsAndCheckPostKill() throws IOException, InterruptedException {
  final TableName tableName = TableName.valueOf("test");
  Admin admin = null;
  Table hTable = null;
  try {
    admin = connection.getAdmin();
    hTable = connection.getTable(tableName);
    TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
      new TableDescriptorBuilder.ModifyableTableDescriptor(tableName);
    tableDescriptor.setColumnFamily(
      new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(fam1));
    admin.createTable(tableDescriptor);

    // Add a multi
    RowMutations rm = new RowMutations(row1);
    Put p1 = new Put(row1);
    p1.addColumn(fam1, qual1, value1);
    p1.setDurability(Durability.SYNC_WAL);
    rm.add(p1);
    hTable.mutateRow(rm);

    // Add a put
    Put p2 = new Put(row1);
    p2.addColumn(fam1, qual2, value2);
    p2.setDurability(Durability.SYNC_WAL);
    hTable.put(p2);

    HRegionServer rs1 = TESTING_UTIL.getRSForFirstRegionInTable(tableName);
    long now = EnvironmentEdgeManager.currentTime();
    // Send the RS Load to ensure correct lastflushedseqid for stores
    rs1.tryRegionServerReport(now - 30000, now);
    // Kill the RS to trigger wal replay
    cluster.killRegionServer(rs1.serverName);

    // Ensure correct data exists
    Get g1 = new Get(row1);
    Result result = hTable.get(g1);
    assertTrue(result.getValue(fam1, qual1) != null);
    assertEquals(0, Bytes.compareTo(result.getValue(fam1, qual1), value1));
    assertTrue(result.getValue(fam1, qual2) != null);
    assertEquals(0, Bytes.compareTo(result.getValue(fam1, qual2), value2));
  } finally {
    if (admin != null) {
      admin.close();
    }
    if (hTable != null) {
      hTable.close();
    }
  }
}