private RowMutations getBogusRowMutations() throws IOException { Put p = new Put(ROWKEY); byte[] value = new byte[0]; p.addColumn(new byte[]{'b', 'o', 'g', 'u', 's'}, new byte[]{'A'}, value); RowMutations rm = new RowMutations(ROWKEY); rm.add(p); return rm; }
@Override public void mutateRow(RowMutations rm) throws IOException { // Don't need nonces here - RowMutations only supports puts and deletes final List<Mutation> m = rm.getMutations(); batchMutate(m.toArray(new Mutation[m.size()]), true, HConstants.NO_NONCE, HConstants.NO_NONCE); }
@Override public void run(MultiResponse resp) { if (controller.failed()) { future.completeExceptionally(controller.getFailed()); } else { try { org.apache.hadoop.hbase.client.MultiResponse multiResp = ResponseConverter.getResults(req, resp, controller.cellScanner()); Throwable ex = multiResp.getException(regionName); if (ex != null) { future.completeExceptionally(ex instanceof IOException ? ex : new IOException( "Failed to mutate row: " + Bytes.toStringBinary(mutation.getRow()), ex)); } else { future.complete(respConverter .apply((Result) multiResp.getResults().get(regionName).result.get(0))); } } catch (IOException e) { future.completeExceptionally(e); } } } });
/** * Atomically checks if a row/family/qualifier value matches the expected * value. If it does, it adds the put. If the passed value is null, the check * is for the lack of column (ie: non-existence) * * The expected value argument of this call is on the left and the current * value of the cell is on the right side of the comparison operator. * * Ie. eg. GREATER operator means expected value > existing <=> add the put. * * @param row to check * @param family column family to check * @param qualifier column qualifier to check * @param op comparison operator to use * @param value the expected value * @param put data to put if check succeeds * @throws IOException e * @return true if the new put was executed, false otherwise * @deprecated Since 2.0.0. Will be removed in 3.0.0. Use {@link #checkAndMutate(byte[], byte[])} */ @Deprecated default boolean checkAndPut(byte[] row, byte[] family, byte[] qualifier, CompareOperator op, byte[] value, Put put) throws IOException { RowMutations mutations = new RowMutations(put.getRow(), 1); mutations.add(put); return checkAndMutate(row, family, qualifier, op, value, mutations); }
@Override public boolean thenPut(Put put) throws IOException { preCheck(); RowMutations rowMutations = new RowMutations(put.getRow()); rowMutations.add(put); return checkAndMutate(row, family, qualifier, op, value, rowMutations); }
/** * Create a {@link RowMutations} with the specified mutations. * @param mutations the mutations to send * @return RowMutations * @throws IOException if any row in mutations is different to another */ public static RowMutations of(List<? extends Mutation> mutations) throws IOException { if (CollectionUtils.isEmpty(mutations)) { throw new IllegalArgumentException("Cannot instantiate a RowMutations by empty list"); } return new RowMutations(mutations.get(0).getRow(), mutations.size()) .add(mutations); }
public void testRegionExceptionByAsync() throws Exception { List<Row> batches = new ArrayList<>(); batches.add(new Put(Bytes.toBytes("good")).addColumn(FAMILY, null, new byte[10])); RowMutations rm = new RowMutations(Bytes.toBytes("fail")); rm.add(new Put(rm.getRow()).addColumn(FAMILY, null, new byte[CELL_SIZE])); batches.add(rm); try (AsyncConnection asyncConnection = ConnectionFactory Result result = table.get(new Get(Bytes.toBytes("good"))).get(); assertEquals(1, result.size()); Cell cell = result.getColumnLatestCell(FAMILY, null);
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); put = new Put(ROW); put.addColumn(FAMILY, null, Bytes.toBytes("checkAndPut")); table.put(put); table.checkAndMutate(ROW, FAMILY).ifEquals(VALUE).thenPut(put); RowMutations mutate = new RowMutations(ROW); mutate.add(new Put(ROW).addColumn(FAMILY, null, Bytes.toBytes("checkAndMutate"))); table.checkAndMutate(ROW, FAMILY).ifEquals(Bytes.toBytes("checkAndPut")).thenMutate(mutate);
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")) .add((Mutation) new Put(Bytes.toBytes("row")).addColumn(CF, CQ, Bytes.toBytes("row")))));
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))); Result getResult = (Result) results.get(0); 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);
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")); mutations.add(put); table.mutateRow(mutations); printStatistics(true, true); mutations = new RowMutations(Bytes.toBytes("row10")); put = new Put(Bytes.toBytes("row10")); put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual20"), delete = new Delete(Bytes.toBytes("row10")); delete.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual17")); mutations.add(put); mutations.add(delete); cap = table.checkAndMutate(Bytes.toBytes("row10"), Bytes.toBytes("colfam1"), Bytes.toBytes("qual10"),
@Test public void testMutateRow() throws Exception { final byte[] qual2 = Bytes.toBytes("qual2"); TableName tableName = TableName.valueOf(TEST_NAME.getMethodName()); HTableDescriptor desc = new HTableDescriptor(tableName); HColumnDescriptor col = new HColumnDescriptor(fam); desc.addFamily(col); TEST_UTIL.getAdmin().createTable(desc); try (Table table = TEST_UTIL.getConnection().getTable(tableName)){ Put p1 = new Put(row1); p1.addColumn(fam, qual, value); p1.setCellVisibility(new CellVisibility(CONFIDENTIAL)); Put p2 = new Put(row1); p2.addColumn(fam, qual2, value); p2.setCellVisibility(new CellVisibility(SECRET)); RowMutations rm = new RowMutations(row1); rm.add(p1); rm.add(p2); table.mutateRow(rm); Get get = new Get(row1); get.setAuthorizations(new Authorizations(CONFIDENTIAL)); Result result = table.get(get); assertTrue(result.containsColumn(fam, qual)); assertFalse(result.containsColumn(fam, qual2)); get.setAuthorizations(new Authorizations(SECRET)); result = table.get(get); assertFalse(result.containsColumn(fam, qual)); assertTrue(result.containsColumn(fam, qual2)); } }
table.batch(puts, results); validateSizeAndEmpty(results, KEYS.length); byte[] qual2 = Bytes.toBytes("qual2"); byte[] val2 = Bytes.toBytes("putvalue2"); Get get = new Get(KEYS[10]); get.addColumn(BYTES_FAMILY, QUALIFIER); actions.add(get); get = new Get(KEYS[11]); put = new Put(KEYS[40]); RowMutations rm = new RowMutations(KEYS[50]); rm.add((Mutation) put); byte[] qual3 = Bytes.toBytes("qual3"); rm.add((Mutation) put); actions.add(rm); table.batch(actions, results); Result r = table.get(get); validateResult(r, qual2, val2);
@Test public void testRowMutations() throws InterruptedException, IOException { Put put = new Put(Bytes.toBytes("good")).addColumn(FAMILY, null, new byte[1]); List<Row> batches = new ArrayList<>(); RowMutations mutations = new RowMutations(Bytes.toBytes("fail")); mutations.add(new Put(Bytes.toBytes("fail")).addColumn(FAMILY, null, new byte[CELL_SIZE])); mutations.add(new Put(Bytes.toBytes("fail")).addColumn(FAMILY, null, new byte[CELL_SIZE])); batches.add(mutations); mutations = new RowMutations(Bytes.toBytes("good")); mutations.add(put); mutations.add(put); batches.add(mutations); Object[] objs = new Object[batches.size()]; try (Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME)) { table.batch(batches, objs); fail("Where is the exception? We put the malformed cells!!!"); } catch (RetriesExhaustedWithDetailsException e) {
Table t = TEST_UTIL.createTable(tableName, FAMILY); byte [][] QUALIFIERS = new byte [][] { Bytes.toBytes("a"), Bytes.toBytes("b") }; RowMutations arm = new RowMutations(ROW); Put p = new Put(ROW); p.addColumn(FAMILY, QUALIFIERS[0], VALUE); arm.add(p); t.mutateRow(arm); Get g = new Get(ROW); Result r = t.get(g); assertEquals(0, Bytes.compareTo(VALUE, r.getValue(FAMILY, QUALIFIERS[0]))); arm = new RowMutations(ROW); p = new Put(ROW); p.addColumn(FAMILY, QUALIFIERS[1], VALUE); arm.add(p); Delete d = new Delete(ROW); d.addColumns(FAMILY, QUALIFIERS[0]); arm.add(d); arm = new RowMutations(ROW); p = new Put(ROW); p.addColumn(new byte[]{'b', 'o', 'g', 'u', 's'}, QUALIFIERS[0], VALUE); arm.add(p); t.mutateRow(arm); fail("Expected NoSuchColumnFamilyException");
RowMutations rm = new RowMutations(Bytes.toBytes("fail")); rm.add(new Put(rm.getRow()).addColumn(FAMILY, null, new byte[CELL_SIZE])); rm.add(new Put(rm.getRow()).addColumn(FAMILY, null, new byte[10])); Put put = new Put(Bytes.toBytes("good")).addColumn(FAMILY, null, new byte[10]); for (Mutation m : rm.getMutations()) { cells.addAll(m.getCellList(FAMILY)); response.getRegionActionResultList().get(1).getResultOrExceptionList().get(0).hasResult()); try (Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME)) { Result result = table.get(new Get(Bytes.toBytes("good"))); assertEquals(1, result.size()); Cell cell = result.getColumnLatestCell(FAMILY, null);
@Test public void testCheckAndRowMutateTimestampsAreMonotonic() throws IOException { region = initHRegion(tableName, method, CONF, fam1); ManualEnvironmentEdge edge = new ManualEnvironmentEdge(); EnvironmentEdgeManager.injectEdge(edge); edge.setValue(10); Put p = new Put(row); p.setDurability(Durability.SKIP_WAL); p.addColumn(fam1, qual1, qual1); region.put(p); Result result = region.get(new Get(row)); Cell c = result.getColumnLatestCell(fam1, qual1); assertNotNull(c); assertEquals(10L, c.getTimestamp()); edge.setValue(1); // clock goes back p = new Put(row); p.setDurability(Durability.SKIP_WAL); p.addColumn(fam1, qual1, qual2); RowMutations rm = new RowMutations(row); rm.add(p); assertTrue(region.checkAndRowMutate(row, fam1, qual1, CompareOperator.EQUAL, new BinaryComparator(qual1), rm)); result = region.get(new Get(row)); c = result.getColumnLatestCell(fam1, qual1); assertEquals(10L, c.getTimestamp()); LOG.info("c value " + Bytes.toStringBinary(c.getValueArray(), c.getValueOffset(), c.getValueLength())); assertTrue(Bytes.equals(c.getValueArray(), c.getValueOffset(), c.getValueLength(), qual2, 0, qual2.length)); }
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); p2.addColumn(fam1, qual2, value2); p2.setDurability(Durability.SYNC_WAL); hTable.put(p2); 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) {
Put put = new Put(Bytes.toBytes("row1")); put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), 4, Bytes.toBytes("val99")); put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual4"), 4, Bytes.toBytes("val100")); delete.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2")); RowMutations mutations = new RowMutations(Bytes.toBytes("row1")); mutations.add(put); mutations.add(delete); boolean res1 = table.checkAndMutate(Bytes.toBytes("row1"), Bytes.toBytes("colfam2"), Bytes.toBytes("qual1"), CompareFilter.CompareOp.LESS, Bytes.toBytes("val1"), mutations); // co CheckAndMutateExample-1-Check1 Check if the column contains a value that is less than "val1". Here we receive "false" as the value is equal, but not lesser. put2.addColumn(Bytes.toBytes("colfam2"), Bytes.toBytes("qual1"), // co CheckAndMutateExample-2-Put1 Update the checked column to have a value greater than what we check for. 4, Bytes.toBytes("val2")); table.put(put2); boolean res2 = table.checkAndMutate(Bytes.toBytes("row1"), Bytes.toBytes("colfam2"), Bytes.toBytes("qual1"), CompareFilter.CompareOp.LESS, Bytes.toBytes("val1"), mutations); // co CheckAndMutateExample-3-Check2 Now "val1" is less than "val2" (binary comparison) and we expect "true" to be printed on the console.
try (ResultScanner scanner = table.getScanner(scan)) { resultCount = 0; for (Result ignore : scanner) { try (ResultScanner scanner = table.getScanner(scan)) { resultCount = 0; for (Result ignore : scanner) { get = new Get(ROW2); Result result = table.get(get); resultCount = result.isEmpty() ? 0 : 1; testReadRequests(resultCount, 1, 0); put = new Put(ROW1); put.addColumn(CF1, COL2, VAL2); boolean checkAndPut = table.checkAndMutate(ROW1, CF1).qualifier(COL2).ifEquals(VAL2).thenPut(put); put = new Put(ROW1); put.addColumn(CF1, COL1, VAL1); RowMutations rm = new RowMutations(ROW1); rm.add(put); boolean checkAndMutate = table.checkAndMutate(ROW1, CF1).qualifier(COL1).ifEquals(VAL1).thenMutate(rm);