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

x33g5p2x  于2022-01-16 转载在 其他  
字(17.2k)|赞(0)|评价(0)|浏览(237)

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

Append介绍

[英]Performs Append operations on a single row.

This operation ensures atomicty to readers. Appends are done under a single row lock, so write operations to a row are synchronized, and readers are guaranteed to see this operation fully completed.

To append to a set of columns of a row, instantiate an Append object with the row to append to. At least one column to append must be specified using the #addColumn(byte[],byte[],byte[]) method.
[中]对单行执行追加操作。
此操作确保了读卡器的原子性。追加是在单行锁下完成的,因此对行的写入操作是同步的,并且保证读卡器可以看到此操作完全完成。
要附加到行的一组列,请使用要附加到的行实例化一个append对象。必须使用#addColumn(byte[],byte[],byte[])方法指定至少一个要追加的列。

代码示例

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

AsyncTable<?> table = tableGetter.apply(TABLE_NAME);
table.putAll(IntStream.range(0, 7)
  .mapToObj(i -> new Put(Bytes.toBytes(i)).addColumn(FAMILY, CQ, Bytes.toBytes((long) i)))
  .collect(Collectors.toList())).get();
List<Row> actions = new ArrayList<>();
actions.add(new Get(Bytes.toBytes(0)));
actions.add(new Put(Bytes.toBytes(1)).addColumn(FAMILY, CQ, Bytes.toBytes(2L)));
actions.add(new Delete(Bytes.toBytes(2)));
actions.add(new Increment(Bytes.toBytes(3)).addColumn(FAMILY, CQ, 1));
actions.add(new Append(Bytes.toBytes(4)).addColumn(FAMILY, CQ, Bytes.toBytes(4)));
RowMutations rm = new RowMutations(Bytes.toBytes(5));
rm.add(new Put(Bytes.toBytes(5)).addColumn(FAMILY, CQ, Bytes.toBytes(100L)));
rm.add(new Put(Bytes.toBytes(5)).addColumn(FAMILY, CQ1, Bytes.toBytes(200L)));
actions.add(rm);
actions.add(new Get(Bytes.toBytes(6)));
assertEquals(0, Bytes.toLong(getResult.getValue(FAMILY, CQ)));
assertEquals(2, Bytes.toLong(table.get(new Get(Bytes.toBytes(1))).get().getValue(FAMILY, CQ)));
assertTrue(table.get(new Get(Bytes.toBytes(2))).get().isEmpty());
Result incrementResult = (Result) results.get(3);
assertEquals(4, Bytes.toLong(incrementResult.getValue(FAMILY, CQ)));

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

@Test
public void testAppendCopyConstructor() throws IOException {
 Append origin = new Append(Bytes.toBytes("ROW-01"));
 origin.setPriority(100);
 byte[] family = Bytes.toBytes("CF-01");
 origin.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY)
  .setRow(origin.getRow())
  .setFamily(family)
  .setQualifier(Bytes.toBytes("q"))
  .setType(Type.Put)
  .setValue(Bytes.toBytes(100))
  .build());
 origin.addColumn(family, Bytes.toBytes("q0"), Bytes.toBytes("value"));
 origin.setTimeRange(100, 1000);
 Append clone = new Append(origin);
 assertEquals(origin, clone);
 origin.addColumn(family, Bytes.toBytes("q1"), Bytes.toBytes("value"));
 //They should have different cell lists
 assertNotEquals(origin.getCellList(family), clone.getCellList(family));
}

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

public static TAppend appendFromHBase(Append in) throws IOException {
 TAppend out = new TAppend();
 out.setRow(in.getRow());
 if (in.getDurability() != Durability.USE_DEFAULT) {
  out.setDurability(durabilityFromHBase(in.getDurability()));
 for (Map.Entry<byte [], List<Cell>> entry : in.getFamilyCellMap().entrySet()) {
  byte[] family = entry.getKey();
  for (Cell cell : entry.getValue()) {
 for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
  out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
    ByteBuffer.wrap(attribute.getValue()));
  CellVisibility cellVisibility = in.getCellVisibility();
  if (cellVisibility != null) {
   TCellVisibility tCellVisibility = new TCellVisibility();
  throw new RuntimeException(e);
 out.setReturnResults(in.isReturnResults());
 return out;

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

private Append newAppendWithSkipWAL() {
 Append append = new Append(Bytes.toBytes("row"));
 append.addColumn(CF, CQ, Bytes.toBytes("value"));
 append.setDurability(Durability.SKIP_WAL);
 return append;
}

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

@Test(expected = DoNotRetryIOException.class)
public void testAppendWithDoNotRetryIOException() throws Exception {
 tableDoNotRetry
   .append(new Append(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("value")));
}

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

@Test
public void testAppend() throws Exception {
 testAppend(new Append(ROW_A).addColumn(TEST_FAMILY, qualifierCol1,
   Bytes.toBytes("value")));
 testAppend(new Append(ROW_A).addColumn(TEST_FAMILY, qualifierCol1,
   Bytes.toBytes("value")).setReturnResults(false));
}

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

org.apache.hadoop.hbase.coprocessor.ObserverContext<RegionCoprocessorEnvironment> e,
  Append append) throws IOException {
byte[] opBuf = append.getAttribute(OPERATION_ATTRIB);
if (opBuf == null) {
  return null;
Cell keyValue = append.getFamilyCellMap().values().iterator().next().iterator().next();
  maxGetTimestamp = minGetTimestamp + 1;
} else {
  clientTimestampBuf = append.getAttribute(MAX_TIMERANGE_ATTRIB);
  if (clientTimestampBuf != null) {
    clientTimestamp = maxGetTimestamp = Bytes.toLong(clientTimestampBuf);
byte[] row = append.getRow();
List<RowLock> locks = Lists.newArrayList();
region.startRegionOperation();
    switch (op) {
    case RETURN_SEQUENCE:
      KeyValue currentValueKV = PhoenixKeyValueUtil.maybeCopyCell(result.rawCells()[0]);
      long expectedValue = PLong.INSTANCE.getCodec().decodeLong(append.getAttribute(CURRENT_VALUE_ATTRIB), 0, SortOrder.getDefault());
      long value = PLong.INSTANCE.getCodec().decodeLong(currentValueKV.getValueArray(),
       currentValueKV.getValueOffset(), SortOrder.getDefault());
      m = new Put(row, currentValueKV.getTimestamp());
      m.getFamilyCellMap().putAll(append.getFamilyCellMap());
      break;
    case DROP_SEQUENCE:

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

@Test
public void testTagsWithAppendAndIncrement() throws Exception {
 TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
 byte[] f = Bytes.toBytes("f");
 byte[] q = Bytes.toBytes("q");
 byte[] row1 = Bytes.toBytes("r1");
 HColumnDescriptor colDesc = new HColumnDescriptor(f);
 desc.addFamily(colDesc);
 TEST_UTIL.getAdmin().createTable(desc);
  kv = KeyValueUtil.ensureKeyValue(result.getColumnLatestCell(f, q));
  kv = KeyValueUtil.ensureKeyValue(result.getColumnLatestCell(f, q));
  Append append = new Append(row3);
  append.addColumn(f, q, Bytes.toBytes("b"));
  TestCoprocessorForTags.tags = null;
  append = new Append(row3);
  append.add(new KeyValue(row3, f, q, 1234L, v));
  append.setAttribute("visibility", Bytes.toBytes("tag2"));
  append = new Append(row4);
  append.add(new KeyValue(row4, f, q, 1234L, v));
  append.setAttribute("visibility", Bytes.toBytes("tag2"));

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

Configuration conf = new Configuration(UTIL.getConfiguration());
UTIL.createTable(tableName, fam1).close();
table.delete(d);
counter = verifyCount(counter);
Put p2 = new Put(row);
p2.addColumn(fam1, Bytes.toBytes("qual"), Bytes.toBytes("val1"));
table.batch(Lists.newArrayList(p, p2), null);
Append append = new Append(row);
append.addColumn(fam1, fam1, Bytes.toBytes("val2"));
table.append(append);
counter = verifyCount(counter);
Get g = new Get(row);
table.get(g);
counter = verifyCount(counter);
counter = verifyCount(counter + 1);
Get g2 = new Get(row);
table.get(Lists.newArrayList(g, g2));
Get get = new Get(row);
get.setPriority(HConstants.ADMIN_QOS);
table.get(get);

代码示例来源:origin: larsgeorge/hbase-book

Put put = new Put(Bytes.toBytes("row10"));
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual10"),
 Bytes.toBytes("val10"));
table.put(put);
printStatistics(true, true);
Get get = new Get(Bytes.toBytes("row10"));
get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual10"));
table.get(get);
printStatistics(true, true);
batch.add(put);
batch.add(get);
table.batch(batch, results);
printStatistics(true, true);
RowMutations mutations = new RowMutations(Bytes.toBytes("row1"));
put = new Put(Bytes.toBytes("row1"));
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual10"),
 Bytes.toBytes("val10"));
Append append = new Append(Bytes.toBytes("row10"));
append.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual15"),
 Bytes.toBytes("-valnew"));
table.append(append);

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

byte[] rowKey = dataGenerator.getDeterministicUniqueKey(rowKeyBase);
Increment inc = new Increment(rowKey);
Append app = new Append(rowKey);
numKeys.addAndGet(1);
int columnCount = 0;
 inc.addColumn(cf, INCREMENT, cfHash);
 buf.setLength(0); // Clear the buffer
 buf.append("#").append(Bytes.toString(INCREMENT));
 buf.append(":").append(MutationType.INCREMENT.getNumber());
 app.addColumn(cf, MUTATE_INFO, Bytes.toBytes(buf.toString()));
 ++columnCount;
 if (!isBatchUpdate) {
  mutate(table, app, rowKeyBase);
  numCols.addAndGet(1);
  app = new Append(rowKey);
   default:
    buf.append(MutationType.APPEND.getNumber());
    app.addColumn(cf, column, hashCodeBytes);
   app.addColumn(cf, MUTATE_INFO, Bytes.toBytes(buf.toString()));
   if (!isBatchUpdate) {
    mutate(table, app, rowKeyBase);
    numCols.addAndGet(1);
    app = new Append(rowKey);

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

@Test
public void testMutation() throws Exception {
 table.increment(inc);
 value = Bytes.toBytes(20L);
 assertResult(table.get(new Get(ROW)), value, value);
 assertObserverHasExecuted();
 Append append = new Append(ROW);
 append.addColumn(FAMILY, QUALIFIER, APPEND_VALUE);
 table.append(append);
  .put(APPEND_VALUE)
  .array();
 assertResult(table.get(new Get(ROW)), value, value);
 assertObserverHasExecuted();
 delete.addColumns(FAMILY, QUALIFIER);
 table.delete(delete);
 assertTrue(Arrays.asList(table.get(new Get(ROW)).rawCells()).toString(),
  table.get(new Get(ROW)).isEmpty());
 assertObserverHasExecuted();
 assertObserverHasExecuted();
 assertTrue(table.get(new Get(ROW)).isEmpty());

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

@Test
public void testLabelsWithAppend() 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("a");
  Put put = new Put(row1);
  put.addColumn(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());
  Append append = new Append(row1);
  append.addColumn(fam, qual, Bytes.toBytes("b"));
  table.append(append);
  result = table.get(get);
  assertTrue(result.isEmpty());
  append = new Append(row1);
  append.addColumn(fam, qual, Bytes.toBytes("c"));
  append.setCellVisibility(new CellVisibility(SECRET));
  table.append(append);
  result = table.get(get);
  assertTrue(!result.isEmpty());
 }
}

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

@Test
public void testStandby() throws Exception {
 MasterFileSystem mfs = UTIL2.getHBaseCluster().getMaster().getMasterFileSystem();
 Path remoteWALDir = getRemoteWALDir(mfs, PEER_ID);
 assertFalse(mfs.getWALFileSystem().exists(remoteWALDir));
 UTIL2.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
  SyncReplicationState.STANDBY);
 assertTrue(mfs.getWALFileSystem().exists(remoteWALDir));
 try (Table table = UTIL2.getConnection().getTable(TABLE_NAME)) {
  assertDisallow(table, t -> t.get(new Get(Bytes.toBytes("row"))));
  assertDisallow(table,
   t -> t.put(new Put(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("row"))));
  assertDisallow(table, t -> t.delete(new Delete(Bytes.toBytes("row"))));
  assertDisallow(table, t -> t.incrementColumnValue(Bytes.toBytes("row"), CF, CQ, 1));
  assertDisallow(table,
   t -> t.append(new Append(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("row"))));
  assertDisallow(table,
   t -> t.get(Arrays.asList(new Get(Bytes.toBytes("row")), new Get(Bytes.toBytes("row1")))));
  assertDisallow(table,
   t -> t.put(
    Arrays.asList(new Put(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("row")),
     new Put(Bytes.toBytes("row1")).addColumn(CF, CQ, Bytes.toBytes("row1")))));
  assertDisallow(table, t -> t.mutateRow(new RowMutations(Bytes.toBytes("row"))

代码示例来源:origin: forcedotcom/phoenix

public Result preAppend(final ObserverContext<RegionCoprocessorEnvironment> e,
    final Append append) throws IOException {
  byte[] opBuf = append.getAttribute(OPERATION_ATTRIB);
  if (opBuf == null) {
    return null;
  KeyValue keyValue = append.getFamilyMap().values().iterator().next().iterator().next();
    maxGetTimestamp = minGetTimestamp + 1;
  } else {
    clientTimestampBuf = append.getAttribute(MAX_TIMERANGE_ATTRIB);
    if (clientTimestampBuf != null) {
      clientTimestamp = maxGetTimestamp = Bytes.toLong(clientTimestampBuf);
  e.complete();
  HRegion region = env.getRegion();
  byte[] row = append.getRow();
  region.startRegionOperation();
  try {
      switch (op) {
      case RETURN_SEQUENCE:
        KeyValue currentValueKV = result.raw()[0];
        long expectedValue = PDataType.LONG.getCodec().decodeLong(append.getAttribute(CURRENT_VALUE_ATTRIB), 0, null);
        long value = PDataType.LONG.getCodec().decodeLong(currentValueKV.getBuffer(), currentValueKV.getValueOffset(), null);
        m = new Put(row, currentValueKV.getTimestamp());
        m.getFamilyMap().putAll(append.getFamilyMap());
        break;

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

@Test
public void testNullQualifier() throws Exception {
 final TableName tableName = TableName.valueOf(name.getMethodName());
 Table table = TEST_UTIL.createTable(tableName, FAMILY);
 Put put = new Put(ROW);
 put.addColumn(FAMILY, null, VALUE);
 table.put(put);
 table.delete(delete);
 Get get = new Get(ROW);
 Result result = table.get(get);
 assertEmptyResult(result);
 table.delete(delete);
 Append append = new Append(ROW);
 append.addColumn(FAMILY, null, VALUE);
 table.append(append);
 getTestNull(table, ROW, FAMILY, VALUE);
 put = new Put(ROW);
 put.addColumn(FAMILY, null, Bytes.toBytes("checkAndPut"));
 table.put(put);
 table.checkAndMutate(ROW, FAMILY).ifEquals(VALUE).thenPut(put);
 mutate.add(new Put(ROW).addColumn(FAMILY, null, Bytes.toBytes("checkAndMutate")));
 table.checkAndMutate(ROW, FAMILY).ifEquals(Bytes.toBytes("checkAndPut")).thenMutate(mutate);

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

((FakeRSRpcServices)badRS.getRSRpcServices()).setExceptionInjector(
  new RoundRobinExceptionInjector());
Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
conf.set("hbase.client.retries.number", "1");
ConnectionImplementation conn =
try {
 Table table = conn.getTable(TABLE_NAME);
 byte[] row = Bytes.toBytes("row1");
 Put put = new Put(row);
 put.addColumn(FAMILY, QUALIFIER, Bytes.toBytes(10));
 Get get = new Get(row);
 Append append = new Append(row);
 append.addColumn(FAMILY, QUALIFIER, Bytes.toBytes(11));
 Increment increment = new Increment(row);
 increment.addColumn(FAMILY, QUALIFIER, 10);
  success = false;
  try {
   table.put(put);
   table.get(get);
   table.append(append);
   table.increment(increment);
   table.delete(delete);

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

desc.setConfiguration("hbase.rowlock.wait.duration", String.valueOf(5000));
desc.addFamily(new HColumnDescriptor(FAMILY));
TEST_UTIL.getAdmin().createTable(desc);
Configuration copy = new Configuration(TEST_UTIL.getConfiguration());
copy.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
try (Connection con = ConnectionFactory.createConnection(copy)) {
 putService.execute(() -> {
  try (Table table = con.getTable(tableName)) {
   Put put = new Put(ROW);
   put.addColumn(FAMILY, QUALIFIER, VALUE);
   table.put(put);
  } catch (IOException ex) {
   throw new RuntimeException(ex);
 ExecutorService appendService = Executors.newSingleThreadExecutor();
 appendService.execute(() -> {
  Append append = new Append(ROW);
  append.addColumn(FAMILY, QUALIFIER, VALUE);
  try (Table table = con.getTable(tableName)) {
   table.append(append);
   fail("The APPEND should fail because the target lock is blocked by previous put");
  } catch (Exception ex) {

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

@Test
public void testAppend() throws InterruptedException, ExecutionException {
 AsyncTable<?> table = getTable.get();
 int count = 10;
 CountDownLatch latch = new CountDownLatch(count);
 char suffix = ':';
 AtomicLong suffixCount = new AtomicLong(0L);
 IntStream.range(0, count).forEachOrdered(
  i -> table.append(new Append(row).addColumn(FAMILY, QUALIFIER, Bytes.toBytes("" + i + suffix)))
    .thenAccept(r -> {
     suffixCount.addAndGet(Bytes.toString(r.getValue(FAMILY, QUALIFIER)).chars()
       .filter(x -> x == suffix).count());
     latch.countDown();
    }));
 latch.await();
 assertEquals((1 + count) * count / 2, suffixCount.get());
 String value = Bytes.toString(
  table.get(new Get(row).addColumn(FAMILY, QUALIFIER)).get().getValue(FAMILY, QUALIFIER));
 int[] actual = Arrays.asList(value.split("" + suffix)).stream().mapToInt(Integer::parseInt)
   .sorted().toArray();
 assertArrayEquals(IntStream.range(0, count).toArray(), actual);
}

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

private List<Result> doAppend(final boolean walUsed) throws IOException {
 LOG.info("Starting testAppend, walUsed is " + walUsed);
 final TableName TABLENAME = TableName.valueOf(walUsed ? "testAppendWithWAL" : "testAppendWithoutWAL");
 Table t = TEST_UTIL.createTable(TABLENAME, FAMILY);
 final byte[] row1 = Bytes.toBytes("c");
 final byte[] row2 = Bytes.toBytes("b");
 final byte[] row3 = Bytes.toBytes("a");
 final byte[] qual = Bytes.toBytes("qual");
 Put put_0 = new Put(row2);
 put_0.addColumn(FAMILY, qual, Bytes.toBytes("put"));
 Put put_1 = new Put(row3);
 put_1.addColumn(FAMILY, qual, Bytes.toBytes("put"));
 Append append_0 = new Append(row1);
 append_0.addColumn(FAMILY, qual, Bytes.toBytes("i"));
 Append append_1 = new Append(row1);
 append_1.addColumn(FAMILY, qual, Bytes.toBytes("k"));
 Append append_2 = new Append(row1);
 append_2.addColumn(FAMILY, qual, Bytes.toBytes("e"));
 if (!walUsed) {
  append_2.setDurability(Durability.SKIP_WAL);
 Append append_3 = new Append(row1);
 append_3.addColumn(FAMILY, qual, Bytes.toBytes("a"));
 Scan s = new Scan();
 s.setCaching(1);
 t.append(append_0);
 t.put(put_0);
 t.put(put_1);

相关文章