@Test public void testIncrementOnSameColumn() throws Exception { LOG.info("Starting " + this.name.getMethodName()); final byte[] TABLENAME = Bytes.toBytes(filterStringSoTableNameSafe(this.name.getMethodName())); Table ht = TEST_UTIL.createTable(TableName.valueOf(TABLENAME), FAMILY); new byte[][] { Bytes.toBytes("A"), Bytes.toBytes("B"), Bytes.toBytes("C") }; Increment inc = new Increment(ROW); for (int i = 0; i < QUALIFIERS.length; i++) { inc.addColumn(FAMILY, QUALIFIERS[i], 1); inc.addColumn(FAMILY, QUALIFIERS[i], 1); ht.increment(inc); inc = new Increment(ROW); for (int i = 0; i < QUALIFIERS.length; i++) { inc.addColumn(FAMILY, QUALIFIERS[i], 1); inc.addColumn(FAMILY, QUALIFIERS[i], 1); ht.increment(inc); r = ht.get(get); kvs = r.rawCells(); assertEquals(3, kvs.length); assertIncrementKey(kvs[0], ROW, FAMILY, QUALIFIERS[0], 2);
public static TIncrement incrementFromHBase(Increment in) throws IOException { TIncrement out = new TIncrement(); 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()) { columnValue.setFamily(family).setQualifier(CellUtil.cloneQualifier(cell)); columnValue.setAmount( Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())); out.addToColumns(columnValue); 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;
@Test public void testIncrementInstance() { final long expected = 13; Increment inc = new Increment(new byte [] {'r'}); int total = 0; for (int i = 0; i < 2; i++) { byte [] bytes = Bytes.toBytes(i); inc.addColumn(bytes, bytes, expected); total++; } Map<byte[], NavigableMap<byte [], Long>> familyMapOfLongs = inc.getFamilyMapOfLongs(); int found = 0; for (Map.Entry<byte [], NavigableMap<byte [], Long>> entry: familyMapOfLongs.entrySet()) { for (Map.Entry<byte [], Long> e: entry.getValue().entrySet()) { assertEquals(expected, e.getValue().longValue()); found++; } } assertEquals(total, found); } }
private Increment newIncrementWithSkipWAL() { Increment increment = new Increment(Bytes.toBytes("row")); increment.addColumn(CF, CQ, 1); increment.setDurability(Durability.SKIP_WAL); return increment; }
/** * Atomically increments a column value. If the column value already exists * and is not a big-endian long, this could throw an exception. If the column * value does not yet exist it is initialized to <code>amount</code> and * written to the specified column. * * <p>Setting durability to {@link Durability#SKIP_WAL} means that in a fail * scenario you will lose any increments that have not been flushed. * @param row The row that contains the cell to increment. * @param family The column family of the cell to increment. * @param qualifier The column qualifier of the cell to increment. * @param amount The amount to increment the cell with (or decrement, if the * amount is negative). * @param durability The persistence guarantee for this increment. * @return The new value, post increment. * @throws IOException if a remote or network exception occurs. */ default long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount, Durability durability) throws IOException { Increment increment = new Increment(row) .addColumn(family, qualifier, amount) .setDurability(durability); Cell cell = increment(increment).getColumnLatestCell(family, qualifier); return Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); }
boolean isIncrement = increment.getAttribute(HBaseTable.DELTA_WRITE) != null; boolean transactional = state.containsTransactionalFamily(increment.getFamilyCellMap().keySet()); if (!isIncrement || !transactional) { return null; byte[] txBytes = increment.getAttribute(TxConstants.TX_OPERATION_ATTRIBUTE_KEY); if (txBytes == null) { throw new IllegalArgumentException("Attribute " + TxConstants.TX_OPERATION_ATTRIBUTE_KEY + " must be set for transactional readless increments"); byte[] wpBytes = increment.getAttribute(HBaseTable.WRITE_POINTER); if (wpBytes == null) { throw new IllegalArgumentException("Attribute " + HBaseTable.WRITE_POINTER + " must be set for transactional readless increments"); long writeVersion = Bytes.toLong(wpBytes); Get get = new Get(increment.getRow()); get.setAttribute(TxConstants.TX_OPERATION_ATTRIBUTE_KEY, txBytes); for (Map.Entry<byte[], NavigableMap<byte[], Long>> entry : increment.getFamilyMapOfLongs().entrySet()) { byte[] family = entry.getKey(); for (byte[] column : entry.getValue().keySet()) { get.addColumn(family, column); Put put = new Put(increment.getRow()); put.setAttribute(HBaseTable.TX_MAX_LIFETIME_MILLIS_KEY, increment.getAttribute(HBaseTable.TX_MAX_LIFETIME_MILLIS_KEY)); put.setAttribute(HBaseTable.TX_ID, increment.getAttribute(HBaseTable.TX_ID)); for (Map.Entry<byte[], NavigableMap<byte[], Long>> entry : increment.getFamilyMapOfLongs().entrySet()) { byte[] family = entry.getKey();
@Test public void testLabelsWithIncrement() 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(1L); 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()); table.incrementColumnValue(row1, fam, qual, 2L); result = table.get(get); assertTrue(result.isEmpty()); Increment increment = new Increment(row1); increment.addColumn(fam, qual, 2L); increment.setCellVisibility(new CellVisibility(SECRET)); table.increment(increment); result = table.get(get); assertTrue(!result.isEmpty()); } }
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, 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)));
@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); Increment increment = new Increment(ROW); increment.addColumn(FAMILY, null, 1L); table.increment(increment); getTestNull(table, ROW, FAMILY, 1L); 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);
@Test public void testMutation() throws Exception { Increment inc = new Increment(ROW); inc.addColumn(FAMILY, QUALIFIER, 10L); table.increment(inc); value = Bytes.toBytes(20L); assertResult(table.get(new Get(ROW)), value, value); assertObserverHasExecuted(); .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());
@Test public void testNonceCollision() throws Exception { LOG.info("test=testNonceCollision"); final Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration()); Table table = connection.getTable(TEST_TABLE); Put put = new Put(ONE_ROW); put.addColumn(BYTES_FAMILY, QUALIFIER, Bytes.toBytes(0L)); Increment inc = new Increment(ONE_ROW); inc.addColumn(BYTES_FAMILY, QUALIFIER, 1L); table.increment(inc); inc = new Increment(ONE_ROW); inc.addColumn(BYTES_FAMILY, QUALIFIER, 1L); Result result = table.increment(inc); validateResult(result, QUALIFIER, Bytes.toBytes(1L)); Get get = new Get(ONE_ROW); get.addColumn(BYTES_FAMILY, QUALIFIER); result = table.get(get); validateResult(result, QUALIFIER, Bytes.toBytes(1L));
@Test public void testIncrement() throws IOException { try (Table t = TEST_UTIL.getConnection().getTable(TABLE_NAME)) { Put put = new Put(ROW); put.addColumn(FAMILY, QUAL, VALUE); t.put(put); assertRowAndValue(t.get(new Get(ROW)), ROW, VALUE); Increment inc = new Increment(ROW); inc.addColumn(FAMILY, QUAL, 99); assertRowAndValue(t.increment(inc), ROW, FIXED_VALUE); assertRowAndValue(t.get(new Get(ROW)), ROW, Bytes.toBytes(199L)); } }
((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); Delete delete = new Delete(row); delete.addColumn(FAMILY, QUALIFIER); success = false; try { table.put(put); table.get(get); table.append(append); table.increment(increment); table.delete(delete);
@Test public void testChangeCellWithDifferntColumnFamily() throws Exception { TableName tableName = TableName.valueOf(name.getMethodName()); createTableWithCoprocessor(tableName, ChangeCellWithDifferntColumnFamilyObserver.class.getName()); try (Table table = connection.getTable(tableName)) { Increment increment = new Increment(ROW).addColumn(CF1_BYTES, CQ1, 1); table.increment(increment); Get get = new Get(ROW).addColumn(CF2_BYTES, CQ1); Result result = table.get(get); assertEquals(1, result.size()); assertEquals(1, Bytes.toLong(result.getValue(CF2_BYTES, CQ1))); Append append = new Append(ROW).addColumn(CF1_BYTES, CQ2, VALUE); table.append(append); get = new Get(ROW).addColumn(CF2_BYTES, CQ2); result = table.get(get); assertEquals(1, result.size()); assertTrue(Bytes.equals(VALUE, result.getValue(CF2_BYTES, CQ2))); } }
@Test public void testIncrementingInvalidValue() throws Exception { LOG.info("Starting " + this.name.getMethodName()); final TableName TABLENAME = TableName.valueOf(filterStringSoTableNameSafe(this.name.getMethodName())); Table ht = TEST_UTIL.createTable(TABLENAME, FAMILY); final byte[] COLUMN = Bytes.toBytes("column"); Put p = new Put(ROW); // write an integer here (not a Long) p.addColumn(FAMILY, COLUMN, Bytes.toBytes(5)); ht.put(p); try { ht.incrementColumnValue(ROW, FAMILY, COLUMN, 5); fail("Should have thrown DoNotRetryIOException"); } catch (DoNotRetryIOException iox) { // success } Increment inc = new Increment(ROW); inc.addColumn(FAMILY, COLUMN, 5); try { ht.increment(inc); fail("Should have thrown DoNotRetryIOException"); } catch (DoNotRetryIOException iox) { // success } }
@Test public void testCellSizeLimit() throws IOException { final TableName tableName = TableName.valueOf("testCellSizeLimit"); HTableDescriptor htd = new HTableDescriptor(tableName); htd.setConfiguration(HRegion.HBASE_MAX_CELL_SIZE_KEY, Integer.toString(10 * 1024)); // 10K HColumnDescriptor fam = new HColumnDescriptor(FAMILY); htd.addFamily(fam); Admin admin = TEST_UTIL.getAdmin(); admin.createTable(htd); try (Table t = TEST_UTIL.getConnection().getTable(tableName)) { t.put(new Put(ROW).addColumn(FAMILY, QUALIFIER, Bytes.toBytes(0L))); t.increment(new Increment(ROW).addColumn(FAMILY, QUALIFIER, 1L)); try (Table t = TEST_UTIL.getConnection().getTable(tableName)) { t.put(new Put(ROW).addColumn(FAMILY, QUALIFIER, new byte[9*1024]));
@Test public void testBatchWithIncrementAndAppend() throws Exception { LOG.info("test=testBatchWithIncrementAndAppend"); final byte[] QUAL1 = Bytes.toBytes("qual1"); final byte[] QUAL2 = Bytes.toBytes("qual2"); final byte[] QUAL3 = Bytes.toBytes("qual3"); final byte[] QUAL4 = Bytes.toBytes("qual4"); Table table = UTIL.getConnection().getTable(TEST_TABLE); Delete d = new Delete(ONE_ROW); table.delete(d); Put put = new Put(ONE_ROW); put.addColumn(BYTES_FAMILY, QUAL1, Bytes.toBytes("abc")); put.addColumn(BYTES_FAMILY, QUAL2, Bytes.toBytes(1L)); table.put(put); Increment inc = new Increment(ONE_ROW); inc.addColumn(BYTES_FAMILY, QUAL2, 1); inc.addColumn(BYTES_FAMILY, QUAL3, 1); table.batch(actions, multiRes); validateResult(multiRes[1], QUAL1, Bytes.toBytes("abcdef")); validateResult(multiRes[1], QUAL4, Bytes.toBytes("xyz"));
byte[] row = increment.getRow(); List<RowLock> locks = Lists.newArrayList(); TimeRange tr = increment.getTimeRange(); region.startRegionOperation(); try { long maxTimestamp = tr.getMax(); boolean validateOnly = true; Get get = new Get(row); get.setTimeRange(tr.getMin(), tr.getMax()); for (Map.Entry<byte[], List<Cell>> entry : increment.getFamilyCellMap().entrySet()) { byte[] cf = entry.getKey(); for (Cell cq : entry.getValue()) { long value = Bytes.toLong(cq.getValueArray(), cq.getValueOffset()); get.addColumn(cf, CellUtil.cloneQualifier(cq)); long cellTimestamp = cq.getTimestamp(); if (result.isEmpty()) { return getErrorResult(row, maxTimestamp, SQLExceptionCode.SEQUENCE_UNDEFINED.getErrorCode()); Put put = new Put(row, timestamp); int numIncrementKVs = increment.getFamilyCellMap().get(PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES).size(); put.add(newCurrentValueKV); Sequence.replaceCurrentValueKV(cells, newCurrentValueKV); region.batchMutate(mutations);
Increment inc = new Increment(Bytes.toBytes("good")).addColumn(FAMILY, null, 100); List<Row> batches = new ArrayList<>(); batches.add(new Put(Bytes.toBytes("fail")).addColumn(FAMILY, null, new byte[CELL_SIZE])); batches.add(new Put(Bytes.toBytes("fail")).addColumn(FAMILY, null, new byte[CELL_SIZE])); batches.add(new Put(Bytes.toBytes("good")).addColumn(FAMILY, null, new byte[1])); 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) {