HBASE-8779 Add mutateRow method support to Thrift2 (Hamed Madani)

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1504251 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Lars George 2013-07-17 20:04:25 +00:00
parent 1356d84fcf
commit aeb529fc78
5 changed files with 1169 additions and 127 deletions

View File

@ -18,16 +18,7 @@
*/
package org.apache.hadoop.hbase.thrift2;
import static org.apache.hadoop.hbase.thrift2.ThriftUtilities.deleteFromThrift;
import static org.apache.hadoop.hbase.thrift2.ThriftUtilities.deletesFromThrift;
import static org.apache.hadoop.hbase.thrift2.ThriftUtilities.getFromThrift;
import static org.apache.hadoop.hbase.thrift2.ThriftUtilities.getsFromThrift;
import static org.apache.hadoop.hbase.thrift2.ThriftUtilities.incrementFromThrift;
import static org.apache.hadoop.hbase.thrift2.ThriftUtilities.putFromThrift;
import static org.apache.hadoop.hbase.thrift2.ThriftUtilities.putsFromThrift;
import static org.apache.hadoop.hbase.thrift2.ThriftUtilities.resultFromHBase;
import static org.apache.hadoop.hbase.thrift2.ThriftUtilities.resultsFromHBase;
import static org.apache.hadoop.hbase.thrift2.ThriftUtilities.scanFromThrift;
import static org.apache.hadoop.hbase.thrift2.ThriftUtilities.*;
import static org.apache.thrift.TBaseHelper.byteBufferToByteArray;
import java.io.IOException;
@ -49,16 +40,9 @@ import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.RowMutations;
import org.apache.hadoop.hbase.thrift.ThriftMetrics;
import org.apache.hadoop.hbase.thrift2.generated.TDelete;
import org.apache.hadoop.hbase.thrift2.generated.TGet;
import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
import org.apache.hadoop.hbase.thrift2.generated.TIOError;
import org.apache.hadoop.hbase.thrift2.generated.TIllegalArgument;
import org.apache.hadoop.hbase.thrift2.generated.TIncrement;
import org.apache.hadoop.hbase.thrift2.generated.TPut;
import org.apache.hadoop.hbase.thrift2.generated.TResult;
import org.apache.hadoop.hbase.thrift2.generated.TScan;
import org.apache.hadoop.hbase.thrift2.generated.*;
import org.apache.thrift.TException;
/**
@ -349,4 +333,16 @@ public class ThriftHBaseServiceHandler implements THBaseService.Iface {
removeScanner(scannerId);
}
@Override
public void mutateRow(ByteBuffer table, TRowMutations rowMutations) throws TIOError, TException {
HTableInterface htable = getTable(table);
try {
htable.mutateRow(rowMutationsFromThrift(rowMutations));
} catch (IOException e) {
throw getTIOError(e);
} finally {
closeTable(htable);
}
}
}

View File

@ -307,6 +307,27 @@ public class ThriftUtilities {
return out;
}
/**
* Creates a {@link RowMutations} (HBase) from a {@link TRowMutations} (Thrift)
*
* @param in the <code>TRowMutations</code> to convert
*
* @return converted <code>RowMutations</code>
*/
public static RowMutations rowMutationsFromThrift(TRowMutations in) throws IOException {
RowMutations out = new RowMutations(in.getRow());
List<TMutation> mutations = in.getMutations();
for (TMutation mutation : mutations) {
if (mutation.isSetPut()) {
out.add(putFromThrift(mutation.getPut()));
}
if (mutation.isSetDeleteSingle()) {
out.add(deleteFromThrift(mutation.getDeleteSingle()));
}
}
return out;
}
public static Scan scanFromThrift(TScan in) throws IOException {
Scan out = new Scan();

View File

@ -182,6 +182,22 @@ struct TScan {
9: optional map<binary, binary> attributes
}
/**
* Atomic mutation for the specified row. It can be either Put or Delete.
*/
union TMutation {
1: optional TPut put,
2: optional TDelete deleteSingle,
}
/**
* A TRowMutations object is used to apply a number of Mutations to a single row.
*/
struct TRowMutations {
1: required binary row
2: required list<TMutation> mutations
}
//
// Exceptions
//
@ -415,4 +431,15 @@ service THBaseService {
2: TIllegalArgument ia
)
/**
* mutateRow performs multiple mutations atomically on a single row.
*/
void mutateRow(
/** table to apply the mutations */
1: required binary table,
/** mutations to apply */
2: required TRowMutations rowMutations
) throws (1: TIOError io)
}

View File

@ -48,6 +48,8 @@ import org.apache.hadoop.hbase.thrift2.generated.TIncrement;
import org.apache.hadoop.hbase.thrift2.generated.TPut;
import org.apache.hadoop.hbase.thrift2.generated.TResult;
import org.apache.hadoop.hbase.thrift2.generated.TScan;
import org.apache.hadoop.hbase.thrift2.generated.TMutation;
import org.apache.hadoop.hbase.thrift2.generated.TRowMutations;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.thrift.TException;
import org.junit.AfterClass;
@ -743,5 +745,67 @@ public class TestThriftHBaseServiceHandler {
assertArrayEquals(delete.getAttribute("attribute1"), attributeValue);
}
/**
* 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.
*
* @throws Exception
*/
@Test
public void testMutateRow() throws Exception {
ThriftHBaseServiceHandler handler = createHandler();
byte[] rowName = "testMutateRow".getBytes();
ByteBuffer table = wrap(tableAname);
List<TColumnValue> columnValuesA = new ArrayList<TColumnValue>();
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<TColumnValue>();
expectedColumnValues.add(columnValueA);
assertTColumnValuesEqual(expectedColumnValues, returnedColumnValues);
List<TColumnValue> columnValuesB = new ArrayList<TColumnValue>();
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<TColumn>();
TColumn deleteColumn = new TColumn(wrap(familyAname));
deleteColumn.setQualifier(qualifierAname);
deleteColumns.add(deleteColumn);
delete.setColumns(deleteColumns);
List<TMutation> mutations = new ArrayList<TMutation>();
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<TColumnValue>();
expectedColumnValues.add(columnValueB);
assertTColumnValuesEqual(expectedColumnValues, returnedColumnValues);
}
}