Search in sources :

Example 46 with Append

use of org.apache.hadoop.hbase.client.Append 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 47 with Append

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

the class TestProtobufUtil method testAppend.

/**
 * Test Append Mutate conversions.
 *
 * @throws IOException if converting to an {@link Append} fails
 */
@Test
public void testAppend() throws IOException {
    MutationProto proto = getAppendMutation(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);
    Append append = ProtobufUtil.toAppend(proto, null);
    // append always use the latest timestamp,
    // reset the timestamp to the original mutate
    mutateBuilder.setTimestamp(append.getTimestamp());
    mutateBuilder.setTimeRange(ProtobufUtil.toTimeRange(append.getTimeRange()));
    assertEquals(mutateBuilder.build(), ProtobufUtil.toMutation(MutationType.APPEND, append));
}
Also used : Append(org.apache.hadoop.hbase.client.Append) MutationProto(org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto) Test(org.junit.Test)

Example 48 with Append

use of org.apache.hadoop.hbase.client.Append 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 49 with Append

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

the class TestAtomicOperation method testAppendMultiThreads.

@Test
public void testAppendMultiThreads() throws IOException {
    LOG.info("Starting test testAppendMultiThreads");
    // run a with mixed column families (1 and 3 versions)
    initHRegion(tableName, name.getMethodName(), new int[] { 1, 3 }, fam1, fam2);
    int numThreads = 100;
    int opsPerThread = 100;
    AtomicOperation[] all = new AtomicOperation[numThreads];
    final byte[] val = new byte[] { 1 };
    AtomicInteger failures = new AtomicInteger(0);
    // create all threads
    for (int i = 0; i < numThreads; i++) {
        all[i] = new AtomicOperation(region, opsPerThread, null, failures) {

            @Override
            public void run() {
                for (int i = 0; i < numOps; i++) {
                    try {
                        Append a = new Append(row);
                        a.addColumn(fam1, qual1, val);
                        a.addColumn(fam1, qual2, val);
                        a.addColumn(fam2, qual3, val);
                        a.setDurability(Durability.ASYNC_WAL);
                        region.append(a, HConstants.NO_NONCE, HConstants.NO_NONCE);
                        Get g = new Get(row);
                        Result result = region.get(g);
                        assertEquals(result.getValue(fam1, qual1).length, result.getValue(fam1, qual2).length);
                        assertEquals(result.getValue(fam1, qual1).length, result.getValue(fam2, qual3).length);
                    } catch (IOException e) {
                        e.printStackTrace();
                        failures.incrementAndGet();
                        fail();
                    }
                }
            }
        };
    }
    // run all threads
    for (int i = 0; i < numThreads; i++) {
        all[i].start();
    }
    // wait for all threads to finish
    for (int i = 0; i < numThreads; i++) {
        try {
            all[i].join();
        } catch (InterruptedException e) {
        }
    }
    assertEquals(0, failures.get());
    Get g = new Get(row);
    Result result = region.get(g);
    assertEquals(10000, result.getValue(fam1, qual1).length);
    assertEquals(10000, result.getValue(fam1, qual2).length);
    assertEquals(10000, result.getValue(fam2, qual3).length);
}
Also used : Append(org.apache.hadoop.hbase.client.Append) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Get(org.apache.hadoop.hbase.client.Get) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result) Test(org.junit.Test)

Example 50 with Append

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

the class TestAtomicOperation method testAppend.

// ////////////////////////////////////////////////////////////////////////////
// New tests that doesn't spin up a mini cluster but rather just test the
// individual code pieces in the HRegion.
// ////////////////////////////////////////////////////////////////////////////
/**
 * Test basic append operation.
 * More tests in
 * @see org.apache.hadoop.hbase.client.TestFromClientSide#testAppend()
 */
@Test
public void testAppend() throws IOException {
    initHRegion(tableName, name.getMethodName(), fam1);
    String v1 = "Ultimate Answer to the Ultimate Question of Life," + " The Universe, and Everything";
    String v2 = " is... 42.";
    Append a = new Append(row);
    a.setReturnResults(false);
    a.addColumn(fam1, qual1, Bytes.toBytes(v1));
    a.addColumn(fam1, qual2, Bytes.toBytes(v2));
    assertTrue(region.append(a, HConstants.NO_NONCE, HConstants.NO_NONCE).isEmpty());
    a = new Append(row);
    a.addColumn(fam1, qual1, Bytes.toBytes(v2));
    a.addColumn(fam1, qual2, Bytes.toBytes(v1));
    Result result = region.append(a, HConstants.NO_NONCE, HConstants.NO_NONCE);
    assertEquals(0, Bytes.compareTo(Bytes.toBytes(v1 + v2), result.getValue(fam1, qual1)));
    assertEquals(0, Bytes.compareTo(Bytes.toBytes(v2 + v1), result.getValue(fam1, qual2)));
}
Also used : Append(org.apache.hadoop.hbase.client.Append) Result(org.apache.hadoop.hbase.client.Result) Test(org.junit.Test)

Aggregations

Append (org.apache.hadoop.hbase.client.Append)62 Test (org.junit.Test)31 Result (org.apache.hadoop.hbase.client.Result)26 Increment (org.apache.hadoop.hbase.client.Increment)25 Put (org.apache.hadoop.hbase.client.Put)23 IOException (java.io.IOException)17 Get (org.apache.hadoop.hbase.client.Get)17 Delete (org.apache.hadoop.hbase.client.Delete)16 Table (org.apache.hadoop.hbase.client.Table)15 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)10 TableName (org.apache.hadoop.hbase.TableName)10 RowMutations (org.apache.hadoop.hbase.client.RowMutations)10 Cell (org.apache.hadoop.hbase.Cell)9 CheckAndMutateResult (org.apache.hadoop.hbase.client.CheckAndMutateResult)8 Mutation (org.apache.hadoop.hbase.client.Mutation)7 ArrayList (java.util.ArrayList)5 CheckAndMutate (org.apache.hadoop.hbase.client.CheckAndMutate)5 MutationProto (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto)5 ByteString (org.apache.hbase.thirdparty.com.google.protobuf.ByteString)5 List (java.util.List)4