Java Code Examples for org.apache.hadoop.hbase.client.Table#increment()

The following examples show how to use org.apache.hadoop.hbase.client.Table#increment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HBaseStorage.java    From cantor with Apache License 2.0 6 votes vote down vote up
/**
 * @return the value before increment
 */
@Override
public Optional<Long> incrementAndGet(long category, long ts, long range) {
    String tbl = String.format(TABLE_FMT, category % TABLE_COUNT);
    Table table = tableConnections.get(tbl);

    try {
        Increment increment = new Increment(Bytes.toBytes(String.format(ROW_KEY_FMT, ts)));
        increment.setTTL(ttl);
        byte[] col = Bytes.toBytes(String.valueOf(category));
        increment.addColumn(SERVICE_FAMILY, col, range);
        Result result = table.increment(increment);
        Long afterInc = Bytes.toLong(result.getValue(SERVICE_FAMILY, col));

        return Optional.of(afterInc);
    } catch (Exception e) {
        if (log.isErrorEnabled())
            log.error(
                    "increment range value failed for [ category: {} ] [ timestamp {} ] [ range {} ]",
                    category, ts, range, e);
        return Optional.empty();
    }
}
 
Example 2
Source File: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Override
public void increment(TIncrement tincrement) throws IOError, TException {

  if (tincrement.getRow().length == 0 || tincrement.getTable().length == 0) {
    throw new TException("Must supply a table and a row key; can't increment");
  }

  if (conf.getBoolean(COALESCE_INC_KEY, false)) {
    this.coalescer.queueIncrement(tincrement);
    return;
  }

  Table table = null;
  try {
    table = getTable(tincrement.getTable());
    Increment inc = ThriftUtilities.incrementFromThrift(tincrement);
    table.increment(inc);
  } catch (IOException e) {
    LOG.warn(e.getMessage(), e);
    throw getIOError(e);
  } finally{
    closeTable(table);
  }
}
 
Example 3
Source File: TestRegionObserverInterface.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testIncrementHook() throws IOException {
  final TableName tableName = TableName.valueOf(TEST_TABLE.getNameAsString() + "." + name.getMethodName());
  Table table = util.createTable(tableName, new byte[][] { A, B, C });
  try {
    Increment inc = new Increment(Bytes.toBytes(0));
    inc.addColumn(A, A, 1);

    verifyMethodResult(SimpleRegionObserver.class,
      new String[] { "hadPreIncrement", "hadPostIncrement", "hadPreIncrementAfterRowLock" },
      tableName, new Boolean[] { false, false, false });

    table.increment(inc);

    verifyMethodResult(SimpleRegionObserver.class,
      new String[] { "hadPreIncrement", "hadPostIncrement", "hadPreIncrementAfterRowLock" },
      tableName, new Boolean[] { true, true, true });
  } finally {
    util.deleteTable(tableName);
    table.close();
  }
}
 
Example 4
Source File: TestRegionObserverInterface.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void testPreWALAppendHook(Table table, TableName tableName) throws IOException {
  int expectedCalls = 0;
  String [] methodArray = new String[1];
  methodArray[0] = "getCtPreWALAppend";
  Object[] resultArray = new Object[1];

  Put p = new Put(ROW);
  p.addColumn(A, A, A);
  table.put(p);
  resultArray[0] = ++expectedCalls;
  verifyMethodResult(SimpleRegionObserver.class, methodArray, tableName, resultArray);

  Append a = new Append(ROW);
  a.addColumn(B, B, B);
  table.append(a);
  resultArray[0] = ++expectedCalls;
  verifyMethodResult(SimpleRegionObserver.class, methodArray, tableName, resultArray);

  Increment i = new Increment(ROW);
  i.addColumn(C, C, 1);
  table.increment(i);
  resultArray[0] = ++expectedCalls;
  verifyMethodResult(SimpleRegionObserver.class, methodArray, tableName, resultArray);

  Delete d = new Delete(ROW);
  table.delete(d);
  resultArray[0] = ++expectedCalls;
  verifyMethodResult(SimpleRegionObserver.class, methodArray, tableName, resultArray);
}
 
Example 5
Source File: MultiThreadedUpdater.java    From hbase with Apache License 2.0 5 votes vote down vote up
public void mutate(Table table, Mutation m,
    long keyBase, byte[] row, byte[] cf, byte[] q, byte[] v) {
  long start = System.currentTimeMillis();
  try {
    m = dataGenerator.beforeMutate(keyBase, m);
    if (m instanceof Increment) {
      table.increment((Increment)m);
    } else if (m instanceof Append) {
      table.append((Append)m);
    } else if (m instanceof Put) {
      table.checkAndMutate(row, cf).qualifier(q).ifEquals(v).thenPut((Put)m);
    } else if (m instanceof Delete) {
      table.checkAndMutate(row, cf).qualifier(q).ifEquals(v).thenDelete((Delete)m);
    } else {
      throw new IllegalArgumentException(
        "unsupported mutation " + m.getClass().getSimpleName());
    }
    totalOpTimeMs.addAndGet(System.currentTimeMillis() - start);
  } catch (IOException e) {
    if (ignoreNonceConflicts) {
      LOG.info("Detected nonce conflict, ignoring: " + e.getMessage());
      totalOpTimeMs.addAndGet(System.currentTimeMillis() - start);
      return;
    }
    failedKeySet.add(keyBase);
    String exceptionInfo;
    if (e instanceof RetriesExhaustedWithDetailsException) {
      RetriesExhaustedWithDetailsException aggEx = (RetriesExhaustedWithDetailsException) e;
      exceptionInfo = aggEx.getExhaustiveDescription();
    } else {
      exceptionInfo = StringUtils.stringifyException(e);
    }
    LOG.error("Failed to mutate: " + keyBase + " after " +
        (System.currentTimeMillis() - start) +
      "ms; region information: " + getRegionDebugInfoSafe(table, m.getRow()) + "; errors: "
        + exceptionInfo);
  }
}
 
Example 6
Source File: MultiThreadedUpdater.java    From hbase with Apache License 2.0 5 votes vote down vote up
public void mutate(Table table, Mutation m,
    long keyBase, byte[] row, byte[] cf, byte[] q, byte[] v) {
  long start = System.currentTimeMillis();
  try {
    m = dataGenerator.beforeMutate(keyBase, m);
    if (m instanceof Increment) {
      table.increment((Increment)m);
    } else if (m instanceof Append) {
      table.append((Append)m);
    } else if (m instanceof Put) {
      table.checkAndMutate(row, cf).qualifier(q).ifEquals(v).thenPut((Put)m);
    } else if (m instanceof Delete) {
      table.checkAndMutate(row, cf).qualifier(q).ifEquals(v).thenDelete((Delete)m);
    } else {
      throw new IllegalArgumentException(
        "unsupported mutation " + m.getClass().getSimpleName());
    }
    totalOpTimeMs.addAndGet(System.currentTimeMillis() - start);
  } catch (IOException e) {
    failedKeySet.add(keyBase);
    String exceptionInfo;
    if (e instanceof RetriesExhaustedWithDetailsException) {
      RetriesExhaustedWithDetailsException aggEx = (RetriesExhaustedWithDetailsException)e;
      exceptionInfo = aggEx.getExhaustiveDescription();
    } else {
      StringWriter stackWriter = new StringWriter();
      PrintWriter pw = new PrintWriter(stackWriter);
      e.printStackTrace(pw);
      pw.flush();
      exceptionInfo = StringUtils.stringifyException(e);
    }
    LOG.error("Failed to mutate: " + keyBase + " after " + (System.currentTimeMillis() - start) +
      "ms; region information: " + getRegionDebugInfoSafe(table, m.getRow()) + "; errors: "
        + exceptionInfo);
  }
}