use of org.apache.hadoop.hbase.thrift2.generated.TResult in project hbase by apache.
the class TestThriftHBaseServiceHandler method testPutTTL.
@Test
public void testPutTTL() throws Exception {
ThriftHBaseServiceHandler handler = createHandler();
byte[] rowName = "testPutTTL".getBytes();
ByteBuffer table = wrap(tableAname);
List<TColumnValue> columnValues = new ArrayList<>(1);
// Add some dummy data
columnValues.add(new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(Bytes.toBytes(1L))));
TPut put = new TPut(wrap(rowName), columnValues);
put.setColumnValues(columnValues);
Map<ByteBuffer, ByteBuffer> attributes = new HashMap<>();
// Time in ms for the kv's to live.
long ttlTimeMs = 2000L;
// the _ttl attribute is a number of ms ttl for key values in this put.
attributes.put(wrap(Bytes.toBytes("_ttl")), wrap(Bytes.toBytes(ttlTimeMs)));
// Attach the attributes
put.setAttributes(attributes);
// Send it.
handler.put(table, put);
// Now get the data back
TGet getOne = new TGet(wrap(rowName));
TResult resultOne = handler.get(table, getOne);
// It's there.
assertArrayEquals(rowName, resultOne.getRow());
assertEquals(1, resultOne.getColumnValuesSize());
// Sleep 30 seconds just to make 100% sure that the key value should be expired.
Thread.sleep(ttlTimeMs * 15);
TGet getTwo = new TGet(wrap(rowName));
TResult resultTwo = handler.get(table, getTwo);
// Nothing should be there since it's ttl'd out.
assertNull(resultTwo.getRow());
assertEquals(0, resultTwo.getColumnValuesSize());
}
use of org.apache.hadoop.hbase.thrift2.generated.TResult in project hbase by apache.
the class TestThriftHBaseServiceHandler method testPutGetMultiple.
@Test
public void testPutGetMultiple() throws Exception {
ThriftHBaseServiceHandler handler = createHandler();
ByteBuffer table = wrap(tableAname);
byte[] rowName1 = "testPutGetMultiple1".getBytes();
byte[] rowName2 = "testPutGetMultiple2".getBytes();
List<TColumnValue> columnValues = new ArrayList<>(2);
columnValues.add(new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)));
columnValues.add(new TColumnValue(wrap(familyBname), wrap(qualifierBname), wrap(valueBname)));
List<TPut> puts = new ArrayList<>(2);
puts.add(new TPut(wrap(rowName1), columnValues));
puts.add(new TPut(wrap(rowName2), columnValues));
handler.putMultiple(table, puts);
List<TGet> gets = new ArrayList<>(2);
gets.add(new TGet(wrap(rowName1)));
gets.add(new TGet(wrap(rowName2)));
List<TResult> results = handler.getMultiple(table, gets);
assertEquals(2, results.size());
assertArrayEquals(rowName1, results.get(0).getRow());
assertTColumnValuesEqual(columnValues, results.get(0).getColumnValues());
assertArrayEquals(rowName2, results.get(1).getRow());
assertTColumnValuesEqual(columnValues, results.get(1).getColumnValues());
}
use of org.apache.hadoop.hbase.thrift2.generated.TResult in project hbase by apache.
the class TestThriftHBaseServiceHandler method testExceptionType.
private void testExceptionType(THBaseService.Iface handler, ThriftMetrics metrics, ByteBuffer tTableName, byte[] rowkey, ErrorThrowingGetObserver.ErrorType errorType) {
long preGetCounter = metricsHelper.getCounter("get_num_ops", metrics.getSource());
String exceptionKey = errorType.getMetricName();
long preExceptionCounter = metricsHelper.checkCounterExists(exceptionKey, metrics.getSource()) ? metricsHelper.getCounter(exceptionKey, metrics.getSource()) : 0;
TGet tGet = new TGet(wrap(rowkey));
Map<ByteBuffer, ByteBuffer> attributes = new HashMap<>();
attributes.put(wrap(Bytes.toBytes(ErrorThrowingGetObserver.SHOULD_ERROR_ATTRIBUTE)), wrap(Bytes.toBytes(errorType.name())));
tGet.setAttributes(attributes);
try {
TResult tResult = handler.get(tTableName, tGet);
fail("Get with error attribute should have thrown an exception");
} catch (TException e) {
LOG.info("Received exception: ", e);
metricsHelper.assertCounter("get_num_ops", preGetCounter + 1, metrics.getSource());
metricsHelper.assertCounter(exceptionKey, preExceptionCounter + 1, metrics.getSource());
}
}
use of org.apache.hadoop.hbase.thrift2.generated.TResult in project hbase by apache.
the class ThriftUtilities method resultFromHBase.
/**
* Creates a {@link TResult} (Thrift) from a {@link Result} (HBase).
*
* @param in the <code>Result</code> to convert
*
* @return converted result, returns an empty result if the input is <code>null</code>
*/
public static TResult resultFromHBase(Result in) {
Cell[] raw = in.rawCells();
TResult out = new TResult();
byte[] row = in.getRow();
if (row != null) {
out.setRow(in.getRow());
}
List<TColumnValue> columnValues = new ArrayList<>(raw.length);
for (Cell kv : raw) {
TColumnValue col = new TColumnValue();
col.setFamily(CellUtil.cloneFamily(kv));
col.setQualifier(CellUtil.cloneQualifier(kv));
col.setTimestamp(kv.getTimestamp());
col.setValue(CellUtil.cloneValue(kv));
if (kv.getTagsLength() > 0) {
col.setTags(CellUtil.getTagArray(kv));
}
columnValues.add(col);
}
out.setColumnValues(columnValues);
return out;
}
use of org.apache.hadoop.hbase.thrift2.generated.TResult in project hbase by apache.
the class TestThriftHBaseServiceHandlerWithLabels method testScanWithVisibilityLabels.
@Test
public void testScanWithVisibilityLabels() throws Exception {
ThriftHBaseServiceHandler handler = createHandler();
ByteBuffer table = wrap(tableAname);
// insert data
TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname));
List<TColumnValue> columnValues = new ArrayList<>(1);
columnValues.add(columnValue);
for (int i = 0; i < 10; i++) {
TPut put = new TPut(wrap(("testScan" + i).getBytes()), columnValues);
if (i == 5) {
put.setCellVisibility(new TCellVisibility().setExpression(PUBLIC));
} else {
put.setCellVisibility(new TCellVisibility().setExpression("(" + SECRET + "|" + CONFIDENTIAL + ")" + "&" + "!" + TOPSECRET));
}
handler.put(table, put);
}
// create scan instance
TScan scan = new TScan();
List<TColumn> columns = new ArrayList<>(1);
TColumn column = new TColumn();
column.setFamily(familyAname);
column.setQualifier(qualifierAname);
columns.add(column);
scan.setColumns(columns);
scan.setStartRow("testScan".getBytes());
scan.setStopRow("testScan".getBytes());
TAuthorization tauth = new TAuthorization();
List<String> labels = new ArrayList<>(2);
labels.add(SECRET);
labels.add(PRIVATE);
tauth.setLabels(labels);
scan.setAuthorizations(tauth);
// get scanner and rows
int scanId = handler.openScanner(table, scan);
List<TResult> results = handler.getScannerRows(scanId, 10);
assertEquals(9, results.size());
Assert.assertFalse(Bytes.equals(results.get(5).getRow(), ("testScan" + 5).getBytes()));
for (int i = 0; i < 9; i++) {
if (i < 5) {
assertArrayEquals(("testScan" + i).getBytes(), results.get(i).getRow());
} else if (i == 5) {
continue;
} else {
assertArrayEquals(("testScan" + (i + 1)).getBytes(), results.get(i).getRow());
}
}
// check that we are at the end of the scan
results = handler.getScannerRows(scanId, 9);
assertEquals(0, results.size());
// close scanner and check that it was indeed closed
handler.closeScanner(scanId);
try {
handler.getScannerRows(scanId, 9);
fail("Scanner id should be invalid");
} catch (TIllegalArgument e) {
}
}
Aggregations