org.apache.hadoop.hbase.Cell类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(15.2k)|赞(0)|评价(0)|浏览(516)

本文整理了Java中org.apache.hadoop.hbase.Cell类的一些代码示例,展示了Cell类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cell类的具体详情如下:
包路径:org.apache.hadoop.hbase.Cell
类名称:Cell

Cell介绍

[英]The unit of storage in HBase consisting of the following fields:

1) row 
2) column family 
3) column qualifier 
4) timestamp 
5) type 
6) MVCC version 
7) value

Uniqueness is determined by the combination of row, column family, column qualifier, timestamp, and type.

The natural comparator will perform a bitwise comparison on row, column family, and column qualifier. Less intuitively, it will then treat the greater timestamp as the lesser value with the goal of sorting newer cells first.

Cell implements Comparable<Cell> which is only meaningful when comparing to other keys in the same table. It uses CellComparator which does not work on the -ROOT- and hbase:meta tables.

In the future, we may consider adding a boolean isOnHeap() method and a getValueBuffer() method that can be used to pass a value directly from an off-heap ByteBuffer to the network without copying into an on-heap byte[].

Historic note: the original Cell implementation (KeyValue) requires that all fields be encoded as consecutive bytes in the same byte[], whereas this interface allows fields to reside in separate byte[]'s.
[中]HBase中的存储单元,由以下字段组成:

1) row 
2) column family 
3) column qualifier 
4) timestamp 
5) type 
6) MVCC version 
7) value

唯一性由行、列族、列限定符、时间戳和类型的组合决定。
自然比较器将对行、列族和列限定符执行位比较。不那么直观的是,它会将较大的时间戳视为较小的值,目标是首先对较新的单元格进行排序。
Cell实现Comparable<Cell>,它仅在与同一表中的其他键进行比较时才有意义。它使用的CellComparator不适用于-ROOT和hbase:meta表。
在将来,我们可以考虑添加一个布尔ISONHEAPE()方法和一个GETValueBuffER()方法,该方法可以用来直接从一个堆堆ByteBuffer传递一个值到网络,而不复制到一个堆上字节[]。
历史注释:原始单元实现(KeyValue)要求所有字段编码为同一字节[]中的连续字节,而此接口允许字段驻留在单独的字节[]中。

代码示例

代码示例来源:origin: apache/hbase

public KeyValue(Cell c) {
 this(c.getRowArray(), c.getRowOffset(), c.getRowLength(),
   c.getFamilyArray(), c.getFamilyOffset(), c.getFamilyLength(),
   c.getQualifierArray(), c.getQualifierOffset(), c.getQualifierLength(),
   c.getTimestamp(), Type.codeToType(c.getTypeByte()), c.getValueArray(), c.getValueOffset(),
   c.getValueLength(), c.getTagsArray(), c.getTagsOffset(), c.getTagsLength());
 this.seqId = c.getSequenceId();
}

代码示例来源:origin: apache/hbase

CellWrapper(Cell cell) {
 assert !(cell instanceof ExtendedCell);
 this.cell = cell;
 this.sequenceId = cell.getSequenceId();
 this.timestamp = cell.getTimestamp();
}

代码示例来源:origin: apache/hbase

public RowColBloomHashKey(Cell cell) {
 super(cell);
 rowLength = cell.getRowLength();
 // We don't consider the family length for ROWCOL bloom. So subtract the famLen from the
 // length calculation. Timestamp and type are of no relevance here
 qualLength = cell.getQualifierLength();
}

代码示例来源:origin: apache/hbase

@Test
public void testSimpleVisibilityLabels() throws Exception {
 TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
 try (Table table = createTableAndWriteDataWithLabels(tableName, SECRET + "|" + CONFIDENTIAL,
   PRIVATE + "|" + CONFIDENTIAL)) {
  Scan s = new Scan();
  s.setAuthorizations(new Authorizations(SECRET, CONFIDENTIAL, PRIVATE));
  ResultScanner scanner = table.getScanner(s);
  Result[] next = scanner.next(3);
  assertTrue(next.length == 2);
  CellScanner cellScanner = next[0].cellScanner();
  cellScanner.advance();
  Cell current = cellScanner.current();
  assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
    current.getRowLength(), row1, 0, row1.length));
  cellScanner = next[1].cellScanner();
  cellScanner.advance();
  current = cellScanner.current();
  assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
    current.getRowLength(), row2, 0, row2.length));
 }
}

代码示例来源:origin: apache/hbase

Put put = new Put(ROWS[i]);
 put.addColumn(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(1, result.size());
 assertEquals(Bytes.SIZEOF_INT, result.rawCells()[0].getValueLength());
 assertEquals(VALUE.length, Bytes.toInt(CellUtil.cloneValue(result.rawCells()[0])));
 count++;
scanner.close();
ht.close();

代码示例来源:origin: apache/hbase

@Test
public void testBasics() throws IOException {
 final TableName tableName = TableName.valueOf(name.getMethodName());
 final byte [][] fs = new byte[][] {Bytes.toBytes("cf1"), Bytes.toBytes("cf2"),
  Bytes.toBytes("cf3") };
 Table ht = TEST_UTIL.createTable(tableName, fs);
  p.addColumn(f, f, f);
 ht.put(p);
 Result r = ht.get(new Get(row));
 int i = 0;
 for (CellScanner cellScanner = r.cellScanner(); cellScanner.advance();) {
  Cell cell = cellScanner.current();
  byte [] f = fs[i++];
  assertTrue(Bytes.toString(f),
   Bytes.equals(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(),
    f, 0, f.length));
 Get get = new Get(row);
 get.addFamily(f);
 r = ht.get(get);
 assertTrue(r.toString(), r.containsColumn(f, f));
 ResultScanner scanner = ht.getScanner(new Scan());
 int count = 0;
 while ((r = scanner.next()) != null) {
  assertTrue(r.listCells().size() == 3);
  count++;

代码示例来源:origin: apache/hbase

/**
 * The latest seqnum that the server writing to meta observed when opening the region.
 * E.g. the seqNum when the result of {@link #getServerName(Result, int)} was written.
 * @param r Result to pull the seqNum from
 * @return SeqNum, or HConstants.NO_SEQNUM if there's no value written.
 */
private static long getSeqNumDuringOpen(final Result r, final int replicaId) {
 Cell cell = r.getColumnLatestCell(getCatalogFamily(), getSeqNumColumn(replicaId));
 if (cell == null || cell.getValueLength() == 0) return HConstants.NO_SEQNUM;
 return Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
}

代码示例来源:origin: apache/hbase

private void verify(final Table table) throws IOException {
 Scan scan = new Scan();
 scan.addColumn(FAMILY_NAME, COLUMN_NAME);
 scan.setMaxVersions(1);
 ResultScanner scanner = table.getScanner(scan);
 for (Result r: scanner) {
  for (Cell kv : r.listCells()) {
   log.debug(Bytes.toString(r.getRow()) + "\t" + Bytes.toString(CellUtil.cloneFamily(kv))
     + "\t" + Bytes.toString(CellUtil.cloneQualifier(kv))
     + "\t" + kv.getTimestamp() + "\t" + Bytes.toBoolean(CellUtil.cloneValue(kv)));
   org.junit.Assert.assertEquals(TIMESTAMP.get(kv.getTimestamp()),
    Bytes.toBoolean(CellUtil.cloneValue(kv)));
  }
 }
 scanner.close();
}

代码示例来源:origin: apache/hbase

CellScanner cellScanner = next[0].cellScanner();
cellScanner.advance();
Cell current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
  current.getRowLength(), ROW_1, 0, ROW_1.length));
assertTrue(Bytes.equals(current.getQualifierArray(), current.getQualifierOffset(),
 current.getQualifierLength(), Q1, 0, Q1.length));
assertTrue(Bytes.equals(current.getValueArray(), current.getValueOffset(),
 current.getValueLength(), value1, 0, value1.length));
cellScanner.advance();
current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
  current.getRowLength(), ROW_1, 0, ROW_1.length));
assertTrue(Bytes.equals(current.getQualifierArray(), current.getQualifierOffset(),
 current.getQualifierLength(), Q2, 0, Q2.length));
assertTrue(Bytes.equals(current.getValueArray(), current.getValueOffset(),
 current.getValueLength(), value2, 0, value2.length));
cellScanner.advance();
current = cellScanner.current();
assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
  current.getRowLength(), ROW_1, 0, ROW_1.length));
assertTrue(Bytes.equals(current.getQualifierArray(), current.getQualifierOffset(),
 current.getQualifierLength(), Q3, 0, Q3.length));
assertTrue(Bytes.equals(current.getValueArray(), current.getValueOffset(),
 current.getValueLength(), value3, 0, value3.length));

代码示例来源:origin: apache/hbase

private Result scanAfterBulkLoad(ResultScanner scanner, Result result, String expctedVal)
  throws IOException {
 while (result != null) {
  List<Cell> cells = result.getColumnCells(Bytes.toBytes("col"), Bytes.toBytes("q"));
  for (Cell _c : cells) {
   if (Bytes.toString(_c.getRowArray(), _c.getRowOffset(), _c.getRowLength())
     .equals("row1")) {
    System.out
      .println(Bytes.toString(_c.getRowArray(), _c.getRowOffset(), _c.getRowLength()));
    System.out.println(Bytes.toString(_c.getQualifierArray(), _c.getQualifierOffset(),
     _c.getQualifierLength()));
    System.out.println(
     Bytes.toString(_c.getValueArray(), _c.getValueOffset(), _c.getValueLength()));
    Assert.assertEquals(expctedVal,
     Bytes.toString(_c.getValueArray(), _c.getValueOffset(), _c.getValueLength()));
   }
  }
  result = scanner.next();
 }
 return result;
}

代码示例来源:origin: apache/hbase

@Override
public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c, final Put put,
  final WALEdit edit, final Durability durability) throws IOException {
 if (put.getAttribute("ttl") != null) {
  Cell cell = put.getFamilyCellMap().values().stream().findFirst().get().get(0);
  ttls.put(
   TableName.valueOf(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(),
    cell.getQualifierLength())),
   Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
  c.bypass();
 } else if (put.getAttribute("versions") != null) {
  Cell cell = put.getFamilyCellMap().values().stream().findFirst().get().get(0);
  versions.put(
   TableName.valueOf(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(),
    cell.getQualifierLength())),
   Bytes.toInt(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
  c.bypass();
 }
}

代码示例来源:origin: apache/kylin

private void loadRecord(Result r) {
  Cell[] cells = r.rawCells();
  Cell cell = cells[0];
  if (Bytes.compareTo(CF_B, 0, CF_B.length, cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()) != 0 //
      || Bytes.compareTo(COL_B, 0, COL_B.length, cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()) != 0)
    throw new IllegalStateException();
  rec.loadCellBlock(0, ByteBuffer.wrap(cell.getRowArray(), cell.getRowOffset() + ID_LEN, cell.getRowLength() - ID_LEN));
  rec.loadCellBlock(1, ByteBuffer.wrap(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
}

代码示例来源:origin: apache/hbase

@Override
public void map(ImmutableBytesWritable key, Result value, Context context)
  throws IOException {
 for (Cell cell : value.listCells()) {
  context.getCounter(TestTableInputFormat.class.getName() + ":row",
    Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()))
    .increment(1l);
  context.getCounter(TestTableInputFormat.class.getName() + ":family",
    Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()))
    .increment(1l);
  context.getCounter(TestTableInputFormat.class.getName() + ":value",
    Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()))
    .increment(1l);
 }
}

代码示例来源:origin: apache/hbase

private void migrateNamespaceTable() throws IOException {
 try (Table nsTable = masterServices.getConnection().getTable(TableName.NAMESPACE_TABLE_NAME);
  ResultScanner scanner = nsTable.getScanner(
   new Scan().addFamily(TableDescriptorBuilder.NAMESPACE_FAMILY_INFO_BYTES).readAllVersions());
  BufferedMutator mutator =
   masterServices.getConnection().getBufferedMutator(TableName.META_TABLE_NAME)) {
  for (Result result;;) {
   result = scanner.next();
   if (result == null) {
    break;
   }
   Put put = new Put(result.getRow());
   result
    .getColumnCells(TableDescriptorBuilder.NAMESPACE_FAMILY_INFO_BYTES,
     TableDescriptorBuilder.NAMESPACE_COL_DESC_BYTES)
    .forEach(c -> put.addColumn(HConstants.NAMESPACE_FAMILY,
     HConstants.NAMESPACE_COL_DESC_QUALIFIER, c.getTimestamp(), CellUtil.cloneValue(c)));
   mutator.mutate(put);
  }
 }
 // schedule a disable procedure instead of block waiting here, as when disabling a table we will
 // wait until master is initialized, but we are part of the initialization...
 masterServices.getMasterProcedureExecutor().submitProcedure(
  new DisableTableProcedure(masterServices.getMasterProcedureExecutor().getEnvironment(),
   TableName.NAMESPACE_TABLE_NAME, false));
}

代码示例来源:origin: apache/hbase

@Test
 public void testFirstSeveralCellsFilterAndBatch() throws IOException {
  Scan scan = new Scan();
  scan.setFilter(new FirstSeveralCellsFilter());
  scan.setBatch(NUM_COLS);
  InternalScanner scanner = REGION.getScanner(scan);
  List<Cell> results = new ArrayList<>();
  for (int i = 0; i < NUM_ROWS; i++) {
   results.clear();
   scanner.next(results);
   assertEquals(NUM_COLS, results.size());
   Cell cell = results.get(0);
   assertArrayEquals(ROWS[i],
     Bytes.copy(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
   assertArrayEquals(FAMILIES[0],
     Bytes.copy(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()));
   assertArrayEquals(QUALIFIERS[0], Bytes.copy(cell.getQualifierArray(),
     cell.getQualifierOffset(), cell.getQualifierLength()));
  }
  assertFalse(scanner.next(results));
  scanner.close();
 }
}

代码示例来源:origin: apache/hbase

private void verifyRows(Table t, byte[] family, byte[] column) throws IOException {
 for (int i = 0; i < 10; i++) {
  byte[] row = Bytes.toBytes("row" + i);
  Get g = new Get(row).addFamily(family);
  Result r = t.get(g);
  Assert.assertNotNull(r);
  Assert.assertEquals(1, r.size());
  Cell cell = r.rawCells()[0];
  Assert.assertTrue(CellUtil.matchingQualifier(cell, column));
  Assert.assertEquals(Bytes.compareTo(cell.getValueArray(), cell.getValueOffset(),
   cell.getValueLength(), row, 0, row.length), 0);
 }
}

代码示例来源:origin: apache/hbase

@Test
public void testIncrementWithCustomTimestamp() throws IOException {
 TableName TABLENAME = TableName.valueOf(name.getMethodName());
 Table table = TEST_UTIL.createTable(TABLENAME, FAMILY);
 long timestamp = 999;
 Increment increment = new Increment(ROW);
 increment.add(CellUtil.createCell(ROW, FAMILY, QUALIFIER, timestamp, KeyValue.Type.Put.getCode(), Bytes.toBytes(100L)));
 Result r = table.increment(increment);
 assertEquals(1, r.size());
 assertEquals(timestamp, r.rawCells()[0].getTimestamp());
 r = table.get(new Get(ROW));
 assertEquals(1, r.size());
 assertEquals(timestamp, r.rawCells()[0].getTimestamp());
 r = table.increment(increment);
 assertEquals(1, r.size());
 assertNotEquals(timestamp, r.rawCells()[0].getTimestamp());
 r = table.get(new Get(ROW));
 assertEquals(1, r.size());
 assertNotEquals(timestamp, r.rawCells()[0].getTimestamp());
}

代码示例来源:origin: apache/hbase

@Test
 public void testAppendWithCustomTimestamp() throws IOException {
  TableName TABLENAME = TableName.valueOf(name.getMethodName());
  Table table = TEST_UTIL.createTable(TABLENAME, FAMILY);
  long timestamp = 999;
  Append append = new Append(ROW);
  append.add(CellUtil.createCell(ROW, FAMILY, QUALIFIER, timestamp, KeyValue.Type.Put.getCode(), Bytes.toBytes(100L)));
  Result r = table.append(append);
  assertEquals(1, r.size());
  assertEquals(timestamp, r.rawCells()[0].getTimestamp());
  r = table.get(new Get(ROW));
  assertEquals(1, r.size());
  assertEquals(timestamp, r.rawCells()[0].getTimestamp());
  r = table.append(append);
  assertEquals(1, r.size());
  assertNotEquals(timestamp, r.rawCells()[0].getTimestamp());
  r = table.get(new Get(ROW));
  assertEquals(1, r.size());
  assertNotEquals(timestamp, r.rawCells()[0].getTimestamp());
 }
}

代码示例来源:origin: apache/hbase

private static void verifyRow(Result result) throws IOException {
 byte[] row = result.getRow();
 CellScanner scanner = result.cellScanner();
 while (scanner.advance()) {
  Cell cell = scanner.current();
  //assert that all Cells in the Result have the same key
  Assert.assertEquals(0, Bytes.compareTo(row, 0, row.length,
    cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
 }
 for (int j = 0; j < FAMILIES.length; j++) {
  byte[] actual = result.getValue(FAMILIES[j], FAMILIES[j]);
  Assert.assertArrayEquals("Row in snapshot does not match, expected:" + Bytes.toString(row)
    + " ,actual:" + Bytes.toString(actual), row, actual);
 }
}

代码示例来源:origin: apache/hbase

private int calculateHashForKey(Cell cell) {
 // pre-calculate the 3 hashes made of byte ranges
 int rowHash = Bytes.hashCode(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
 int familyHash = Bytes.hashCode(cell.getFamilyArray(), cell.getFamilyOffset(),
   cell.getFamilyLength());
 int qualifierHash = Bytes.hashCode(cell.getQualifierArray(), cell.getQualifierOffset(),
   cell.getQualifierLength());
 // combine the 6 sub-hashes
 int hash = 31 * rowHash + familyHash;
 hash = 31 * hash + qualifierHash;
 hash = 31 * hash + (int) cell.getTimestamp();
 hash = 31 * hash + cell.getTypeByte();
 return hash;
}

相关文章