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);
}
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);
}
}
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);
}
}
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;
}
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));
}
Aggregations