FlatRowAdapter implements ResponseAdapter<FlatRow, Result> { @Override public Result adaptResponse(FlatRow flatRow) { if (flatRow == null || flatRow.getRowKey() == null) { return Result.EMPTY_RESULT; } byte[] RowKey = flatRow.getRowKey().toByteArray(); List<FlatRow.Cell> cells = flatRow.getCells(); List<Cell> hbaseCells = new ArrayList<>(cells.size()); byte[] previousFamilyBytes = null; String previousFamily = null; for (FlatRow.Cell cell : cells) { String family = cell.getFamily(); byte[] familyBytes = !Objects.equal(family, previousFamily) ? Bytes.toBytes(family) : previousFamilyBytes; hbaseCells.add(toRowCell(RowKey, cell, familyBytes)); previousFamily = family; previousFamilyBytes = familyBytes; } return Result.create(hbaseCells); }  @Override Result adaptResponse(FlatRow flatRow);  }
@Test public void adaptResponse_null() { assertNull(instance.adaptResponse(null).rawCells()); }
@Test public void adaptResponse_emptyRow() { FlatRow row = FlatRow.newBuilder().withRowKey(ByteString.copyFromUtf8("key")).build(); Result result = instance.adaptResponse(row); assertEquals(0, result.rawCells().length); assertNull(transformToFlatRow(result)); }
@Test public void adaptResponse_oneRow() { String family1 = "family1"; String family2 = "family2"; byte[] qualifier1 = "qualifier1".getBytes(); byte[] qualifier2 = "qualifier2".getBytes(); byte[] value1 = "value1".getBytes(); byte[] value2 = "value2".getBytes(); byte[] value3 = "value3".getBytes(); byte[] value4 = "value4".getBytes(); byte[] value5 = "value5".getBytes(); byte[] value6 = "value6".getBytes(); FlatRow row = FlatRow.newBuilder() .withRowKey(ByteString.copyFromUtf8("key")) .addCell(family1, ByteString.copyFrom(qualifier1), 54321L, ByteString.copyFrom(value1)) .addCell(family1, ByteString.copyFrom(qualifier1), 12345L, ByteString.copyFrom(value2)) .addCell( family1, ByteString.copyFrom(qualifier1), 12345L, ByteString.copyFrom(value6), Collections.singletonList("label")) .addCell(family1, ByteString.copyFrom(qualifier2), 54321L, ByteString.copyFrom(value3)) .addCell(family2, ByteString.copyFrom(qualifier1), 54321L, ByteString.copyFrom(value4)) .addCell(family2, ByteString.copyFrom(qualifier2), 54321L, ByteString.copyFrom(value5)) .build(); Result result = instance.adaptResponse(row); assertEquals(6, result.rawCells().length); List<org.apache.hadoop.hbase.Cell> cells1 = result.getColumnCells(family1.getBytes(), qualifier1); assertEquals(3, cells1.size()); assertEquals(Bytes.toString(value1), Bytes.toString(CellUtil.cloneValue(cells1.get(0)))); assertEquals(Bytes.toString(value2), Bytes.toString(CellUtil.cloneValue(cells1.get(1)))); assertEquals(Bytes.toString(value6), Bytes.toString(CellUtil.cloneValue(cells1.get(2)))); List<org.apache.hadoop.hbase.Cell> cells2 = result.getColumnCells(family1.getBytes(), qualifier2); assertEquals(1, cells2.size()); assertEquals(Bytes.toString(value3), Bytes.toString(CellUtil.cloneValue(cells2.get(0)))); List<org.apache.hadoop.hbase.Cell> cells3 = result.getColumnCells(family2.getBytes(), qualifier1); assertEquals(1, cells3.size()); assertEquals(Bytes.toString(value4), Bytes.toString(CellUtil.cloneValue(cells3.get(0)))); List<org.apache.hadoop.hbase.Cell> cells4 = result.getColumnCells(family2.getBytes(), qualifier2); assertEquals(1, cells4.size()); assertEquals(Bytes.toString(value5), Bytes.toString(CellUtil.cloneValue(cells4.get(0)))); FlatRow expected = FlatRow.newBuilder() .withRowKey(ByteString.copyFromUtf8("key")) .addCell(family1, ByteString.copyFrom(qualifier1), 54000L, ByteString.copyFrom(value1)) .addCell(family1, ByteString.copyFrom(qualifier1), 12000L, ByteString.copyFrom(value2)) .addCell( family1, ByteString.copyFrom(qualifier1), 12000L, ByteString.copyFrom(value6), Collections.singletonList("label")) .addCell(family1, ByteString.copyFrom(qualifier2), 54000L, ByteString.copyFrom(value3)) .addCell(family2, ByteString.copyFrom(qualifier1), 54000L, ByteString.copyFrom(value4)) .addCell(family2, ByteString.copyFrom(qualifier2), 54000L, ByteString.copyFrom(value5)) .build(); assertEquals(expected, transformToFlatRow(result)); }
@Test public void resultRoundTrip() { Cell inputKeyValue = CellUtil.createCell( "key".getBytes(), "family".getBytes(), "qualifier".getBytes(), 1200, Type.Put.getCode(), "value".getBytes()); Result inputResult = Result.create(new Cell[] {inputKeyValue}); FlatRow intermediateRow = transformToFlatRow(inputResult); Result outputRow = instance.adaptResponse(intermediateRow); Cell outputCell = outputRow.listCells().get(0); assertTrue(CellComparator.equals(inputKeyValue, outputCell)); }