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

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

本文整理了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

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

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

  1. @Test
  2. public void testAppendCopyConstructor() throws IOException {
  3. Append origin = new Append(Bytes.toBytes("ROW-01"));
  4. origin.setPriority(100);
  5. byte[] family = Bytes.toBytes("CF-01");
  6. origin.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY)
  7. .setRow(origin.getRow())
  8. .setFamily(family)
  9. .setQualifier(Bytes.toBytes("q"))
  10. .setType(Type.Put)
  11. .setValue(Bytes.toBytes(100))
  12. .build());
  13. origin.addColumn(family, Bytes.toBytes("q0"), Bytes.toBytes("value"));
  14. origin.setTimeRange(100, 1000);
  15. Append clone = new Append(origin);
  16. assertEquals(origin, clone);
  17. origin.addColumn(family, Bytes.toBytes("q1"), Bytes.toBytes("value"));
  18. //They should have different cell lists
  19. assertNotEquals(origin.getCellList(family), clone.getCellList(family));
  20. }

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

  1. public static TAppend appendFromHBase(Append in) throws IOException {
  2. TAppend out = new TAppend();
  3. out.setRow(in.getRow());
  4. if (in.getDurability() != Durability.USE_DEFAULT) {
  5. out.setDurability(durabilityFromHBase(in.getDurability()));
  6. for (Map.Entry<byte [], List<Cell>> entry : in.getFamilyCellMap().entrySet()) {
  7. byte[] family = entry.getKey();
  8. for (Cell cell : entry.getValue()) {
  9. for (Map.Entry<String, byte[]> attribute : in.getAttributesMap().entrySet()) {
  10. out.putToAttributes(ByteBuffer.wrap(Bytes.toBytes(attribute.getKey())),
  11. ByteBuffer.wrap(attribute.getValue()));
  12. CellVisibility cellVisibility = in.getCellVisibility();
  13. if (cellVisibility != null) {
  14. TCellVisibility tCellVisibility = new TCellVisibility();
  15. throw new RuntimeException(e);
  16. out.setReturnResults(in.isReturnResults());
  17. return out;

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

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

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

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

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

  1. @Test
  2. public void testAppend() throws Exception {
  3. testAppend(new Append(ROW_A).addColumn(TEST_FAMILY, qualifierCol1,
  4. Bytes.toBytes("value")));
  5. testAppend(new Append(ROW_A).addColumn(TEST_FAMILY, qualifierCol1,
  6. Bytes.toBytes("value")).setReturnResults(false));
  7. }

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

  1. org.apache.hadoop.hbase.coprocessor.ObserverContext<RegionCoprocessorEnvironment> e,
  2. Append append) throws IOException {
  3. byte[] opBuf = append.getAttribute(OPERATION_ATTRIB);
  4. if (opBuf == null) {
  5. return null;
  6. Cell keyValue = append.getFamilyCellMap().values().iterator().next().iterator().next();
  7. maxGetTimestamp = minGetTimestamp + 1;
  8. } else {
  9. clientTimestampBuf = append.getAttribute(MAX_TIMERANGE_ATTRIB);
  10. if (clientTimestampBuf != null) {
  11. clientTimestamp = maxGetTimestamp = Bytes.toLong(clientTimestampBuf);
  12. byte[] row = append.getRow();
  13. List<RowLock> locks = Lists.newArrayList();
  14. region.startRegionOperation();
  15. switch (op) {
  16. case RETURN_SEQUENCE:
  17. KeyValue currentValueKV = PhoenixKeyValueUtil.maybeCopyCell(result.rawCells()[0]);
  18. long expectedValue = PLong.INSTANCE.getCodec().decodeLong(append.getAttribute(CURRENT_VALUE_ATTRIB), 0, SortOrder.getDefault());
  19. long value = PLong.INSTANCE.getCodec().decodeLong(currentValueKV.getValueArray(),
  20. currentValueKV.getValueOffset(), SortOrder.getDefault());
  21. m = new Put(row, currentValueKV.getTimestamp());
  22. m.getFamilyCellMap().putAll(append.getFamilyCellMap());
  23. break;
  24. case DROP_SEQUENCE:

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

  1. @Test
  2. public void testTagsWithAppendAndIncrement() throws Exception {
  3. TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  4. byte[] f = Bytes.toBytes("f");
  5. byte[] q = Bytes.toBytes("q");
  6. byte[] row1 = Bytes.toBytes("r1");
  7. HColumnDescriptor colDesc = new HColumnDescriptor(f);
  8. desc.addFamily(colDesc);
  9. TEST_UTIL.getAdmin().createTable(desc);
  10. kv = KeyValueUtil.ensureKeyValue(result.getColumnLatestCell(f, q));
  11. kv = KeyValueUtil.ensureKeyValue(result.getColumnLatestCell(f, q));
  12. Append append = new Append(row3);
  13. append.addColumn(f, q, Bytes.toBytes("b"));
  14. TestCoprocessorForTags.tags = null;
  15. append = new Append(row3);
  16. append.add(new KeyValue(row3, f, q, 1234L, v));
  17. append.setAttribute("visibility", Bytes.toBytes("tag2"));
  18. append = new Append(row4);
  19. append.add(new KeyValue(row4, f, q, 1234L, v));
  20. append.setAttribute("visibility", Bytes.toBytes("tag2"));

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

  1. Configuration conf = new Configuration(UTIL.getConfiguration());
  2. UTIL.createTable(tableName, fam1).close();
  3. table.delete(d);
  4. counter = verifyCount(counter);
  5. Put p2 = new Put(row);
  6. p2.addColumn(fam1, Bytes.toBytes("qual"), Bytes.toBytes("val1"));
  7. table.batch(Lists.newArrayList(p, p2), null);
  8. Append append = new Append(row);
  9. append.addColumn(fam1, fam1, Bytes.toBytes("val2"));
  10. table.append(append);
  11. counter = verifyCount(counter);
  12. Get g = new Get(row);
  13. table.get(g);
  14. counter = verifyCount(counter);
  15. counter = verifyCount(counter + 1);
  16. Get g2 = new Get(row);
  17. table.get(Lists.newArrayList(g, g2));
  18. Get get = new Get(row);
  19. get.setPriority(HConstants.ADMIN_QOS);
  20. table.get(get);

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

  1. Put put = new Put(Bytes.toBytes("row10"));
  2. put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual10"),
  3. Bytes.toBytes("val10"));
  4. table.put(put);
  5. printStatistics(true, true);
  6. Get get = new Get(Bytes.toBytes("row10"));
  7. get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual10"));
  8. table.get(get);
  9. printStatistics(true, true);
  10. batch.add(put);
  11. batch.add(get);
  12. table.batch(batch, results);
  13. printStatistics(true, true);
  14. RowMutations mutations = new RowMutations(Bytes.toBytes("row1"));
  15. put = new Put(Bytes.toBytes("row1"));
  16. put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual10"),
  17. Bytes.toBytes("val10"));
  18. Append append = new Append(Bytes.toBytes("row10"));
  19. append.add(Bytes.toBytes("colfam1"), Bytes.toBytes("qual15"),
  20. Bytes.toBytes("-valnew"));
  21. table.append(append);

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

  1. byte[] rowKey = dataGenerator.getDeterministicUniqueKey(rowKeyBase);
  2. Increment inc = new Increment(rowKey);
  3. Append app = new Append(rowKey);
  4. numKeys.addAndGet(1);
  5. int columnCount = 0;
  6. inc.addColumn(cf, INCREMENT, cfHash);
  7. buf.setLength(0); // Clear the buffer
  8. buf.append("#").append(Bytes.toString(INCREMENT));
  9. buf.append(":").append(MutationType.INCREMENT.getNumber());
  10. app.addColumn(cf, MUTATE_INFO, Bytes.toBytes(buf.toString()));
  11. ++columnCount;
  12. if (!isBatchUpdate) {
  13. mutate(table, app, rowKeyBase);
  14. numCols.addAndGet(1);
  15. app = new Append(rowKey);
  16. default:
  17. buf.append(MutationType.APPEND.getNumber());
  18. app.addColumn(cf, column, hashCodeBytes);
  19. app.addColumn(cf, MUTATE_INFO, Bytes.toBytes(buf.toString()));
  20. if (!isBatchUpdate) {
  21. mutate(table, app, rowKeyBase);
  22. numCols.addAndGet(1);
  23. app = new Append(rowKey);

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

  1. @Test
  2. public void testMutation() throws Exception {
  3. table.increment(inc);
  4. value = Bytes.toBytes(20L);
  5. assertResult(table.get(new Get(ROW)), value, value);
  6. assertObserverHasExecuted();
  7. Append append = new Append(ROW);
  8. append.addColumn(FAMILY, QUALIFIER, APPEND_VALUE);
  9. table.append(append);
  10. .put(APPEND_VALUE)
  11. .array();
  12. assertResult(table.get(new Get(ROW)), value, value);
  13. assertObserverHasExecuted();
  14. delete.addColumns(FAMILY, QUALIFIER);
  15. table.delete(delete);
  16. assertTrue(Arrays.asList(table.get(new Get(ROW)).rawCells()).toString(),
  17. table.get(new Get(ROW)).isEmpty());
  18. assertObserverHasExecuted();
  19. assertObserverHasExecuted();
  20. assertTrue(table.get(new Get(ROW)).isEmpty());

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

  1. @Test
  2. public void testLabelsWithAppend() throws Throwable {
  3. TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  4. try (Table table = TEST_UTIL.createTable(tableName, fam)) {
  5. byte[] row1 = Bytes.toBytes("row1");
  6. byte[] val = Bytes.toBytes("a");
  7. Put put = new Put(row1);
  8. put.addColumn(fam, qual, HConstants.LATEST_TIMESTAMP, val);
  9. put.setCellVisibility(new CellVisibility(SECRET + " & " + CONFIDENTIAL));
  10. table.put(put);
  11. Get get = new Get(row1);
  12. get.setAuthorizations(new Authorizations(SECRET));
  13. Result result = table.get(get);
  14. assertTrue(result.isEmpty());
  15. Append append = new Append(row1);
  16. append.addColumn(fam, qual, Bytes.toBytes("b"));
  17. table.append(append);
  18. result = table.get(get);
  19. assertTrue(result.isEmpty());
  20. append = new Append(row1);
  21. append.addColumn(fam, qual, Bytes.toBytes("c"));
  22. append.setCellVisibility(new CellVisibility(SECRET));
  23. table.append(append);
  24. result = table.get(get);
  25. assertTrue(!result.isEmpty());
  26. }
  27. }

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

  1. @Test
  2. public void testStandby() throws Exception {
  3. MasterFileSystem mfs = UTIL2.getHBaseCluster().getMaster().getMasterFileSystem();
  4. Path remoteWALDir = getRemoteWALDir(mfs, PEER_ID);
  5. assertFalse(mfs.getWALFileSystem().exists(remoteWALDir));
  6. UTIL2.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
  7. SyncReplicationState.STANDBY);
  8. assertTrue(mfs.getWALFileSystem().exists(remoteWALDir));
  9. try (Table table = UTIL2.getConnection().getTable(TABLE_NAME)) {
  10. assertDisallow(table, t -> t.get(new Get(Bytes.toBytes("row"))));
  11. assertDisallow(table,
  12. t -> t.put(new Put(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("row"))));
  13. assertDisallow(table, t -> t.delete(new Delete(Bytes.toBytes("row"))));
  14. assertDisallow(table, t -> t.incrementColumnValue(Bytes.toBytes("row"), CF, CQ, 1));
  15. assertDisallow(table,
  16. t -> t.append(new Append(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("row"))));
  17. assertDisallow(table,
  18. t -> t.get(Arrays.asList(new Get(Bytes.toBytes("row")), new Get(Bytes.toBytes("row1")))));
  19. assertDisallow(table,
  20. t -> t.put(
  21. Arrays.asList(new Put(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("row")),
  22. new Put(Bytes.toBytes("row1")).addColumn(CF, CQ, Bytes.toBytes("row1")))));
  23. assertDisallow(table, t -> t.mutateRow(new RowMutations(Bytes.toBytes("row"))

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

  1. public Result preAppend(final ObserverContext<RegionCoprocessorEnvironment> e,
  2. final Append append) throws IOException {
  3. byte[] opBuf = append.getAttribute(OPERATION_ATTRIB);
  4. if (opBuf == null) {
  5. return null;
  6. KeyValue keyValue = append.getFamilyMap().values().iterator().next().iterator().next();
  7. maxGetTimestamp = minGetTimestamp + 1;
  8. } else {
  9. clientTimestampBuf = append.getAttribute(MAX_TIMERANGE_ATTRIB);
  10. if (clientTimestampBuf != null) {
  11. clientTimestamp = maxGetTimestamp = Bytes.toLong(clientTimestampBuf);
  12. e.complete();
  13. HRegion region = env.getRegion();
  14. byte[] row = append.getRow();
  15. region.startRegionOperation();
  16. try {
  17. switch (op) {
  18. case RETURN_SEQUENCE:
  19. KeyValue currentValueKV = result.raw()[0];
  20. long expectedValue = PDataType.LONG.getCodec().decodeLong(append.getAttribute(CURRENT_VALUE_ATTRIB), 0, null);
  21. long value = PDataType.LONG.getCodec().decodeLong(currentValueKV.getBuffer(), currentValueKV.getValueOffset(), null);
  22. m = new Put(row, currentValueKV.getTimestamp());
  23. m.getFamilyMap().putAll(append.getFamilyMap());
  24. break;

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

  1. @Test
  2. public void testNullQualifier() throws Exception {
  3. final TableName tableName = TableName.valueOf(name.getMethodName());
  4. Table table = TEST_UTIL.createTable(tableName, FAMILY);
  5. Put put = new Put(ROW);
  6. put.addColumn(FAMILY, null, VALUE);
  7. table.put(put);
  8. table.delete(delete);
  9. Get get = new Get(ROW);
  10. Result result = table.get(get);
  11. assertEmptyResult(result);
  12. table.delete(delete);
  13. Append append = new Append(ROW);
  14. append.addColumn(FAMILY, null, VALUE);
  15. table.append(append);
  16. getTestNull(table, ROW, FAMILY, VALUE);
  17. put = new Put(ROW);
  18. put.addColumn(FAMILY, null, Bytes.toBytes("checkAndPut"));
  19. table.put(put);
  20. table.checkAndMutate(ROW, FAMILY).ifEquals(VALUE).thenPut(put);
  21. mutate.add(new Put(ROW).addColumn(FAMILY, null, Bytes.toBytes("checkAndMutate")));
  22. table.checkAndMutate(ROW, FAMILY).ifEquals(Bytes.toBytes("checkAndPut")).thenMutate(mutate);

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

  1. ((FakeRSRpcServices)badRS.getRSRpcServices()).setExceptionInjector(
  2. new RoundRobinExceptionInjector());
  3. Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
  4. conf.set("hbase.client.retries.number", "1");
  5. ConnectionImplementation conn =
  6. try {
  7. Table table = conn.getTable(TABLE_NAME);
  8. byte[] row = Bytes.toBytes("row1");
  9. Put put = new Put(row);
  10. put.addColumn(FAMILY, QUALIFIER, Bytes.toBytes(10));
  11. Get get = new Get(row);
  12. Append append = new Append(row);
  13. append.addColumn(FAMILY, QUALIFIER, Bytes.toBytes(11));
  14. Increment increment = new Increment(row);
  15. increment.addColumn(FAMILY, QUALIFIER, 10);
  16. success = false;
  17. try {
  18. table.put(put);
  19. table.get(get);
  20. table.append(append);
  21. table.increment(increment);
  22. table.delete(delete);

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

  1. desc.setConfiguration("hbase.rowlock.wait.duration", String.valueOf(5000));
  2. desc.addFamily(new HColumnDescriptor(FAMILY));
  3. TEST_UTIL.getAdmin().createTable(desc);
  4. Configuration copy = new Configuration(TEST_UTIL.getConfiguration());
  5. copy.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
  6. try (Connection con = ConnectionFactory.createConnection(copy)) {
  7. putService.execute(() -> {
  8. try (Table table = con.getTable(tableName)) {
  9. Put put = new Put(ROW);
  10. put.addColumn(FAMILY, QUALIFIER, VALUE);
  11. table.put(put);
  12. } catch (IOException ex) {
  13. throw new RuntimeException(ex);
  14. ExecutorService appendService = Executors.newSingleThreadExecutor();
  15. appendService.execute(() -> {
  16. Append append = new Append(ROW);
  17. append.addColumn(FAMILY, QUALIFIER, VALUE);
  18. try (Table table = con.getTable(tableName)) {
  19. table.append(append);
  20. fail("The APPEND should fail because the target lock is blocked by previous put");
  21. } catch (Exception ex) {

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

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

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

  1. private List<Result> doAppend(final boolean walUsed) throws IOException {
  2. LOG.info("Starting testAppend, walUsed is " + walUsed);
  3. final TableName TABLENAME = TableName.valueOf(walUsed ? "testAppendWithWAL" : "testAppendWithoutWAL");
  4. Table t = TEST_UTIL.createTable(TABLENAME, FAMILY);
  5. final byte[] row1 = Bytes.toBytes("c");
  6. final byte[] row2 = Bytes.toBytes("b");
  7. final byte[] row3 = Bytes.toBytes("a");
  8. final byte[] qual = Bytes.toBytes("qual");
  9. Put put_0 = new Put(row2);
  10. put_0.addColumn(FAMILY, qual, Bytes.toBytes("put"));
  11. Put put_1 = new Put(row3);
  12. put_1.addColumn(FAMILY, qual, Bytes.toBytes("put"));
  13. Append append_0 = new Append(row1);
  14. append_0.addColumn(FAMILY, qual, Bytes.toBytes("i"));
  15. Append append_1 = new Append(row1);
  16. append_1.addColumn(FAMILY, qual, Bytes.toBytes("k"));
  17. Append append_2 = new Append(row1);
  18. append_2.addColumn(FAMILY, qual, Bytes.toBytes("e"));
  19. if (!walUsed) {
  20. append_2.setDurability(Durability.SKIP_WAL);
  21. Append append_3 = new Append(row1);
  22. append_3.addColumn(FAMILY, qual, Bytes.toBytes("a"));
  23. Scan s = new Scan();
  24. s.setCaching(1);
  25. t.append(append_0);
  26. t.put(put_0);
  27. t.put(put_1);

相关文章