use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.
the class WriteBatch method writeBatch.
public static void writeBatch(String projectId, String instanceId, String tableId) {
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
long timestamp = System.currentTimeMillis() * 1000;
BulkMutation bulkMutation = BulkMutation.create(tableId).add("tablet#a0b81f74#20190501", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), timestamp, 1).setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc1")).add("tablet#a0b81f74#20190502", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), timestamp, 1).setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6"));
dataClient.bulkMutateRows(bulkMutation);
System.out.print("Successfully wrote 2 rows");
} catch (Exception e) {
System.out.println("Error during WriteBatch: \n" + e.toString());
}
}
use of com.google.cloud.bigtable.data.v2.BigtableDataClient 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.BigtableDataClient in project java-bigtable by googleapis.
the class MobileTimeSeriesBaseTest method writeStatsData.
public static void writeStatsData() throws IOException {
try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
BulkMutation bulkMutation = BulkMutation.create(TABLE_ID).add("phone#4c410523#20190501", Mutation.create().setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP, 1).setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP, 1).setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP, "PQ2A.190405.003")).add("phone#4c410523#20190502", Mutation.create().setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP, 1).setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP, 1).setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP, "PQ2A.190405.004")).add("phone#4c410523#20190505", Mutation.create().setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP, 0).setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP, 1).setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP, "PQ2A.190406.000")).add("phone#5c10102#20190501", Mutation.create().setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP, 1).setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP, 1).setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP, "PQ2A.190401.002")).add("phone#5c10102#20190502", Mutation.create().setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP, 1).setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP, 0).setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP, "PQ2A.190406.000"));
dataClient.bulkMutateRows(bulkMutation);
} catch (IOException e) {
System.out.println("Error during writeTestData: \n" + e.toString());
throw (e);
}
}
use of com.google.cloud.bigtable.data.v2.BigtableDataClient 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);
}
use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.
the class BigtableBackupIT method createAndPopulateTestTable.
private static Table createAndPopulateTestTable(BigtableTableAdminClient tableAdmin, BigtableDataClient dataClient) throws InterruptedException {
String tableId = PrefixGenerator.newPrefix("BigtableBackupIT#createAndPopulateTestTable");
Table testTable = tableAdmin.createTable(CreateTableRequest.of(tableId).addFamily("cf1"));
// Populate test data.
byte[] rowBytes = new byte[1024];
Random random = new Random();
random.nextBytes(rowBytes);
try (Batcher<RowMutationEntry, Void> batcher = dataClient.newBulkMutationBatcher(tableId)) {
for (int i = 0; i < 10; i++) {
batcher.add(RowMutationEntry.create("test-row-" + i).setCell("cf1", ByteString.EMPTY, ByteString.copyFrom(rowBytes)));
}
}
return testTable;
}
Aggregations