org.apache.hadoop.hbase.client.Append Java Examples

The following examples show how to use org.apache.hadoop.hbase.client.Append. 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: TestRegionObserverInterface.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testAppendHook() throws IOException {
  final TableName tableName = TableName.valueOf(TEST_TABLE.getNameAsString() + "." + name.getMethodName());
  Table table = util.createTable(tableName, new byte[][] { A, B, C });
  try {
    Append app = new Append(Bytes.toBytes(0));
    app.addColumn(A, A, A);

    verifyMethodResult(SimpleRegionObserver.class,
      new String[] { "hadPreAppend", "hadPostAppend", "hadPreAppendAfterRowLock" }, tableName,
      new Boolean[] { false, false, false });

    table.append(app);

    verifyMethodResult(SimpleRegionObserver.class,
      new String[] { "hadPreAppend", "hadPostAppend", "hadPreAppendAfterRowLock" }, tableName,
      new Boolean[] { true, true, true });
  } finally {
    util.deleteTable(tableName);
    table.close();
  }
}
 
Example #2
Source File: MultiThreadedUpdaterWithACL.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Override
public Object run() throws Exception {
  try {
    if (table == null) {
      table = connection.getTable(tableName);
    }
    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) {
    recordFailure(m, keyBase, start, e);
  }
  return null;
}
 
Example #3
Source File: AccessController.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Override
public Result preAppendAfterRowLock(final ObserverContext<RegionCoprocessorEnvironment> c,
    final Append append) throws IOException {
  if (append.getAttribute(CHECK_COVERING_PERM) != null) {
    // We had failure with table, cf and q perm checks and now giving a chance for cell
    // perm check
    TableName table = c.getEnvironment().getRegion().getRegionInfo().getTable();
    AuthResult authResult = null;
    User user = getActiveUser(c);
    if (checkCoveringPermission(user, OpType.APPEND, c.getEnvironment(), append.getRow(),
        append.getFamilyCellMap(), append.getTimeRange().getMax(), Action.WRITE)) {
      authResult = AuthResult.allow(OpType.APPEND.toString(),
          "Covering cell set", user, Action.WRITE, table, append.getFamilyCellMap());
    } else {
      authResult = AuthResult.deny(OpType.APPEND.toString(),
          "Covering cell set", user, Action.WRITE, table, append.getFamilyCellMap());
    }
    AccessChecker.logResult(authResult);
    if (authorizationEnabled && !authResult.isAllowed()) {
      throw new AccessDeniedException("Insufficient permissions " +
        authResult.toContextString());
    }
  }
  return null;
}
 
Example #4
Source File: RSRpcServices.java    From hbase with Apache License 2.0 6 votes vote down vote up
private static Get toGet(final Mutation mutation) throws IOException {
  if(!(mutation instanceof Increment) && !(mutation instanceof Append)) {
    throw new AssertionError("mutation must be a instance of Increment or Append");
  }
  Get get = new Get(mutation.getRow());
  CellScanner cellScanner = mutation.cellScanner();
  while (!cellScanner.advance()) {
    Cell cell = cellScanner.current();
    get.addColumn(CellUtil.cloneFamily(cell), CellUtil.cloneQualifier(cell));
  }
  if (mutation instanceof Increment) {
    // Increment
    Increment increment = (Increment) mutation;
    get.setTimeRange(increment.getTimeRange().getMin(), increment.getTimeRange().getMax());
  } else {
    // Append
    Append append = (Append) mutation;
    get.setTimeRange(append.getTimeRange().getMin(), append.getTimeRange().getMax());
  }
  for (Entry<String, byte[]> entry : mutation.getAttributesMap().entrySet()) {
    get.setAttribute(entry.getKey(), entry.getValue());
  }
  return get;
}
 
Example #5
Source File: RegionCoprocessorHost.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Supports Coprocessor 'bypass'.
 * @param append append object
 * @return result to return to client if default operation should be bypassed, null otherwise
 * @throws IOException if an error occurred on the coprocessor
 */
public Result preAppend(final Append append) throws IOException {
  boolean bypassable = true;
  Result defaultResult = null;
  if (this.coprocEnvironments.isEmpty()) {
    return defaultResult;
  }
  return execOperationWithResult(
    new ObserverOperationWithResult<RegionObserver, Result>(regionObserverGetter, defaultResult,
          bypassable) {
        @Override
        public Result call(RegionObserver observer) throws IOException {
          return observer.preAppend(this, append);
        }
      });
}
 
Example #6
Source File: ThriftUtilities.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * From a {@link TAppend} create an {@link Append}.
 * @param tappend the Thrift version of an append.
 * @return an increment that the {@link TAppend} represented.
 */
public static Append appendFromThrift(TAppend tappend) {
  Append append = new Append(tappend.getRow());
  List<ByteBuffer> columns = tappend.getColumns();
  List<ByteBuffer> values = tappend.getValues();

  if (columns.size() != values.size()) {
    throw new IllegalArgumentException(
        "Sizes of columns and values in tappend object are not matching");
  }

  int length = columns.size();

  for (int i = 0; i < length; i++) {
    byte[][] famAndQf = CellUtil.parseColumn(getBytes(columns.get(i)));
    append.addColumn(famAndQf[0], famAndQf[1], getBytes(values.get(i)));
  }
  return append;
}
 
Example #7
Source File: RegionCoprocessorHost.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Supports Coprocessor 'bypass'.
 * @param append append object
 * @return result to return to client if default operation should be bypassed, null otherwise
 * @throws IOException if an error occurred on the coprocessor
 */
public Result preAppendAfterRowLock(final Append append) throws IOException {
  boolean bypassable = true;
  Result defaultResult = null;
  if (this.coprocEnvironments.isEmpty()) {
    return defaultResult;
  }
  return execOperationWithResult(
      new ObserverOperationWithResult<RegionObserver, Result>(regionObserverGetter,
          defaultResult, bypassable) {
        @Override
        public Result call(RegionObserver observer) throws IOException {
          return observer.preAppendAfterRowLock(this, append);
        }
      });
}
 
Example #8
Source File: ThriftUtilities.java    From hbase with Apache License 2.0 6 votes vote down vote up
public static Append appendFromThrift(TAppend append) throws IOException {
  Append out = new Append(append.getRow());
  for (TColumnValue column : append.getColumns()) {
    out.addColumn(column.getFamily(), column.getQualifier(), column.getValue());
  }

  if (append.isSetAttributes()) {
    addAttributes(out, append.getAttributes());
  }

  if (append.isSetDurability()) {
    out.setDurability(durabilityFromThrift(append.getDurability()));
  }

  if(append.getCellVisibility() != null) {
    out.setCellVisibility(new CellVisibility(append.getCellVisibility().getExpression()));
  }

  if (append.isSetReturnResults()) {
    out.setReturnResults(append.isReturnResults());
  }

  return out;
}
 
Example #9
Source File: Sequence.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public Append createSequence(long startWith, long incrementBy, long cacheSize, long timestamp, long minValue, long maxValue, boolean cycle) {
    byte[] key = this.key.getKey();
    Append append = new Append(key);
    append.setAttribute(SequenceRegionObserver.OPERATION_ATTRIB, new byte[] {(byte)MetaOp.CREATE_SEQUENCE.ordinal()});
    if (timestamp != HConstants.LATEST_TIMESTAMP) {
        append.setAttribute(SequenceRegionObserver.MAX_TIMERANGE_ATTRIB, Bytes.toBytes(timestamp));
    }
    Map<byte[], List<Cell>> familyMap = append.getFamilyCellMap();
    byte[] startWithBuf = PLong.INSTANCE.toBytes(startWith);
    familyMap.put(PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, Arrays.<Cell>asList(
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_BYTES, timestamp, ByteUtil.EMPTY_BYTE_ARRAY),
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.CURRENT_VALUE_BYTES, timestamp, startWithBuf),
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.START_WITH_BYTES, timestamp, startWithBuf),
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.INCREMENT_BY_BYTES, timestamp, PLong.INSTANCE.toBytes(incrementBy)),
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.CACHE_SIZE_BYTES, timestamp, PLong.INSTANCE.toBytes(cacheSize)),
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.MIN_VALUE_BYTES, timestamp, PLong.INSTANCE.toBytes(minValue)),
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.MAX_VALUE_BYTES, timestamp, PLong.INSTANCE.toBytes(maxValue)),
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.CYCLE_FLAG_BYTES, timestamp, PBoolean.INSTANCE.toBytes(cycle)),
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.LIMIT_REACHED_FLAG_BYTES, timestamp, PDataType.FALSE_BYTES)
            ));
    return append;
}
 
Example #10
Source File: TestAccessController.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testAppend() throws Exception {

  AccessTestAction appendAction = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      byte[] row = TEST_ROW;
      byte[] qualifier = TEST_QUALIFIER;
      Put put = new Put(row);
      put.addColumn(TEST_FAMILY, qualifier, Bytes.toBytes(1));
      Append append = new Append(row);
      append.addColumn(TEST_FAMILY, qualifier, Bytes.toBytes(2));
      try(Connection conn = ConnectionFactory.createConnection(conf);
          Table t = conn.getTable(TEST_TABLE)) {
        t.put(put);
        t.append(append);
      }
      return null;
    }
  };

  verifyAllowed(appendAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_CREATE, USER_RW,
    USER_GROUP_WRITE);
  verifyDenied(appendAction, USER_RO, USER_NONE, USER_GROUP_CREATE, USER_GROUP_READ,
    USER_GROUP_ADMIN);
}
 
Example #11
Source File: ConnectionQueryServicesImpl.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public long dropSequence(String tenantId, String schemaName, String sequenceName, long timestamp) throws SQLException {
    SequenceKey sequenceKey = new SequenceKey(tenantId, schemaName, sequenceName, nSequenceSaltBuckets);
    Sequence newSequences = new Sequence(sequenceKey);
    Sequence sequence = sequenceMap.putIfAbsent(sequenceKey, newSequences);
    if (sequence == null) {
        sequence = newSequences;
    }
    try {
        sequence.getLock().lock();
        // Now that we have the lock we need, create the sequence
        Append append = sequence.dropSequence(timestamp);
        HTableInterface htable = this.getTable(PhoenixDatabaseMetaData.SEQUENCE_FULLNAME_BYTES);
        try {
            Result result = htable.append(append);
            return sequence.dropSequence(result);
        } catch (IOException e) {
            throw ServerUtil.parseServerException(e);
        } finally {
            Closeables.closeQuietly(htable);
        }
    } finally {
        sequence.getLock().unlock();
    }
}
 
Example #12
Source File: Sequence.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public Append createSequence(long startWith, long incrementBy, long cacheSize, long timestamp, long minValue, long maxValue, boolean cycle) {
    byte[] key = this.key.getKey();
    Append append = new Append(key);
    append.setAttribute(SequenceRegionObserver.OPERATION_ATTRIB, new byte[] {(byte)MetaOp.CREATE_SEQUENCE.ordinal()});
    if (timestamp != HConstants.LATEST_TIMESTAMP) {
        append.setAttribute(SequenceRegionObserver.MAX_TIMERANGE_ATTRIB, Bytes.toBytes(timestamp));
    }
    Map<byte[], List<Cell>> familyMap = append.getFamilyCellMap();
    byte[] startWithBuf = PLong.INSTANCE.toBytes(startWith);
    familyMap.put(PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, Arrays.<Cell>asList(
            PhoenixKeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_BYTES, timestamp, ByteUtil.EMPTY_BYTE_ARRAY),
            PhoenixKeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.CURRENT_VALUE_BYTES, timestamp, startWithBuf),
            PhoenixKeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.START_WITH_BYTES, timestamp, startWithBuf),
            PhoenixKeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.INCREMENT_BY_BYTES, timestamp, PLong.INSTANCE.toBytes(incrementBy)),
            PhoenixKeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.CACHE_SIZE_BYTES, timestamp, PLong.INSTANCE.toBytes(cacheSize)),
            PhoenixKeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.MIN_VALUE_BYTES, timestamp, PLong.INSTANCE.toBytes(minValue)),
            PhoenixKeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.MAX_VALUE_BYTES, timestamp, PLong.INSTANCE.toBytes(maxValue)),
            PhoenixKeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.CYCLE_FLAG_BYTES, timestamp, PBoolean.INSTANCE.toBytes(cycle)),
            PhoenixKeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.LIMIT_REACHED_FLAG_BYTES, timestamp, PDataType.FALSE_BYTES)
            ));
    return append;
}
 
Example #13
Source File: ConnectionQueryServicesImpl.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public long createSequence(String tenantId, String schemaName, String sequenceName, long startWith, long incrementBy, int cacheSize, long timestamp) 
        throws SQLException {
    SequenceKey sequenceKey = new SequenceKey(tenantId, schemaName, sequenceName);
    Sequence newSequences = new Sequence(sequenceKey);
    Sequence sequence = sequenceMap.putIfAbsent(sequenceKey, newSequences);
    if (sequence == null) {
        sequence = newSequences;
    }
    try {
        sequence.getLock().lock();
        // Now that we have the lock we need, create the sequence
        Append append = sequence.createSequence(startWith, incrementBy, cacheSize, timestamp);
        HTableInterface htable = this.getTable(PhoenixDatabaseMetaData.SEQUENCE_TABLE_NAME_BYTES);
        try {
            Result result = htable.append(append);
            return sequence.createSequence(result);
        } catch (IOException e) {
            throw ServerUtil.parseServerException(e);
        }
    } finally {
        sequence.getLock().unlock();
    }
}
 
Example #14
Source File: ConnectionQueryServicesImpl.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public long dropSequence(String tenantId, String schemaName, String sequenceName, long timestamp) throws SQLException {
    SequenceKey sequenceKey = new SequenceKey(tenantId, schemaName, sequenceName);
    Sequence newSequences = new Sequence(sequenceKey);
    Sequence sequence = sequenceMap.putIfAbsent(sequenceKey, newSequences);
    if (sequence == null) {
        sequence = newSequences;
    }
    try {
        sequence.getLock().lock();
        // Now that we have the lock we need, create the sequence
        Append append = sequence.dropSequence(timestamp);
        HTableInterface htable = this.getTable(PhoenixDatabaseMetaData.SEQUENCE_TABLE_NAME_BYTES);
        try {
            Result result = htable.append(append);
            return sequence.dropSequence(result);
        } catch (IOException e) {
            throw ServerUtil.parseServerException(e);
        }
    } finally {
        sequence.getLock().unlock();
    }
}
 
Example #15
Source File: TestAppendTimeRange.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Override
public Result preAppend(final ObserverContext<RegionCoprocessorEnvironment> e,
    final Append append) throws IOException {
  NavigableMap<byte [], List<Cell>> map = append.getFamilyCellMap();
  for (Map.Entry<byte [], List<Cell>> entry : map.entrySet()) {
    for (Cell cell : entry.getValue()) {
      String appendStr = Bytes.toString(cell.getValueArray(), cell.getValueOffset(),
          cell.getValueLength());
      if (appendStr.equals("b")) {
        tr10 = append.getTimeRange();
      } else if (appendStr.equals("c") && !append.getTimeRange().isAllTime()) {
        tr2 = append.getTimeRange();
      }
    }
  }
  return null;
}
 
Example #16
Source File: TestPostIncrementAndAppendBeforeWAL.java    From hbase with Apache License 2.0 6 votes vote down vote up
@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)));
  }
}
 
Example #17
Source File: Sequence.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Append createSequence(long startWith, long incrementBy, int cacheSize, long timestamp) {
    byte[] key = SchemaUtil.getSequenceKey(this.key.getTenantId(), this.key.getSchemaName(), this.key.getSequenceName());
    Append append = new Append(key);
    append.setAttribute(SequenceRegionObserver.OPERATION_ATTRIB, new byte[] {(byte)SequenceRegionObserver.Op.CREATE_SEQUENCE.ordinal()});
    if (timestamp != HConstants.LATEST_TIMESTAMP) {
        append.setAttribute(SequenceRegionObserver.MAX_TIMERANGE_ATTRIB, Bytes.toBytes(timestamp));
    }
    Map<byte[], List<KeyValue>> familyMap = append.getFamilyMap();
    byte[] startWithBuf = PDataType.LONG.toBytes(startWith);
    familyMap.put(PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, Arrays.<KeyValue>asList(
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_BYTES, timestamp, ByteUtil.EMPTY_BYTE_ARRAY),
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.CURRENT_VALUE_BYTES, timestamp, startWithBuf),
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.START_WITH_BYTES, timestamp, startWithBuf),
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.INCREMENT_BY_BYTES, timestamp, PDataType.LONG.toBytes(incrementBy)),
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.CACHE_SIZE_BYTES, timestamp, PDataType.INTEGER.toBytes(cacheSize))
            ));
    return append;
}
 
Example #18
Source File: Sequence.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public Append dropSequence(long timestamp) {
    byte[] key =  this.key.getKey();
    Append append = new Append(key);
    append.setAttribute(SequenceRegionObserver.OPERATION_ATTRIB, new byte[] {(byte)MetaOp.DROP_SEQUENCE.ordinal()});
    if (timestamp != HConstants.LATEST_TIMESTAMP) {
        append.setAttribute(SequenceRegionObserver.MAX_TIMERANGE_ATTRIB, Bytes.toBytes(timestamp));
    }
    Map<byte[], List<Cell>> familyMap = append.getFamilyCellMap();
    familyMap.put(PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, Arrays.<Cell>asList(
            PhoenixKeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_BYTES, timestamp, ByteUtil.EMPTY_BYTE_ARRAY)));
    return append;
}
 
Example #19
Source File: Sequence.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private Append newReturn(SequenceValue value) {
    byte[] key = this.key.getKey();
    Append append = new Append(key);
    byte[] opBuf = new byte[] {(byte)MetaOp.RETURN_SEQUENCE.ordinal()};
    append.setAttribute(SequenceRegionObserver.OPERATION_ATTRIB, opBuf);
    append.setAttribute(SequenceRegionObserver.CURRENT_VALUE_ATTRIB, PLong.INSTANCE.toBytes(value.nextValue));
    Map<byte[], List<Cell>> familyMap = append.getFamilyCellMap();
    familyMap.put(PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, Arrays.<Cell>asList(
    		PhoenixKeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.CURRENT_VALUE_BYTES, value.timestamp, PLong.INSTANCE.toBytes(value.currentValue)),
    		PhoenixKeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.LIMIT_REACHED_FLAG_BYTES, value.timestamp, PBoolean.INSTANCE.toBytes(value.limitReached))
            ));
    return append;
}
 
Example #20
Source File: Sequence.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Append newReturn(SequenceValue value) {
    byte[] key = SchemaUtil.getSequenceKey(this.key.getTenantId(), this.key.getSchemaName(), this.key.getSequenceName());
    Append append = new Append(key);
    byte[] opBuf = new byte[] {(byte)SequenceRegionObserver.Op.RETURN_SEQUENCE.ordinal()};
    append.setAttribute(SequenceRegionObserver.OPERATION_ATTRIB, opBuf);
    append.setAttribute(SequenceRegionObserver.CURRENT_VALUE_ATTRIB, PDataType.LONG.toBytes(value.nextValue));
    Map<byte[], List<KeyValue>> familyMap = append.getFamilyMap();
    familyMap.put(PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, Arrays.<KeyValue>asList(
            KeyValueUtil.newKeyValue(key, PhoenixDatabaseMetaData.SEQUENCE_FAMILY_BYTES, PhoenixDatabaseMetaData.CURRENT_VALUE_BYTES, value.timestamp, PDataType.LONG.toBytes(value.currentValue))
            ));
    return append;
}
 
Example #21
Source File: Sequence.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public Append newReturn(long timestamp) throws EmptySequenceCacheException {
    SequenceValue value = findSequenceValue(timestamp);
    if (value == null) {
        throw EMPTY_SEQUENCE_CACHE_EXCEPTION;
    }
    if (value.currentValue == value.nextValue) {
        throw EMPTY_SEQUENCE_CACHE_EXCEPTION;
    }
    return newReturn(value);
}
 
Example #22
Source File: ProtobufUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
public static MutationProto toMutation(final MutationType type, final Mutation mutation,
    MutationProto.Builder builder, long nonce)
throws IOException {
  builder = getMutationBuilderAndSetCommonFields(type, mutation, builder);
  if (nonce != HConstants.NO_NONCE) {
    builder.setNonce(nonce);
  }
  if (type == MutationType.INCREMENT) {
    builder.setTimeRange(ProtobufUtil.toTimeRange(((Increment) mutation).getTimeRange()));
  }
  if (type == MutationType.APPEND) {
    builder.setTimeRange(ProtobufUtil.toTimeRange(((Append) mutation).getTimeRange()));
  }
  ColumnValue.Builder columnBuilder = ColumnValue.newBuilder();
  QualifierValue.Builder valueBuilder = QualifierValue.newBuilder();
  for (Map.Entry<byte[],List<Cell>> family: mutation.getFamilyCellMap().entrySet()) {
    columnBuilder.clear();
    columnBuilder.setFamily(UnsafeByteOperations.unsafeWrap(family.getKey()));
    for (Cell cell: family.getValue()) {
      valueBuilder.clear();
      valueBuilder.setQualifier(UnsafeByteOperations.unsafeWrap(
          cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()));
      valueBuilder.setValue(UnsafeByteOperations.unsafeWrap(
          cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
      valueBuilder.setTimestamp(cell.getTimestamp());
      if (type == MutationType.DELETE || (type == MutationType.PUT && CellUtil.isDelete(cell))) {
        KeyValue.Type keyValueType = KeyValue.Type.codeToType(cell.getTypeByte());
        valueBuilder.setDeleteType(toDeleteType(keyValueType));
      }
      columnBuilder.addQualifierValue(valueBuilder.build());
    }
    builder.addColumnValue(columnBuilder.build());
  }
  return builder.build();
}
 
Example #23
Source File: Sequence.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public List<Append> newReturns() {
    if (values == null) {
        return Collections.emptyList();
    }
    List<Append> appends = Lists.newArrayListWithExpectedSize(values.size());
    for (SequenceValue value : values) {
        if (value.isInitialized() && value.currentValue != value.nextValue) {
            appends.add(newReturn(value));
        }
    }
    return appends;
}
 
Example #24
Source File: HBaseAction.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public Status act(UpstreamJob job, byte[] bytes) {
    HBaseConnection connection = connectionMap.get(job.getTopic());
    if (connection == null) {
        LogUtils.logErrorInfo("HBASE_error", "no hbase connection for topic=" + job.getTopic());
        return FAIL;
    }

    if (CollectionUtils.isNotEmpty(job.getHbaseCommands())) {
        try {
            for (HbaseCommand hbaseCommand : job.getHbaseCommands()) {
                HTableInterface table = connection.getTable(hbaseCommand.getTableName());
                Mutation mutation = hbaseCommand.getMutation();

                if (mutation instanceof Put) {
                    table.put((Put) mutation);
                } else if (mutation instanceof Delete) {
                    table.delete((Delete) mutation);
                } else if (mutation instanceof Append) {
                    table.append((Append) mutation);
                } else if (mutation instanceof Increment) {
                    table.increment((Increment) mutation);
                }
            }
            MetricUtils.qpsAndFilterMetric(job, MetricUtils.ConsumeResult.SUCCESS);
            return FINISH;
        } catch (IOException e) {
            LogUtils.logErrorInfo("HBASE_error", "job=" + job, e);
            return FAIL;
        }
    } else {
        LogUtils.logErrorInfo("HBASE_error", "no hbase command found, group:{}, topic:{}", group, job.getTopic());
        return FAIL;
    }
}
 
Example #25
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);
  }
}
 
Example #26
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 #27
Source File: TestIncrementAndAppendWithNullResult.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void testAppend(Append append) throws Exception {
  checkResult(table.append(append));
  List<Row> actions = Arrays.asList(append, append);
  Object[] results = new Object[actions.size()];
  table.batch(actions, results);
  checkResult(results);
}
 
Example #28
Source File: TestIncrementAndAppendWithNullResult.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testAppend() throws Exception {
  testAppend(new Append(ROW_A).addColumn(TEST_FAMILY, qualifierCol1,
      Bytes.toBytes("value")));
  testAppend(new Append(ROW_A).addColumn(TEST_FAMILY, qualifierCol1,
      Bytes.toBytes("value")).setReturnResults(false));

}
 
Example #29
Source File: TestAppendTimeRange.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testHTableInterfaceMethods() throws Exception {
  try (Table table = util.createTable(TableName.valueOf(name.getMethodName()), TEST_FAMILY)) {
    table.put(new Put(ROW).addColumn(TEST_FAMILY, QUAL, VALUE));
    long time = EnvironmentEdgeManager.currentTime();
    mee.setValue(time);
    table.put(new Put(ROW).addColumn(TEST_FAMILY, QUAL, Bytes.toBytes("a")));
    checkRowValue(table, ROW, Bytes.toBytes("a"));

    time = EnvironmentEdgeManager.currentTime();
    mee.setValue(time);
    TimeRange range10 = new TimeRange(1, time + 10);
    Result r = table.append(new Append(ROW).addColumn(TEST_FAMILY, QUAL, Bytes.toBytes("b"))
        .setTimeRange(range10.getMin(), range10.getMax()));
    checkRowValue(table, ROW, Bytes.toBytes("ab"));
    assertEquals(MyObserver.tr10.getMin(), range10.getMin());
    assertEquals(MyObserver.tr10.getMax(), range10.getMax());
    time = EnvironmentEdgeManager.currentTime();
    mee.setValue(time);
    TimeRange range2 = new TimeRange(1, time+20);
    List<Row> actions =
        Arrays.asList(new Row[] {
            new Append(ROW).addColumn(TEST_FAMILY, QUAL, Bytes.toBytes("c"))
                .setTimeRange(range2.getMin(), range2.getMax()),
            new Append(ROW).addColumn(TEST_FAMILY, QUAL, Bytes.toBytes("c"))
                .setTimeRange(range2.getMin(), range2.getMax()) });
    Object[] results1 = new Object[actions.size()];
    table.batch(actions, results1);
    assertEquals(MyObserver.tr2.getMin(), range2.getMin());
    assertEquals(MyObserver.tr2.getMax(), range2.getMax());
    for (Object r2 : results1) {
      assertTrue(r2 instanceof Result);
    }
    checkRowValue(table, ROW, Bytes.toBytes("abcc"));
  }
}
 
Example #30
Source File: TestPassCustomCellViaRegionObserver.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public Result preAppend(ObserverContext<RegionCoprocessorEnvironment> c, Append append)
  throws IOException {
  append.add(createCustomCell(append));
  COUNT.incrementAndGet();
  return null;
}