Search in sources :

Example 16 with BigtableDataClient

use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-docs-samples by GoogleCloudPlatform.

the class FiltersTest method beforeClass.

@BeforeClass
public static void beforeClass() throws IOException {
    projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
    instanceId = requireEnv(INSTANCE_ENV);
    try (BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(projectId, instanceId)) {
        CreateTableRequest createTableRequest = CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_NAME_STATS).addFamily(COLUMN_FAMILY_NAME_DATA);
        adminClient.createTable(createTableRequest);
        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_NANO, 1).setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP_NANO, "PQ2A.190405.003").setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_01gb", TIMESTAMP_MINUS_HR_NANO, "true").setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_01gb", TIMESTAMP_NANO, "false").setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_05gb", TIMESTAMP_NANO, "true")).add("phone#4c410523#20190502", Mutation.create().setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP_NANO, "PQ2A.190405.004").setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_05gb", TIMESTAMP_NANO, "true")).add("phone#4c410523#20190505", Mutation.create().setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 0).setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP_NANO, "PQ2A.190406.000").setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_05gb", TIMESTAMP_NANO, "true")).add("phone#5c10102#20190501", Mutation.create().setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP_NANO, "PQ2A.190401.002").setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_10gb", TIMESTAMP_NANO, "true")).add("phone#5c10102#20190502", Mutation.create().setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME_STATS, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 0).setCell(COLUMN_FAMILY_NAME_STATS, "os_build", TIMESTAMP_NANO, "PQ2A.190406.000").setCell(COLUMN_FAMILY_NAME_DATA, "data_plan_10gb", TIMESTAMP_NANO, "true"));
            dataClient.bulkMutateRows(bulkMutation);
        }
    } catch (Exception e) {
        System.out.println("Error during beforeClass: \n" + e.toString());
        throw (e);
    }
}
Also used : BulkMutation(com.google.cloud.bigtable.data.v2.models.BulkMutation) BigtableTableAdminClient(com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) CreateTableRequest(com.google.cloud.bigtable.admin.v2.models.CreateTableRequest) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 17 with BigtableDataClient

use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-docs-samples by GoogleCloudPlatform.

the class Memcached method memcachedBigtable.

public static void memcachedBigtable(String projectId, String instanceId, String tableId, String discoveryEndpoint) {
    try {
        MemcachedClient mcc = new MemcachedClient(new InetSocketAddress(discoveryEndpoint, 11211));
        System.out.println("Connected to Memcached successfully");
        // Get value from cache
        String rowkey = "phone#4c410523#20190501";
        String columnFamily = "stats_summary";
        String column = "os_build";
        String cacheKey = String.format("%s:%s:%s", rowkey, columnFamily, column);
        Object value = mcc.get(cacheKey);
        if (value != null) {
            System.out.println("Value fetched from cache: " + value);
        } else {
            System.out.println("didn't get value from cache");
            // Get data from Bigtable source and add to cache for 30 minutes.
            try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
                Row row = dataClient.readRow(tableId, rowkey);
                String cellValue = row.getCells(columnFamily, column).get(0).getValue().toStringUtf8();
                System.out.println("got data from bt " + cellValue);
                // Set data into memcached server.
                mcc.set(cacheKey, 30 * 60, cellValue);
                System.out.println("Value fetched from Bigtable: " + cellValue);
            } catch (Exception e) {
                System.out.println("Could not set cache value.");
                e.printStackTrace();
            }
        }
        mcc.shutdown();
    } catch (Exception e) {
        System.out.println("Could not get cache value.");
        e.printStackTrace();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) MemcachedClient(net.spy.memcached.MemcachedClient) Row(com.google.cloud.bigtable.data.v2.models.Row) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient)

Example 18 with BigtableDataClient

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

the class Filters method readFilter.

// [END bigtable_filters_composing_condition]
// [END_EXCLUDE]
private static void readFilter(String projectId, String instanceId, String tableId, Filter filter) {
    // 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 : 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 19 with BigtableDataClient

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

the class Reads method readRows.

public static void readRows(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).rowKey("phone#4c410523#20190501").rowKey("phone#4c410523#20190502");
        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 20 with BigtableDataClient

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

the class Reads method readRowPartial.

public static void readRowPartial(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";
        Filters.Filter filter = FILTERS.chain().filter(FILTERS.family().exactMatch("stats_summary")).filter(FILTERS.qualifier().exactMatch("os_build"));
        Row row = dataClient.readRow(tableId, rowkey, filter);
        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) 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