Search in sources :

Example 36 with TResult

use of org.apache.hadoop.hbase.thrift2.generated.TResult 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)

Example 37 with TResult

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

the class TestThriftHBaseServiceHandler method testScan.

@Test
public void testScan() 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(Bytes.toBytes("testScan" + i)), columnValues);
        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(Bytes.toBytes("testScan"));
    scan.setStopRow(Bytes.toBytes("testScan\uffff"));
    // get scanner and rows
    int scanId = handler.openScanner(table, scan);
    List<TResult> results = handler.getScannerRows(scanId, 10);
    assertEquals(10, results.size());
    for (int i = 0; i < 10; i++) {
        // check if the rows are returned and in order
        assertArrayEquals(Bytes.toBytes("testScan" + i), results.get(i).getRow());
    }
    // check that we are at the end of the scan
    results = handler.getScannerRows(scanId, 10);
    assertEquals(0, results.size());
    // close scanner and check that it was indeed closed
    handler.closeScanner(scanId);
    try {
        handler.getScannerRows(scanId, 10);
        fail("Scanner id should be invalid");
    } catch (TIllegalArgument e) {
    }
}
Also used : TColumn(org.apache.hadoop.hbase.thrift2.generated.TColumn) ArrayList(java.util.ArrayList) TColumnValue(org.apache.hadoop.hbase.thrift2.generated.TColumnValue) ByteBuffer(java.nio.ByteBuffer) TResult(org.apache.hadoop.hbase.thrift2.generated.TResult) TIllegalArgument(org.apache.hadoop.hbase.thrift2.generated.TIllegalArgument) TScan(org.apache.hadoop.hbase.thrift2.generated.TScan) TPut(org.apache.hadoop.hbase.thrift2.generated.TPut) Test(org.junit.Test)

Example 38 with TResult

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));
        col.setType(kv.getType().getCode());
        if (kv.getTagsLength() > 0) {
            col.setTags(PrivateCellUtil.cloneTags(kv));
        }
        columnValues.add(col);
    }
    out.setColumnValues(columnValues);
    out.setStale(in.isStale());
    out.setPartial(in.mayHaveMoreCellsInRow());
    return out;
}
Also used : ArrayList(java.util.ArrayList) TColumnValue(org.apache.hadoop.hbase.thrift2.generated.TColumnValue) Cell(org.apache.hadoop.hbase.Cell) TResult(org.apache.hadoop.hbase.thrift2.generated.TResult)

Aggregations

TResult (org.apache.hadoop.hbase.thrift2.generated.TResult)36 TColumnValue (org.apache.hadoop.hbase.thrift2.generated.TColumnValue)33 ByteBuffer (java.nio.ByteBuffer)32 ArrayList (java.util.ArrayList)31 TPut (org.apache.hadoop.hbase.thrift2.generated.TPut)31 Test (org.junit.Test)31 TGet (org.apache.hadoop.hbase.thrift2.generated.TGet)23 TColumn (org.apache.hadoop.hbase.thrift2.generated.TColumn)14 TScan (org.apache.hadoop.hbase.thrift2.generated.TScan)11 TIllegalArgument (org.apache.hadoop.hbase.thrift2.generated.TIllegalArgument)8 TDelete (org.apache.hadoop.hbase.thrift2.generated.TDelete)7 TAuthorization (org.apache.hadoop.hbase.thrift2.generated.TAuthorization)6 TCellVisibility (org.apache.hadoop.hbase.thrift2.generated.TCellVisibility)6 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 Table (org.apache.hadoop.hbase.client.Table)3 TColumnIncrement (org.apache.hadoop.hbase.thrift2.generated.TColumnIncrement)3 THBaseService (org.apache.hadoop.hbase.thrift2.generated.THBaseService)3 TIncrement (org.apache.hadoop.hbase.thrift2.generated.TIncrement)3 Cell (org.apache.hadoop.hbase.Cell)2