use of org.apache.hadoop.hbase.thrift.generated.TRowResult in project hbase by apache.
the class ThriftUtilities method rowResultFromHBase.
/**
* This utility method creates a list of Thrift TRowResult "struct" based on
* an Hbase RowResult object. The empty list is returned if the input is
* null.
*
* @param in
* Hbase RowResult object
* @param sortColumns
* This boolean dictates if row data is returned in a sorted order
* sortColumns = True will set TRowResult's sortedColumns member
* which is an ArrayList of TColumn struct
* sortColumns = False will set TRowResult's columns member which is
* a map of columnName and TCell struct
* @return Thrift TRowResult array
*/
public static List<TRowResult> rowResultFromHBase(Result[] in, boolean sortColumns) {
List<TRowResult> results = new ArrayList<>(in.length);
for (Result result_ : in) {
if (result_ == null || result_.isEmpty()) {
continue;
}
TRowResult result = new TRowResult();
result.row = ByteBuffer.wrap(result_.getRow());
if (sortColumns) {
result.sortedColumns = new ArrayList<>();
for (Cell kv : result_.rawCells()) {
result.sortedColumns.add(new TColumn(ByteBuffer.wrap(KeyValue.makeColumn(CellUtil.cloneFamily(kv), CellUtil.cloneQualifier(kv))), new TCell(ByteBuffer.wrap(CellUtil.cloneValue(kv)), kv.getTimestamp())));
}
} else {
result.columns = new TreeMap<>();
for (Cell kv : result_.rawCells()) {
result.columns.put(ByteBuffer.wrap(KeyValue.makeColumn(CellUtil.cloneFamily(kv), CellUtil.cloneQualifier(kv))), new TCell(ByteBuffer.wrap(CellUtil.cloneValue(kv)), kv.getTimestamp()));
}
}
results.add(result);
}
return results;
}
use of org.apache.hadoop.hbase.thrift.generated.TRowResult in project hbase by apache.
the class TestThriftServer method testMetricsWithException.
@Test
public void testMetricsWithException() throws Exception {
String rowkey = "row1";
String family = "f";
String col = "c";
// create a table which will throw exceptions for requests
final TableName tableName = TableName.valueOf(name.getMethodName());
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
tableDesc.addCoprocessor(ErrorThrowingGetObserver.class.getName());
tableDesc.addFamily(new HColumnDescriptor(family));
Table table = UTIL.createTable(tableDesc, null);
long now = System.currentTimeMillis();
table.put(new Put(Bytes.toBytes(rowkey)).addColumn(Bytes.toBytes(family), Bytes.toBytes(col), now, Bytes.toBytes("val1")));
Configuration conf = UTIL.getConfiguration();
ThriftMetrics metrics = getMetrics(conf);
ThriftServerRunner.HBaseHandler hbaseHandler = new ThriftServerRunner.HBaseHandler(UTIL.getConfiguration(), UserProvider.instantiate(UTIL.getConfiguration()));
Hbase.Iface handler = HbaseHandlerMetricsProxy.newInstance(hbaseHandler, metrics, conf);
ByteBuffer tTableName = asByteBuffer(tableName.getNameAsString());
// check metrics increment with a successful get
long preGetCounter = metricsHelper.checkCounterExists("getRow_num_ops", metrics.getSource()) ? metricsHelper.getCounter("getRow_num_ops", metrics.getSource()) : 0;
List<TRowResult> tRowResult = handler.getRow(tTableName, asByteBuffer(rowkey), null);
assertEquals(1, tRowResult.size());
TRowResult tResult = tRowResult.get(0);
TCell expectedColumnValue = new TCell(asByteBuffer("val1"), now);
assertArrayEquals(Bytes.toBytes(rowkey), tResult.getRow());
Collection<TCell> returnedColumnValues = tResult.getColumns().values();
assertEquals(1, returnedColumnValues.size());
assertEquals(expectedColumnValue, returnedColumnValues.iterator().next());
metricsHelper.assertCounter("getRow_num_ops", preGetCounter + 1, metrics.getSource());
// check metrics increment when the get throws each exception type
for (ErrorThrowingGetObserver.ErrorType type : ErrorThrowingGetObserver.ErrorType.values()) {
testExceptionType(handler, metrics, tTableName, rowkey, type);
}
}
use of org.apache.hadoop.hbase.thrift.generated.TRowResult in project hbase by apache.
the class TestThriftServer method doTestTableScanners.
/**
* Tests the four different scanner-opening methods (with and without
* a stoprow, with and without a timestamp).
*
* @throws Exception
*/
public void doTestTableScanners() throws Exception {
// Setup
ThriftServerRunner.HBaseHandler handler = new ThriftServerRunner.HBaseHandler(UTIL.getConfiguration(), UserProvider.instantiate(UTIL.getConfiguration()));
handler.createTable(tableAname, getColumnDescriptors());
// Apply timestamped Mutations to rowA
long time1 = System.currentTimeMillis();
handler.mutateRowTs(tableAname, rowAname, getMutations(), time1, null);
// Sleep to assure that 'time1' and 'time2' will be different even with a
// coarse grained system timer.
Thread.sleep(1000);
// Apply timestamped BatchMutations for rowA and rowB
long time2 = System.currentTimeMillis();
handler.mutateRowsTs(tableAname, getBatchMutations(), time2, null);
time1 += 1;
// Test a scanner on all rows and all columns, no timestamp
int scanner1 = handler.scannerOpen(tableAname, rowAname, getColumnList(true, true), null);
TRowResult rowResult1a = handler.scannerGet(scanner1).get(0);
assertEquals(rowResult1a.row, rowAname);
// This used to be '1'. I don't know why when we are asking for two columns
// and when the mutations above would seem to add two columns to the row.
// -- St.Ack 05/12/2009
assertEquals(rowResult1a.columns.size(), 1);
assertEquals(rowResult1a.columns.get(columnBname).value, valueCname);
TRowResult rowResult1b = handler.scannerGet(scanner1).get(0);
assertEquals(rowResult1b.row, rowBname);
assertEquals(rowResult1b.columns.size(), 2);
assertEquals(rowResult1b.columns.get(columnAname).value, valueCname);
assertEquals(rowResult1b.columns.get(columnBname).value, valueDname);
closeScanner(scanner1, handler);
// Test a scanner on all rows and all columns, with timestamp
int scanner2 = handler.scannerOpenTs(tableAname, rowAname, getColumnList(true, true), time1, null);
TRowResult rowResult2a = handler.scannerGet(scanner2).get(0);
assertEquals(rowResult2a.columns.size(), 1);
// column A deleted, does not exist.
//assertTrue(Bytes.equals(rowResult2a.columns.get(columnAname).value, valueAname));
assertEquals(rowResult2a.columns.get(columnBname).value, valueBname);
closeScanner(scanner2, handler);
// Test a scanner on the first row and first column only, no timestamp
int scanner3 = handler.scannerOpenWithStop(tableAname, rowAname, rowBname, getColumnList(true, false), null);
closeScanner(scanner3, handler);
// Test a scanner on the first row and second column only, with timestamp
int scanner4 = handler.scannerOpenWithStopTs(tableAname, rowAname, rowBname, getColumnList(false, true), time1, null);
TRowResult rowResult4a = handler.scannerGet(scanner4).get(0);
assertEquals(rowResult4a.columns.size(), 1);
assertEquals(rowResult4a.columns.get(columnBname).value, valueBname);
// Test scanner using a TScan object once with sortColumns False and once with sortColumns true
TScan scanNoSortColumns = new TScan();
scanNoSortColumns.setStartRow(rowAname);
scanNoSortColumns.setStopRow(rowBname);
int scanner5 = handler.scannerOpenWithScan(tableAname, scanNoSortColumns, null);
TRowResult rowResult5 = handler.scannerGet(scanner5).get(0);
assertEquals(rowResult5.columns.size(), 1);
assertEquals(rowResult5.columns.get(columnBname).value, valueCname);
TScan scanSortColumns = new TScan();
scanSortColumns.setStartRow(rowAname);
scanSortColumns.setStopRow(rowBname);
scanSortColumns = scanSortColumns.setSortColumns(true);
int scanner6 = handler.scannerOpenWithScan(tableAname, scanSortColumns, null);
TRowResult rowResult6 = handler.scannerGet(scanner6).get(0);
assertEquals(rowResult6.sortedColumns.size(), 1);
assertEquals(rowResult6.sortedColumns.get(0).getCell().value, valueCname);
List<Mutation> rowBmutations = new ArrayList<>(20);
for (int i = 0; i < 20; i++) {
rowBmutations.add(new Mutation(false, asByteBuffer("columnA:" + i), valueCname, true));
}
ByteBuffer rowC = asByteBuffer("rowC");
handler.mutateRow(tableAname, rowC, rowBmutations, null);
TScan scanSortMultiColumns = new TScan();
scanSortMultiColumns.setStartRow(rowC);
scanSortMultiColumns = scanSortMultiColumns.setSortColumns(true);
int scanner7 = handler.scannerOpenWithScan(tableAname, scanSortMultiColumns, null);
TRowResult rowResult7 = handler.scannerGet(scanner7).get(0);
ByteBuffer smallerColumn = asByteBuffer("columnA:");
for (int i = 0; i < 20; i++) {
ByteBuffer currentColumn = rowResult7.sortedColumns.get(i).columnName;
assertTrue(Bytes.compareTo(smallerColumn.array(), currentColumn.array()) < 0);
smallerColumn = currentColumn;
}
TScan reversedScan = new TScan();
reversedScan.setReversed(true);
reversedScan.setStartRow(rowBname);
reversedScan.setStopRow(rowAname);
int scanner8 = handler.scannerOpenWithScan(tableAname, reversedScan, null);
List<TRowResult> results = handler.scannerGet(scanner8);
handler.scannerClose(scanner8);
assertEquals(results.size(), 1);
assertEquals(ByteBuffer.wrap(results.get(0).getRow()), rowBname);
// Teardown
handler.disableTable(tableAname);
handler.deleteTable(tableAname);
}
use of org.apache.hadoop.hbase.thrift.generated.TRowResult in project hbase by apache.
the class TestThriftServer method doTestCheckAndPut.
/**
* Check that checkAndPut fails if the cell does not exist, then put in the cell, then check that
* the checkAndPut succeeds.
*
* @throws Exception
*/
public static void doTestCheckAndPut() throws Exception {
ThriftServerRunner.HBaseHandler handler = new ThriftServerRunner.HBaseHandler(UTIL.getConfiguration(), UserProvider.instantiate(UTIL.getConfiguration()));
handler.createTable(tableAname, getColumnDescriptors());
try {
List<Mutation> mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, columnAname, valueAname, true));
Mutation putB = (new Mutation(false, columnBname, valueBname, true));
assertFalse(handler.checkAndPut(tableAname, rowAname, columnAname, valueAname, putB, null));
handler.mutateRow(tableAname, rowAname, mutations, null);
assertTrue(handler.checkAndPut(tableAname, rowAname, columnAname, valueAname, putB, null));
TRowResult rowResult = handler.getRow(tableAname, rowAname, null).get(0);
assertEquals(rowAname, rowResult.row);
assertEquals(valueBname, rowResult.columns.get(columnBname).value);
} finally {
handler.disableTable(tableAname);
handler.deleteTable(tableAname);
}
}
use of org.apache.hadoop.hbase.thrift.generated.TRowResult in project hbase by apache.
the class DemoClient method run.
private void run() throws Exception {
TTransport transport = new TSocket(host, port);
if (secure) {
Map<String, String> saslProperties = new HashMap<>();
saslProperties.put(Sasl.QOP, "auth-conf,auth-int,auth");
/**
* The Thrift server the DemoClient is trying to connect to
* must have a matching principal, and support authentication.
*
* The HBase cluster must be secure, allow proxy user.
*/
transport = new TSaslClientTransport("GSSAPI", null, // Thrift server user name, should be an authorized proxy user.
"hbase", // Thrift server domain
host, saslProperties, null, transport);
}
transport.open();
TProtocol protocol = new TBinaryProtocol(transport, true, true);
Hbase.Client client = new Hbase.Client(protocol);
byte[] t = bytes("demo_table");
//
// Scan all tables, look for the demo table and delete it.
//
System.out.println("scanning tables...");
for (ByteBuffer name : client.getTableNames()) {
System.out.println(" found: " + utf8(name.array()));
if (utf8(name.array()).equals(utf8(t))) {
if (client.isTableEnabled(name)) {
System.out.println(" disabling table: " + utf8(name.array()));
client.disableTable(name);
}
System.out.println(" deleting table: " + utf8(name.array()));
client.deleteTable(name);
}
}
//
// Create the demo table with two column families, entry: and unused:
//
ArrayList<ColumnDescriptor> columns = new ArrayList<>(2);
ColumnDescriptor col;
col = new ColumnDescriptor();
col.name = ByteBuffer.wrap(bytes("entry:"));
col.timeToLive = Integer.MAX_VALUE;
col.maxVersions = 10;
columns.add(col);
col = new ColumnDescriptor();
col.name = ByteBuffer.wrap(bytes("unused:"));
col.timeToLive = Integer.MAX_VALUE;
columns.add(col);
System.out.println("creating table: " + utf8(t));
try {
client.createTable(ByteBuffer.wrap(t), columns);
} catch (AlreadyExists ae) {
System.out.println("WARN: " + ae.message);
}
System.out.println("column families in " + utf8(t) + ": ");
Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(ByteBuffer.wrap(t));
for (ColumnDescriptor col2 : columnMap.values()) {
System.out.println(" column: " + utf8(col2.name.array()) + ", maxVer: " + Integer.toString(col2.maxVersions));
}
Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
boolean writeToWal = false;
//
// Test UTF-8 handling
//
byte[] invalid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xfc, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1 };
byte[] valid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xE7, (byte) 0x94, (byte) 0x9F, (byte) 0xE3, (byte) 0x83, (byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, (byte) 0xE3, (byte) 0x83, (byte) 0xAB };
ArrayList<Mutation> mutations;
// non-utf8 is fine for data
mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("foo")), mutations, dummyAttributes);
// this row name is valid utf8
mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(valid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(valid), mutations, dummyAttributes);
// non-utf8 is now allowed in row names because HBase stores values as binary
mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(invalid), mutations, dummyAttributes);
// Run a scanner on the rows we just created
ArrayList<ByteBuffer> columnNames = new ArrayList<>();
columnNames.add(ByteBuffer.wrap(bytes("entry:")));
System.out.println("Starting scanner...");
int scanner = client.scannerOpen(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), columnNames, dummyAttributes);
while (true) {
List<TRowResult> entry = client.scannerGet(scanner);
if (entry.isEmpty()) {
break;
}
printRow(entry);
}
//
for (int i = 100; i >= 0; --i) {
// format row keys as "00000" to "00100"
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumIntegerDigits(5);
nf.setGroupingUsed(false);
byte[] row = bytes(nf.format(i));
mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")), ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes);
// sleep to force later timestamp
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// no-op
}
mutations = new ArrayList<>(2);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes("0")), writeToWal));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(bytes("FOO")), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
Mutation m;
mutations = new ArrayList<>(2);
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:foo"));
m.isDelete = true;
mutations.add(m);
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:num"));
m.value = ByteBuffer.wrap(bytes("-1"));
mutations.add(m);
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
mutations = new ArrayList<>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")), ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
// sleep to force later timestamp
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// no-op
}
mutations.clear();
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:num"));
m.value = ByteBuffer.wrap(bytes("-999"));
mutations.add(m);
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:sqr"));
m.isDelete = true;
// shouldn't override latest
client.mutateRowTs(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, 1, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
List<TCell> versions = client.getVer(ByteBuffer.wrap(t), ByteBuffer.wrap(row), ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes);
printVersions(ByteBuffer.wrap(row), versions);
if (versions.isEmpty()) {
System.out.println("FATAL: wrong # of versions");
System.exit(-1);
}
List<TCell> result = client.get(ByteBuffer.wrap(t), ByteBuffer.wrap(row), ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes);
if (!result.isEmpty()) {
System.out.println("FATAL: shouldn't get here");
System.exit(-1);
}
System.out.println("");
}
// scan all rows/columnNames
columnNames.clear();
for (ColumnDescriptor col2 : client.getColumnDescriptors(ByteBuffer.wrap(t)).values()) {
System.out.println("column with name: " + new String(col2.name.array()));
System.out.println(col2.toString());
columnNames.add(col2.name);
}
System.out.println("Starting scanner...");
scanner = client.scannerOpenWithStop(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("00020")), ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes);
while (true) {
List<TRowResult> entry = client.scannerGet(scanner);
if (entry.isEmpty()) {
System.out.println("Scanner finished");
break;
}
printRow(entry);
}
transport.close();
}
Aggregations