Search in sources :

Example 31 with Row

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());
    }
}
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 32 with Row

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

Example 33 with Row

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

Example 34 with Row

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

Example 35 with Row

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);
}
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)

Aggregations

Row (com.google.cloud.bigtable.data.v2.models.Row)71 Test (org.junit.Test)64 Query (com.google.cloud.bigtable.data.v2.models.Query)34 ByteString (com.google.protobuf.ByteString)28 ReadModifyWriteRow (com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow)26 BigtableDataClient (com.google.cloud.bigtable.data.v2.BigtableDataClient)19 RowCell (com.google.cloud.bigtable.data.v2.models.RowCell)19 ReadRowsRequest (com.google.bigtable.v2.ReadRowsRequest)13 Filter (com.google.cloud.bigtable.data.v2.models.Filters.Filter)11 IOException (java.io.IOException)10 RowMutation (com.google.cloud.bigtable.data.v2.models.RowMutation)9 ServerStreamingStashCallable (com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi.ServerStreamingStashCallable)7 Put (org.apache.hadoop.hbase.client.Put)7 DefaultRowAdapter (com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter)6 MutateRowRequest (com.google.bigtable.v2.MutateRowRequest)5 Mutation (com.google.bigtable.v2.Mutation)5 SetCell (com.google.bigtable.v2.Mutation.SetCell)5 NotFoundException (com.google.api.gax.rpc.NotFoundException)4 ReadRowsResponse (com.google.bigtable.v2.ReadRowsResponse)4 ConditionalRowMutation (com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation)4