use of com.google.cloud.bigtable.data.v2.models.Row in project java-bigtable by googleapis.
the class WriteIncrement method writeIncrement.
public static void writeIncrement(String projectId, String instanceId, String tableId) {
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
// Get an existing row that has a cell with an incrementable value. A value can be incremented
// if it is encoded as a 64-bit big-endian signed integer.
String rowkey = "phone#4c410523#20190501";
ReadModifyWriteRow mutation = ReadModifyWriteRow.create(tableId, rowkey).increment(COLUMN_FAMILY_NAME, "connected_cell", -1);
Row success = dataClient.readModifyWriteRow(mutation);
System.out.printf("Successfully updated row %s", success.getKey().toString(Charset.defaultCharset()));
} catch (Exception e) {
System.out.println("Error during WriteIncrement: \n" + e.toString());
}
}
use of com.google.cloud.bigtable.data.v2.models.Row in project java-bigtable by googleapis.
the class WriteSimple method writeSimple.
public static void writeSimple(String projectId, String instanceId, String tableId) {
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
long timestamp = System.currentTimeMillis() * 1000;
String rowkey = "phone#4c410523#20190501";
RowMutation rowMutation = RowMutation.create(tableId, rowkey).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), timestamp, 1).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), timestamp, 1).setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "PQ2A.190405.003");
dataClient.mutateRow(rowMutation);
System.out.printf("Successfully wrote row %s", rowkey);
} catch (Exception e) {
System.out.println("Error during WriteSimple: \n" + e.toString());
}
}
use of com.google.cloud.bigtable.data.v2.models.Row in project java-bigtable by googleapis.
the class BulkReadIT method testBulkRead.
@Test
public void testBulkRead() throws InterruptedException, ExecutionException {
BigtableDataClient client = testEnvRule.env().getDataClient();
String family = testEnvRule.env().getFamilyId();
String rowPrefix = UUID.randomUUID().toString();
int numRows = 10;
BulkMutation bulkMutation = BulkMutation.create(testEnvRule.env().getTableId());
List<Row> expectedRows = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
bulkMutation.add(RowMutationEntry.create(rowPrefix + "-" + i).setCell(family, "qualifier", 10_000L, "value-" + i));
expectedRows.add(Row.create(ByteString.copyFromUtf8(rowPrefix + "-" + i), ImmutableList.of(RowCell.create(family, ByteString.copyFromUtf8("qualifier"), 10_000L, ImmutableList.<String>of(), ByteString.copyFromUtf8("value-" + i)))));
}
client.bulkMutateRows(bulkMutation);
try (Batcher<ByteString, Row> batcher = client.newBulkReadRowsBatcher(testEnvRule.env().getTableId())) {
List<ApiFuture<Row>> rowFutures = new ArrayList<>(numRows);
for (int rowCount = 0; rowCount < numRows; rowCount++) {
ApiFuture<Row> entryResponse = batcher.add(ByteString.copyFromUtf8(rowPrefix + "-" + rowCount));
rowFutures.add(entryResponse);
}
batcher.flush();
List<Row> actualRows = ApiFutures.allAsList(rowFutures).get();
assertThat(actualRows).isEqualTo(expectedRows);
// To verify non-existent and duplicate row keys
rowFutures = new ArrayList<>();
// non-existent row key
rowFutures.add(batcher.add(ByteString.copyFromUtf8(UUID.randomUUID().toString())));
// duplicate row key
rowFutures.add(batcher.add(ByteString.copyFromUtf8(rowPrefix + "-" + 0)));
rowFutures.add(batcher.add(ByteString.copyFromUtf8(rowPrefix + "-" + 0)));
batcher.flush();
actualRows = ApiFutures.allAsList(rowFutures).get();
assertThat(actualRows.get(0)).isNull();
assertThat(actualRows.get(1)).isEqualTo(expectedRows.get(0));
assertThat(actualRows.get(2)).isEqualTo(expectedRows.get(0));
}
}
use of com.google.cloud.bigtable.data.v2.models.Row in project java-bigtable by googleapis.
the class MutateRowIT method test.
@Test
public void test() throws Exception {
String rowKey = UUID.randomUUID().toString();
String familyId = testEnvRule.env().getFamilyId();
testEnvRule.env().getDataClient().mutateRowAsync(RowMutation.create(testEnvRule.env().getTableId(), rowKey).setCell(familyId, "q", "myVal").setCell(familyId, "q2", "myVal2").setCell(familyId, "q3", "myVal3").setCell(familyId, "q4", 0x12345678)).get(1, TimeUnit.MINUTES);
testEnvRule.env().getDataClient().mutateRowAsync(RowMutation.create(testEnvRule.env().getTableId(), rowKey).deleteCells(familyId, "q2")).get(1, TimeUnit.MINUTES);
Row row = testEnvRule.env().getDataClient().readRowsCallable().first().call(Query.create(testEnvRule.env().getTableId()).rowKey(rowKey));
assertThat(row.getCells()).hasSize(3);
assertThat(row.getCells().get(0).getValue()).isEqualTo(ByteString.copyFromUtf8("myVal"));
assertThat(row.getCells().get(1).getValue()).isEqualTo(ByteString.copyFromUtf8("myVal3"));
assertThat(row.getCells().get(2).getValue()).isEqualTo(ByteString.copyFrom(new byte[] { 0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78 }));
}
use of com.google.cloud.bigtable.data.v2.models.Row in project java-bigtable by googleapis.
the class RowMutationEntryBatcherIT method testNewBatcher.
@Test
public void testNewBatcher() throws Exception {
BigtableDataClient client = testEnvRule.env().getDataClient();
String tableId = testEnvRule.env().getTableId();
String family = testEnvRule.env().getFamilyId();
String rowPrefix = UUID.randomUUID().toString();
try (Batcher<RowMutationEntry, Void> batcher = client.newBulkMutationBatcher(tableId)) {
for (int i = 0; i < 10; i++) {
batcher.add(RowMutationEntry.create(rowPrefix + "-" + i).setCell(family, "qualifier", 10_000L, "value-" + i));
}
}
List<Row> expectedRows = new ArrayList<>();
for (int i = 0; i < 10; i++) {
expectedRows.add(Row.create(ByteString.copyFromUtf8(rowPrefix + "-" + i), ImmutableList.of(RowCell.create(family, ByteString.copyFromUtf8("qualifier"), 10_000L, ImmutableList.<String>of(), ByteString.copyFromUtf8("value-" + i)))));
}
ServerStream<Row> actualRows = client.readRows(Query.create(tableId).prefix(rowPrefix));
assertThat(actualRows).containsExactlyElementsIn(expectedRows);
}
Aggregations