protected long atomicIncrement(ByteBuffer tableName, ByteBuffer row, byte [] family, byte [] qualifier, long amount) throws IOError, IllegalArgument, TException { Table table = null; try { table = getTable(tableName); return table.incrementColumnValue( getBytes(row), family, qualifier, amount); } catch (IOException e) { LOG.warn(e.getMessage(), e); throw getIOError(e); } finally { closeTable(table); } }
throw new IOException("Auto-Fail rest of ICVs"); table.incrementColumnValue(row.getRowKey(), row.getFamily(), row.getQualifier(), counter); } catch (IOException e) {
@Test public void testIncrementWithDeletes() 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"); ht.incrementColumnValue(ROW, FAMILY, COLUMN, 5); TEST_UTIL.flush(TABLENAME); Delete del = new Delete(ROW); ht.delete(del); ht.incrementColumnValue(ROW, FAMILY, COLUMN, 5); Get get = new Get(ROW); Result r = ht.get(get); assertEquals(1, r.size()); assertEquals(5, Bytes.toLong(r.getValue(FAMILY, COLUMN))); }
@Override public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount, Durability durability) throws IOException { return delegate.incrementColumnValue(row, family, qualifier, amount, durability); }
@Override public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount) throws IOException { return delegate.incrementColumnValue(row, family, qualifier, amount); }
@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 } }
try { try( Table table = TEST_UTIL.getConnection().getTable(tableName)) { table.incrementColumnValue(ROW1, FAM, FAM, 1);
ht.incrementColumnValue(ROW, FAMILY, QUALIFIERS[0], 1); ht.incrementColumnValue(ROW, FAMILY, QUALIFIERS[1], 2); ht.incrementColumnValue(ROW, FAMILY, QUALIFIERS[2], 3); ht.incrementColumnValue(ROW, FAMILY, QUALIFIERS[3], 4);
try { ht.incrementColumnValue(null, FAMILY, COLUMN, 5); fail("Should have thrown IOException"); } catch (IOException iox) { ht.incrementColumnValue(ROW, null, COLUMN, 5); fail("Should have thrown IOException"); } catch (IOException iox) {
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"))));
@Override public Void run() throws Exception { try (Connection connection = ConnectionFactory.createConnection(conf)) { try (Table t = connection.getTable(TEST_TABLE.getTableName())) { Increment inc = new Increment(TEST_ROW1); inc.setTimeRange(0, 123); inc.addColumn(TEST_FAMILY1, TEST_Q1, 2L); t.increment(inc); t.incrementColumnValue(TEST_ROW1, TEST_FAMILY1, TEST_Q2, 1L); } } return null; } });
@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()); } }
getTestNull(table, ROW, FAMILY, 1L); table.incrementColumnValue(ROW, FAMILY, null, 1L); getTestNull(table, ROW, FAMILY, 2L);
/** * Increase a counter by rowkey and qualifier to a table */ public long increase(String table, byte[] family, byte[] rowkey, byte[] qualifier, long value) { try (Table htable = table(table)) { return htable.incrementColumnValue(rowkey, family, qualifier, value); } catch (IOException e) { throw new BackendException(e); } }
public static void main(String[] args) throws IOException { Configuration conf = HBaseConfiguration.create(); HBaseHelper helper = HBaseHelper.getHelper(conf); helper.dropTable("testtable"); helper.createTable("testtable", "daily"); Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(TableName.valueOf("testtable")); // vv IncrementSingleExample long cnt1 = table.incrementColumnValue(Bytes.toBytes("20110101"), // co IncrementSingleExample-1-Incr1 Increase counter by one. Bytes.toBytes("daily"), Bytes.toBytes("hits"), 1); long cnt2 = table.incrementColumnValue(Bytes.toBytes("20110101"), // co IncrementSingleExample-2-Incr2 Increase counter by one a second time. Bytes.toBytes("daily"), Bytes.toBytes("hits"), 1); long current = table.incrementColumnValue(Bytes.toBytes("20110101"), // co IncrementSingleExample-3-GetCurrent Get current value of the counter without increasing it. Bytes.toBytes("daily"), Bytes.toBytes("hits"), 0); long cnt3 = table.incrementColumnValue(Bytes.toBytes("20110101"), // co IncrementSingleExample-4-Decr1 Decrease counter by one. Bytes.toBytes("daily"), Bytes.toBytes("hits"), -1); // ^^ IncrementSingleExample System.out.println("cnt1: " + cnt1 + ", cnt2: " + cnt2 + ", current: " + current + ", cnt3: " + cnt3); } }
table.incrementColumnValue(Bytes.toBytes("row10"), Bytes.toBytes("colfam1"), Bytes.toBytes("qual12"), 1); printStatistics(true, true);
@Override public Object generate(TableGeneratorDiscriptor discriptor, ClientBase client, String dataType) { String tableName = HBaseUtils.getHTableName(discriptor.getSchema(), discriptor.getPkColumnValue()); try { Table hTable = ((HBaseDataHandler) ((HBaseClient) client).handler).gethTable(tableName); Long latestCount = hTable.incrementColumnValue(HBaseUtils.AUTO_ID_ROW.getBytes(), discriptor .getPkColumnValue().getBytes(), discriptor.getValueColumnName().getBytes(), 1); if (latestCount == 1) { return (long) discriptor.getInitialValue(); } else if (discriptor.getAllocationSize() == 1) { return latestCount + discriptor.getInitialValue(); } else { return (latestCount - 1) * discriptor.getAllocationSize() + discriptor.getInitialValue(); } } catch (IOException ioex) { log.error("Error while generating id for entity, Caused by: .", ioex); throw new KunderaException(ioex); } } }
@Override public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount) throws IOException { return delegate.incrementColumnValue(row, family, qualifier, amount); }
@Override public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount, Durability durability) throws IOException { return delegate.incrementColumnValue(row, family, qualifier, amount, durability); }
@Override public long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier, long amount, Durability durability) throws IOException { if (allowNonTransactional) { return hTable.incrementColumnValue(row, family, qualifier, amount, durability); } else { throw new UnsupportedOperationException("Operation is not supported transactionally"); } }