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