Search in sources :

Example 1 with TRowMutations

use of org.apache.hadoop.hbase.thrift2.generated.TRowMutations in project hbase by apache.

the class TestThriftHBaseServiceHandler method testMutateRow.

/**
 * Put valueA to a row, make sure put has happened, then create a mutation object to put valueB
 * and delete ValueA, then check that the row value is only valueB.
 */
@Test
public void testMutateRow() throws Exception {
    ThriftHBaseServiceHandler handler = createHandler();
    byte[] rowName = Bytes.toBytes("testMutateRow");
    ByteBuffer table = wrap(tableAname);
    List<TColumnValue> columnValuesA = new ArrayList<>(1);
    TColumnValue columnValueA = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname));
    columnValuesA.add(columnValueA);
    TPut putA = new TPut(wrap(rowName), columnValuesA);
    putA.setColumnValues(columnValuesA);
    handler.put(table, putA);
    TGet get = new TGet(wrap(rowName));
    TResult result = handler.get(table, get);
    assertArrayEquals(rowName, result.getRow());
    List<TColumnValue> returnedColumnValues = result.getColumnValues();
    List<TColumnValue> expectedColumnValues = new ArrayList<>(1);
    expectedColumnValues.add(columnValueA);
    assertTColumnValuesEqual(expectedColumnValues, returnedColumnValues);
    List<TColumnValue> columnValuesB = new ArrayList<>(1);
    TColumnValue columnValueB = new TColumnValue(wrap(familyAname), wrap(qualifierBname), wrap(valueBname));
    columnValuesB.add(columnValueB);
    TPut putB = new TPut(wrap(rowName), columnValuesB);
    putB.setColumnValues(columnValuesB);
    TDelete delete = new TDelete(wrap(rowName));
    List<TColumn> deleteColumns = new ArrayList<>(1);
    TColumn deleteColumn = new TColumn(wrap(familyAname));
    deleteColumn.setQualifier(qualifierAname);
    deleteColumns.add(deleteColumn);
    delete.setColumns(deleteColumns);
    List<TMutation> mutations = new ArrayList<>(2);
    TMutation mutationA = TMutation.put(putB);
    mutations.add(mutationA);
    TMutation mutationB = TMutation.deleteSingle(delete);
    mutations.add(mutationB);
    TRowMutations tRowMutations = new TRowMutations(wrap(rowName), mutations);
    handler.mutateRow(table, tRowMutations);
    result = handler.get(table, get);
    assertArrayEquals(rowName, result.getRow());
    returnedColumnValues = result.getColumnValues();
    expectedColumnValues = new ArrayList<>(1);
    expectedColumnValues.add(columnValueB);
    assertTColumnValuesEqual(expectedColumnValues, returnedColumnValues);
}
Also used : TGet(org.apache.hadoop.hbase.thrift2.generated.TGet) TColumn(org.apache.hadoop.hbase.thrift2.generated.TColumn) ArrayList(java.util.ArrayList) TDelete(org.apache.hadoop.hbase.thrift2.generated.TDelete) TColumnValue(org.apache.hadoop.hbase.thrift2.generated.TColumnValue) ByteBuffer(java.nio.ByteBuffer) TResult(org.apache.hadoop.hbase.thrift2.generated.TResult) TMutation(org.apache.hadoop.hbase.thrift2.generated.TMutation) TRowMutations(org.apache.hadoop.hbase.thrift2.generated.TRowMutations) TPut(org.apache.hadoop.hbase.thrift2.generated.TPut) Test(org.junit.Test)

Example 2 with TRowMutations

use of org.apache.hadoop.hbase.thrift2.generated.TRowMutations in project hbase by apache.

the class TestThriftHBaseServiceHandlerWithReadOnly method testCheckAndMutateWithReadOnly.

@Test
public void testCheckAndMutateWithReadOnly() throws Exception {
    ThriftHBaseServiceHandler handler = createHandler();
    ByteBuffer table = wrap(tableAname);
    ByteBuffer row = wrap(Bytes.toBytes("row"));
    ByteBuffer family = wrap(familyAname);
    ByteBuffer qualifier = wrap(qualifierAname);
    ByteBuffer value = wrap(valueAname);
    List<TColumnValue> columnValuesB = new ArrayList<>(1);
    TColumnValue columnValueB = new TColumnValue(family, wrap(qualifierBname), wrap(valueBname));
    columnValuesB.add(columnValueB);
    TPut putB = new TPut(row, columnValuesB);
    putB.setColumnValues(columnValuesB);
    TRowMutations tRowMutations = new TRowMutations(row, Arrays.<TMutation>asList(TMutation.put(putB)));
    boolean exceptionCaught = false;
    try {
        handler.checkAndMutate(table, row, family, qualifier, TCompareOperator.EQUAL, value, tRowMutations);
    } catch (TIOError e) {
        exceptionCaught = true;
        assertTrue(e.getCause() instanceof DoNotRetryIOException);
        assertEquals("Thrift Server is in Read-only mode.", e.getMessage());
    } finally {
        assertTrue(exceptionCaught);
    }
}
Also used : TIOError(org.apache.hadoop.hbase.thrift2.generated.TIOError) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) ArrayList(java.util.ArrayList) TRowMutations(org.apache.hadoop.hbase.thrift2.generated.TRowMutations) TColumnValue(org.apache.hadoop.hbase.thrift2.generated.TColumnValue) TPut(org.apache.hadoop.hbase.thrift2.generated.TPut) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with TRowMutations

use of org.apache.hadoop.hbase.thrift2.generated.TRowMutations in project hbase by apache.

the class TestThriftHBaseServiceHandlerWithReadOnly method testMutateRowWithReadOnly.

@Test
public void testMutateRowWithReadOnly() throws Exception {
    ThriftHBaseServiceHandler handler = createHandler();
    byte[] rowName = Bytes.toBytes("testMutateRow");
    ByteBuffer table = wrap(tableAname);
    List<TColumnValue> columnValuesA = new ArrayList<>(1);
    TColumnValue columnValueA = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname));
    columnValuesA.add(columnValueA);
    TPut putA = new TPut(wrap(rowName), columnValuesA);
    putA.setColumnValues(columnValuesA);
    TDelete delete = new TDelete(wrap(rowName));
    List<TMutation> mutations = new ArrayList<>(2);
    TMutation mutationA = TMutation.put(putA);
    mutations.add(mutationA);
    TMutation mutationB = TMutation.deleteSingle(delete);
    mutations.add(mutationB);
    TRowMutations tRowMutations = new TRowMutations(wrap(rowName), mutations);
    boolean exceptionCaught = false;
    try {
        handler.mutateRow(table, tRowMutations);
    } catch (TIOError e) {
        exceptionCaught = true;
        assertTrue(e.getCause() instanceof DoNotRetryIOException);
        assertEquals("Thrift Server is in Read-only mode.", e.getMessage());
    } finally {
        assertTrue(exceptionCaught);
    }
}
Also used : DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) ArrayList(java.util.ArrayList) TDelete(org.apache.hadoop.hbase.thrift2.generated.TDelete) TColumnValue(org.apache.hadoop.hbase.thrift2.generated.TColumnValue) ByteBuffer(java.nio.ByteBuffer) TIOError(org.apache.hadoop.hbase.thrift2.generated.TIOError) TMutation(org.apache.hadoop.hbase.thrift2.generated.TMutation) TRowMutations(org.apache.hadoop.hbase.thrift2.generated.TRowMutations) TPut(org.apache.hadoop.hbase.thrift2.generated.TPut) Test(org.junit.Test)

Example 4 with TRowMutations

use of org.apache.hadoop.hbase.thrift2.generated.TRowMutations in project hbase by apache.

the class ThriftUtilities method rowMutationsFromHBase.

public static TRowMutations rowMutationsFromHBase(RowMutations in) {
    TRowMutations tRowMutations = new TRowMutations();
    tRowMutations.setRow(in.getRow());
    for (Mutation mutation : in.getMutations()) {
        TMutation tMutation = new TMutation();
        if (mutation instanceof Put) {
            tMutation.setPut(ThriftUtilities.putFromHBase((Put) mutation));
        } else if (mutation instanceof Delete) {
            tMutation.setDeleteSingle(ThriftUtilities.deleteFromHBase((Delete) mutation));
        } else {
            throw new IllegalArgumentException("Only Put and Delete is supported in mutateRow, but muation=" + mutation);
        }
        tRowMutations.addToMutations(tMutation);
    }
    return tRowMutations;
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) TDelete(org.apache.hadoop.hbase.thrift2.generated.TDelete) TRowMutations(org.apache.hadoop.hbase.thrift2.generated.TRowMutations) TMutation(org.apache.hadoop.hbase.thrift2.generated.TMutation) Mutation(org.apache.hadoop.hbase.client.Mutation) TMutation(org.apache.hadoop.hbase.thrift2.generated.TMutation) TPut(org.apache.hadoop.hbase.thrift2.generated.TPut) Put(org.apache.hadoop.hbase.client.Put)

Example 5 with TRowMutations

use of org.apache.hadoop.hbase.thrift2.generated.TRowMutations in project hbase by apache.

the class TestThriftHBaseServiceHandler method testCheckAndMutate.

@Test
public void testCheckAndMutate() throws Exception {
    ThriftHBaseServiceHandler handler = createHandler();
    ByteBuffer table = wrap(tableAname);
    ByteBuffer row = wrap(Bytes.toBytes("row"));
    ByteBuffer family = wrap(familyAname);
    ByteBuffer qualifier = wrap(qualifierAname);
    ByteBuffer value = wrap(valueAname);
    // Create a mutation to write to 'B', our "mutate" of "checkAndMutate"
    List<TColumnValue> columnValuesB = new ArrayList<>(1);
    TColumnValue columnValueB = new TColumnValue(family, wrap(qualifierBname), wrap(valueBname));
    columnValuesB.add(columnValueB);
    TPut putB = new TPut(row, columnValuesB);
    putB.setColumnValues(columnValuesB);
    TRowMutations tRowMutations = new TRowMutations(row, Arrays.<TMutation>asList(TMutation.put(putB)));
    // Empty table when we begin
    TResult result = handler.get(table, new TGet(row));
    assertEquals(0, result.getColumnValuesSize());
    // checkAndMutate -- condition should fail because the value doesn't exist.
    assertFalse("Expected condition to not pass", handler.checkAndMutate(table, row, family, qualifier, TCompareOperator.EQUAL, value, tRowMutations));
    List<TColumnValue> columnValuesA = new ArrayList<>(1);
    TColumnValue columnValueA = new TColumnValue(family, qualifier, value);
    columnValuesA.add(columnValueA);
    // Put an update 'A'
    handler.put(table, new TPut(row, columnValuesA));
    // Verify that the update is there
    result = handler.get(table, new TGet(row));
    assertEquals(1, result.getColumnValuesSize());
    assertTColumnValueEqual(columnValueA, result.getColumnValues().get(0));
    // checkAndMutate -- condition should pass since we added the value
    assertTrue("Expected condition to pass", handler.checkAndMutate(table, row, family, qualifier, TCompareOperator.EQUAL, value, tRowMutations));
    result = handler.get(table, new TGet(row));
    assertEquals(2, result.getColumnValuesSize());
    assertTColumnValueEqual(columnValueA, result.getColumnValues().get(0));
    assertTColumnValueEqual(columnValueB, result.getColumnValues().get(1));
}
Also used : TGet(org.apache.hadoop.hbase.thrift2.generated.TGet) ArrayList(java.util.ArrayList) TRowMutations(org.apache.hadoop.hbase.thrift2.generated.TRowMutations) TColumnValue(org.apache.hadoop.hbase.thrift2.generated.TColumnValue) TPut(org.apache.hadoop.hbase.thrift2.generated.TPut) ByteBuffer(java.nio.ByteBuffer) TResult(org.apache.hadoop.hbase.thrift2.generated.TResult) Test(org.junit.Test)

Aggregations

TRowMutations (org.apache.hadoop.hbase.thrift2.generated.TRowMutations)6 TPut (org.apache.hadoop.hbase.thrift2.generated.TPut)5 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 TColumnValue (org.apache.hadoop.hbase.thrift2.generated.TColumnValue)4 TMutation (org.apache.hadoop.hbase.thrift2.generated.TMutation)4 Test (org.junit.Test)4 TDelete (org.apache.hadoop.hbase.thrift2.generated.TDelete)3 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)2 TGet (org.apache.hadoop.hbase.thrift2.generated.TGet)2 TIOError (org.apache.hadoop.hbase.thrift2.generated.TIOError)2 TResult (org.apache.hadoop.hbase.thrift2.generated.TResult)2 Delete (org.apache.hadoop.hbase.client.Delete)1 Mutation (org.apache.hadoop.hbase.client.Mutation)1 Put (org.apache.hadoop.hbase.client.Put)1 RowMutations (org.apache.hadoop.hbase.client.RowMutations)1 TColumn (org.apache.hadoop.hbase.thrift2.generated.TColumn)1