/** * 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; }
@Override public void mutateRow(ByteBuffer table, TRowMutations rowMutations) throws TIOError, TException { Table htable = getTable(table); try { htable.mutateRow(rowMutationsFromThrift(rowMutations)); } catch (IOException e) { throw getTIOError(e); } finally { closeTable(htable); } }
@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); } }
/** * 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 { List<TMutation> mutations = in.getMutations(); RowMutations out = new RowMutations(in.getRow(), mutations.size()); for (TMutation mutation : mutations) { if (mutation.isSetPut()) { out.add(putFromThrift(mutation.getPut())); } if (mutation.isSetDeleteSingle()) { out.add(deleteFromThrift(mutation.getDeleteSingle())); } } return out; }
@Override public boolean checkAndMutate(ByteBuffer table, ByteBuffer row, ByteBuffer family, ByteBuffer qualifier, TCompareOp compareOp, ByteBuffer value, TRowMutations rowMutations) throws TIOError, TException { checkReadOnlyMode(); try (final Table htable = getTable(table)) { return htable.checkAndMutate(byteBufferToByteArray(row), byteBufferToByteArray(family)) .qualifier(byteBufferToByteArray(qualifier)) .ifMatches(compareOpFromThrift(compareOp), byteBufferToByteArray(value)) .thenMutate(rowMutationsFromThrift(rowMutations)); } catch (IOException e) { throw getTIOError(e); } }
@Override public void mutateRow(ByteBuffer table, TRowMutations rowMutations) throws TIOError, TException { checkReadOnlyMode(); Table htable = getTable(table); try { htable.mutateRow(rowMutationsFromThrift(rowMutations)); } catch (IOException e) { throw getTIOError(e); } finally { closeTable(htable); } }
@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, TCompareOp.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); } }
@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); } }
/** * 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); }
/** * 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); }
@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, TCompareOp.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, TCompareOp.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)); }