FlatRowConverter { public static Row convert(FlatRow row) { if (row == null) { return null; } Row.Builder rowBuilder = Row.newBuilder().setKey(row.getRowKey()); String prevFamily = null; Family.Builder familyBuilder = null; ByteString previousColumn = null; Column.Builder columnBuilder = null; for (FlatRow.Cell cell : row.getCells()) { final String currentFamily = cell.getFamily(); if (!currentFamily.equals(prevFamily)) { if (familyBuilder != null) { if (columnBuilder != null) { familyBuilder.addColumns(columnBuilder.build()); columnBuilder = null; previousColumn = null; } rowBuilder.addFamilies(familyBuilder.build()); } familyBuilder = Family.newBuilder().setName(currentFamily); prevFamily = currentFamily; } ByteString currentQualifier = cell.getQualifier(); if (!currentQualifier.equals(previousColumn)) { if (columnBuilder != null) { familyBuilder.addColumns(columnBuilder.build()); } columnBuilder = Column.newBuilder().setQualifier(currentQualifier); previousColumn = currentQualifier; } columnBuilder.addCells(toCell(cell)); } if (familyBuilder != null) { if (columnBuilder != null) { familyBuilder.addColumns(columnBuilder.build()); } rowBuilder.addFamilies(familyBuilder.build()); } return rowBuilder.build(); }  static Row convert(FlatRow row); static com.google.cloud.bigtable.data.v2.models.Row convertToModelRow(FlatRow row);  }
@Test public void testOneCell() { FlatRow simpleRow = FlatRow.newBuilder() .withRowKey(toByteString("key")) .addCell("family", toByteString("column"), 500, toByteString("value"), null) .build(); FlatRow.Cell simpleCell = simpleRow.getCells().get(0); Row expectedRow = Row.newBuilder() .setKey(simpleRow.getRowKey()) .addFamilies( Family.newBuilder() .setName(simpleCell.getFamily()) .addColumns( Column.newBuilder() .setQualifier(simpleCell.getQualifier()) .addCells(asProtoCell(simpleCell)) .build()) .build()) .build(); Assert.assertEquals(expectedRow, FlatRowConverter.convert(simpleRow)); }
@Test public void testManyCells() { FlatRow simpleRow = FlatRow.newBuilder() .withRowKey(toByteString("key")) .addCell("family1", toByteString("column"), 500, toByteString("value"), null) .addCell("family1", toByteString("column2"), 500, toByteString("value"), null) .addCell("family1", toByteString("column2"), 400, toByteString("value"), null) .addCell("family2", toByteString("column"), 500, toByteString("value"), null) .build(); FlatRow.Cell simpleCell0 = simpleRow.getCells().get(0); FlatRow.Cell simpleCell1 = simpleRow.getCells().get(1); FlatRow.Cell simpleCell2 = simpleRow.getCells().get(2); FlatRow.Cell simpleCell3 = simpleRow.getCells().get(3); Row expectedRow = Row.newBuilder() .setKey(simpleRow.getRowKey()) .addFamilies( Family.newBuilder() .setName(simpleCell0.getFamily()) .addColumns( Column.newBuilder() .setQualifier(simpleCell0.getQualifier()) .addCells(asProtoCell(simpleCell0)) .build()) .addColumns( Column.newBuilder() .setQualifier(simpleCell1.getQualifier()) .addCells(asProtoCell(simpleCell1)) .addCells(asProtoCell(simpleCell2)) .build()) .build()) .addFamilies( Family.newBuilder() .setName(simpleCell3.getFamily()) .addColumns( Column.newBuilder() .setQualifier(simpleCell3.getQualifier()) .addCells(asProtoCell(simpleCell3)) .build())) .build(); Assert.assertEquals(expectedRow, FlatRowConverter.convert(simpleRow)); }