Search in sources :

Example 11 with BigtableDataClient

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());
    }
}
Also used : BulkMutation(com.google.cloud.bigtable.data.v2.models.BulkMutation) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient)

Example 12 with BigtableDataClient

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());
    }
}
Also used : Row(com.google.cloud.bigtable.data.v2.models.Row) ReadModifyWriteRow(com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) ReadModifyWriteRow(com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow)

Example 13 with BigtableDataClient

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);
    }
}
Also used : BulkMutation(com.google.cloud.bigtable.data.v2.models.BulkMutation) IOException(java.io.IOException) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient)

Example 14 with BigtableDataClient

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);
}
Also used : RowMutationEntry(com.google.cloud.bigtable.data.v2.models.RowMutationEntry) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Row(com.google.cloud.bigtable.data.v2.models.Row) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) Test(org.junit.Test)

Example 15 with BigtableDataClient

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;
}
Also used : Table(com.google.cloud.bigtable.admin.v2.models.Table) Random(java.util.Random) RowMutationEntry(com.google.cloud.bigtable.data.v2.models.RowMutationEntry) ByteString(com.google.protobuf.ByteString)

Aggregations

BigtableDataClient (com.google.cloud.bigtable.data.v2.BigtableDataClient)27 Row (com.google.cloud.bigtable.data.v2.models.Row)18 IOException (java.io.IOException)13 Query (com.google.cloud.bigtable.data.v2.models.Query)8 BulkMutation (com.google.cloud.bigtable.data.v2.models.BulkMutation)7 ByteString (com.google.protobuf.ByteString)7 Test (org.junit.Test)6 BigtableTableAdminClient (com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient)4 BigtableDataSettings (com.google.cloud.bigtable.data.v2.BigtableDataSettings)4 CreateTableRequest (com.google.cloud.bigtable.admin.v2.models.CreateTableRequest)3 RowCell (com.google.cloud.bigtable.data.v2.models.RowCell)3 RowMutation (com.google.cloud.bigtable.data.v2.models.RowMutation)3 RowMutationEntry (com.google.cloud.bigtable.data.v2.models.RowMutationEntry)3 BeforeClass (org.junit.BeforeClass)3 ApiFuture (com.google.api.core.ApiFuture)2 Filters (com.google.cloud.bigtable.data.v2.models.Filters)2 ReadModifyWriteRow (com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow)2 ArrayList (java.util.ArrayList)2 BatcherImpl (com.google.api.gax.batching.BatcherImpl)1 FlowControlEventStats (com.google.api.gax.batching.FlowControlEventStats)1