Search in sources :

Example 6 with BigtableDataClient

use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.

the class WriteConditionally method writeConditionally.

public static void writeConditionally(String projectId, String instanceId, String tableId) {
    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
        long timestamp = System.currentTimeMillis() * 1000;
        String rowkey = "phone#4c410523#20190501";
        Mutation mutation = Mutation.create().setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "android");
        Filter filter = FILTERS.chain().filter(FILTERS.family().exactMatch(COLUMN_FAMILY_NAME)).filter(FILTERS.qualifier().exactMatch("os_build")).filter(FILTERS.value().regex("PQ2A\\..*"));
        ConditionalRowMutation conditionalRowMutation = ConditionalRowMutation.create(tableId, rowkey).condition(filter).then(mutation);
        boolean success = dataClient.checkAndMutateRow(conditionalRowMutation);
        System.out.printf("Successfully updated row's os_name: %b", success);
    } catch (Exception e) {
        System.out.println("Error during WriteConditionally: \n" + e.toString());
        e.printStackTrace();
    }
}
Also used : ConditionalRowMutation(com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation) Filter(com.google.cloud.bigtable.data.v2.models.Filters.Filter) Mutation(com.google.cloud.bigtable.data.v2.models.Mutation) ConditionalRowMutation(com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient)

Example 7 with BigtableDataClient

use of com.google.cloud.bigtable.data.v2.BigtableDataClient 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 8 with BigtableDataClient

use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.

the class Reads method readRowRanges.

public static void readRowRanges(String projectId, String instanceId, String tableId) {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
        Query query = Query.create(tableId).range("phone#4c410523#20190501", "phone#4c410523#20190601").range("phone#5c10102#20190501", "phone#5c10102#20190601");
        ServerStream<Row> rows = dataClient.readRows(query);
        for (Row row : rows) {
            printRow(row);
        }
    } catch (IOException e) {
        System.out.println("Unable to initialize service client, as a network error occurred: \n" + e.toString());
    }
}
Also used : Query(com.google.cloud.bigtable.data.v2.models.Query) Row(com.google.cloud.bigtable.data.v2.models.Row) IOException(java.io.IOException) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient)

Example 9 with BigtableDataClient

use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.

the class Reads method readFilter.

public static void readFilter(String projectId, String instanceId, String tableId) {
    Filters.Filter filter = FILTERS.value().regex("PQ2A.*");
    // the "close" method on the client to safely clean up any remaining background resources.
    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
        Query query = Query.create(tableId).filter(filter);
        ServerStream<Row> rows = dataClient.readRows(query);
        for (Row row : rows) {
            printRow(row);
        }
    } catch (IOException e) {
        System.out.println("Unable to initialize service client, as a network error occurred: \n" + e.toString());
    }
}
Also used : Filters(com.google.cloud.bigtable.data.v2.models.Filters) Query(com.google.cloud.bigtable.data.v2.models.Query) Row(com.google.cloud.bigtable.data.v2.models.Row) IOException(java.io.IOException) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient)

Example 10 with BigtableDataClient

use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.

the class Reads method readRow.

public static void readRow(String projectId, String instanceId, String tableId) {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
        String rowkey = "phone#4c410523#20190501";
        Row row = dataClient.readRow(tableId, rowkey);
        printRow(row);
    } catch (IOException e) {
        System.out.println("Unable to initialize service client, as a network error occurred: \n" + e.toString());
    }
}
Also used : Row(com.google.cloud.bigtable.data.v2.models.Row) IOException(java.io.IOException) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient)

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