Search in sources :

Example 61 with Increment

use of org.apache.hadoop.hbase.client.Increment in project hbase by apache.

the class RequestConverter method buildMultiRequest.

private static ClientProtos.MultiRequest buildMultiRequest(final byte[] regionName, final RowMutations rowMutations, final Condition condition, long nonceGroup, long nonce) throws IOException {
    RegionAction.Builder builder = getRegionActionBuilderWithRegion(RegionAction.newBuilder(), regionName);
    builder.setAtomic(true);
    boolean hasNonce = false;
    ClientProtos.Action.Builder actionBuilder = ClientProtos.Action.newBuilder();
    MutationProto.Builder mutationBuilder = MutationProto.newBuilder();
    for (Mutation mutation : rowMutations.getMutations()) {
        mutationBuilder.clear();
        MutationProto mp;
        if (mutation instanceof Increment || mutation instanceof Append) {
            mp = ProtobufUtil.toMutation(getMutationType(mutation), mutation, mutationBuilder, nonce);
            hasNonce = true;
        } else {
            mp = ProtobufUtil.toMutation(getMutationType(mutation), mutation, mutationBuilder);
        }
        actionBuilder.clear();
        actionBuilder.setMutation(mp);
        builder.addAction(actionBuilder.build());
    }
    if (condition != null) {
        builder.setCondition(condition);
    }
    MultiRequest.Builder multiRequestBuilder = MultiRequest.newBuilder();
    if (hasNonce) {
        multiRequestBuilder.setNonceGroup(nonceGroup);
    }
    return multiRequestBuilder.addRegionAction(builder.build()).build();
}
Also used : Action(org.apache.hadoop.hbase.client.Action) RegionAction(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionAction) Append(org.apache.hadoop.hbase.client.Append) MultiRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MultiRequest) Increment(org.apache.hadoop.hbase.client.Increment) RegionAction(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.RegionAction) Mutation(org.apache.hadoop.hbase.client.Mutation) MutationProto(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto)

Example 62 with Increment

use of org.apache.hadoop.hbase.client.Increment in project hbase by apache.

the class ProtobufUtil method toMutation.

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();
}
Also used : Append(org.apache.hadoop.hbase.client.Append) KeyValue(org.apache.hadoop.hbase.KeyValue) Increment(org.apache.hadoop.hbase.client.Increment) QualifierValue(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.ColumnValue.QualifierValue) ColumnValue(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.ColumnValue) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) Cell(org.apache.hadoop.hbase.Cell) ByteBufferExtendedCell(org.apache.hadoop.hbase.ByteBufferExtendedCell)

Example 63 with Increment

use of org.apache.hadoop.hbase.client.Increment in project hbase by apache.

the class TestProtobufUtil method testIncrement.

/**
 * Test Increment Mutate conversions.
 *
 * @throws IOException if converting to an {@link Increment} or
 *                     {@link org.apache.hadoop.hbase.client.Mutation} fails
 */
@Test
public void testIncrement() throws IOException {
    MutationProto proto = getIncrementMutation(111111L);
    // default fields
    assertEquals(MutationProto.Durability.USE_DEFAULT, proto.getDurability());
    // set the default value for equal comparison
    MutationProto.Builder mutateBuilder = MutationProto.newBuilder(proto);
    mutateBuilder.setDurability(MutationProto.Durability.USE_DEFAULT);
    Increment increment = ProtobufUtil.toIncrement(proto, null);
    mutateBuilder.setTimestamp(increment.getTimestamp());
    mutateBuilder.setTimeRange(ProtobufUtil.toTimeRange(increment.getTimeRange()));
    assertEquals(mutateBuilder.build(), ProtobufUtil.toMutation(MutationType.INCREMENT, increment));
}
Also used : Increment(org.apache.hadoop.hbase.client.Increment) MutationProto(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto) Test(org.junit.Test)

Example 64 with Increment

use of org.apache.hadoop.hbase.client.Increment in project hbase by apache.

the class TestRegionServerReadRequestMetrics method testReadRequestsCountNotFiltered.

@Test
public void testReadRequestsCountNotFiltered() throws Exception {
    int resultCount;
    Scan scan;
    Append append;
    Put put;
    Increment increment;
    Get get;
    // test for scan
    scan = new Scan();
    try (ResultScanner scanner = table.getScanner(scan)) {
        resultCount = 0;
        for (Result ignore : scanner) {
            resultCount++;
        }
        testReadRequests(resultCount, 3, 0);
    }
    // test for scan
    scan = new Scan().withStartRow(ROW2).withStopRow(ROW3);
    try (ResultScanner scanner = table.getScanner(scan)) {
        resultCount = 0;
        for (Result ignore : scanner) {
            resultCount++;
        }
        testReadRequests(resultCount, 1, 0);
    }
    // test for get
    get = new Get(ROW2);
    Result result = table.get(get);
    resultCount = result.isEmpty() ? 0 : 1;
    testReadRequests(resultCount, 1, 0);
    // test for increment
    increment = new Increment(ROW1);
    increment.addColumn(CF1, COL3, 1);
    result = table.increment(increment);
    resultCount = result.isEmpty() ? 0 : 1;
    testReadRequests(resultCount, 1, 0);
    // test for checkAndPut
    put = new Put(ROW1);
    put.addColumn(CF1, COL2, VAL2);
    boolean checkAndPut = table.checkAndMutate(ROW1, CF1).qualifier(COL2).ifEquals(VAL2).thenPut(put);
    resultCount = checkAndPut ? 1 : 0;
    testReadRequests(resultCount, 1, 0);
    // test for append
    append = new Append(ROW1);
    append.addColumn(CF1, COL2, VAL2);
    result = table.append(append);
    resultCount = result.isEmpty() ? 0 : 1;
    testReadRequests(resultCount, 1, 0);
    // test for checkAndMutate
    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);
    resultCount = checkAndMutate ? 1 : 0;
    testReadRequests(resultCount, 1, 0);
}
Also used : Append(org.apache.hadoop.hbase.client.Append) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) Increment(org.apache.hadoop.hbase.client.Increment) Get(org.apache.hadoop.hbase.client.Get) Scan(org.apache.hadoop.hbase.client.Scan) Put(org.apache.hadoop.hbase.client.Put) Result(org.apache.hadoop.hbase.client.Result) RowMutations(org.apache.hadoop.hbase.client.RowMutations) Test(org.junit.Test)

Example 65 with Increment

use of org.apache.hadoop.hbase.client.Increment in project hbase by apache.

the class TestRegionIncrement method testUnContendedSingleCellIncrement.

/**
 * Have each thread update its own Cell. Avoid contention with another thread.
 */
@Test
public void testUnContendedSingleCellIncrement() throws IOException, InterruptedException {
    final HRegion region = getRegion(TEST_UTIL.getConfiguration(), TestIncrementsFromClientSide.filterStringSoTableNameSafe(this.name.getMethodName()));
    long startTime = EnvironmentEdgeManager.currentTime();
    try {
        SingleCellIncrementer[] threads = new SingleCellIncrementer[THREAD_COUNT];
        for (int i = 0; i < threads.length; i++) {
            byte[] rowBytes = Bytes.toBytes(i);
            Increment increment = new Increment(rowBytes);
            increment.addColumn(INCREMENT_BYTES, INCREMENT_BYTES, 1);
            threads[i] = new SingleCellIncrementer(i, INCREMENT_COUNT, region, increment);
        }
        for (int i = 0; i < threads.length; i++) {
            threads[i].start();
        }
        for (int i = 0; i < threads.length; i++) {
            threads[i].join();
        }
        RegionScanner regionScanner = region.getScanner(new Scan());
        List<Cell> cells = new ArrayList<>(THREAD_COUNT);
        while (regionScanner.next(cells)) continue;
        assertEquals(THREAD_COUNT, cells.size());
        long total = 0;
        for (Cell cell : cells) total += Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
        assertEquals(INCREMENT_COUNT * THREAD_COUNT, total);
    } finally {
        closeRegion(region);
        LOG.info(this.name.getMethodName() + " " + (EnvironmentEdgeManager.currentTime() - startTime) + "ms");
    }
}
Also used : Increment(org.apache.hadoop.hbase.client.Increment) ArrayList(java.util.ArrayList) Scan(org.apache.hadoop.hbase.client.Scan) Cell(org.apache.hadoop.hbase.Cell) Test(org.junit.Test)

Aggregations

Increment (org.apache.hadoop.hbase.client.Increment)81 Test (org.junit.Test)42 Put (org.apache.hadoop.hbase.client.Put)31 Append (org.apache.hadoop.hbase.client.Append)25 Result (org.apache.hadoop.hbase.client.Result)25 Delete (org.apache.hadoop.hbase.client.Delete)21 Get (org.apache.hadoop.hbase.client.Get)19 IOException (java.io.IOException)16 TableName (org.apache.hadoop.hbase.TableName)15 Table (org.apache.hadoop.hbase.client.Table)15 ArrayList (java.util.ArrayList)14 Cell (org.apache.hadoop.hbase.Cell)11 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)11 CheckAndMutateResult (org.apache.hadoop.hbase.client.CheckAndMutateResult)9 Mutation (org.apache.hadoop.hbase.client.Mutation)9 RowMutations (org.apache.hadoop.hbase.client.RowMutations)9 List (java.util.List)8 Map (java.util.Map)8 Scan (org.apache.hadoop.hbase.client.Scan)7 KeyValue (org.apache.hadoop.hbase.KeyValue)5