use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.
the class Reads method readRowRange.
public static void readRowRange(String projectId, String instanceId, String tableId) {
String start = "phone#4c410523#20190501";
String end = "phone#4c410523#201906201";
// 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(start, end);
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 NativeImageBigtableSample method insertData.
public static void insertData(BigtableDataClient client, String tableId, long timestamp, ImmutableMap<String, Long> dataWithLong, ImmutableMap<String, String> dataWithStrings) {
String rowKey = String.format("phone#%d", timestamp);
RowMutation rowMutation = RowMutation.create(tableId, rowKey);
for (Entry<String, Long> longEntry : dataWithLong.entrySet()) {
rowMutation.setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom(longEntry.getKey().getBytes()), timestamp, longEntry.getValue());
}
for (Entry<String, String> stringEntry : dataWithStrings.entrySet()) {
rowMutation.setCell(COLUMN_FAMILY_NAME, stringEntry.getKey(), timestamp, stringEntry.getValue());
}
client.mutateRow(rowMutation);
System.out.println("Successfully wrote row: " + rowKey);
}
use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.
the class NativeImageBigtableSample method readData.
static void readData(BigtableDataClient client, String tableId) {
Query query = Query.create(tableId).prefix("");
ServerStream<Row> rows = client.readRows(query);
System.out.println("Reading phone data in table:");
for (Row row : rows) {
System.out.println("Key: " + row.getKey().toStringUtf8());
for (RowCell cell : row.getCells()) {
System.out.printf("\t%s: %s @%s\n", cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8(), cell.getTimestamp());
}
System.out.println();
}
}
use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.
the class NativeImageBigtableSample method main.
/**
* Entrypoint to the BigTable sample application.
*/
public static void main(String[] args) throws IOException {
String projectId = ServiceOptions.getDefaultProjectId();
BigtableTableAdminSettings adminClientSettings = BigtableTableAdminSettings.newBuilder().setInstanceId(INSTANCE_NAME).setProjectId(projectId).build();
BigtableDataSettings clientSettings = BigtableDataSettings.newBuilder().setInstanceId(INSTANCE_NAME).setProjectId(projectId).build();
BigtableInstanceAdminSettings instanceAdminSettings = BigtableInstanceAdminSettings.newBuilder().setProjectId(projectId).build();
BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(adminClientSettings);
BigtableDataClient standardClient = BigtableDataClient.create(clientSettings);
BigtableInstanceAdminClient instanceAdminClient = BigtableInstanceAdminClient.create(instanceAdminSettings);
if (!instanceAdminClient.exists(INSTANCE_NAME)) {
instanceAdminClient.createInstance(CreateInstanceRequest.of(INSTANCE_NAME).addCluster("cluster", "us-central1-f", 3, StorageType.SSD).setType(Instance.Type.PRODUCTION).addLabel("example", "instance_admin"));
}
String tableName = TABLE_NAME + UUID.randomUUID().toString().replace("-", "");
createTable(adminClient, tableName);
// Add data into table
ImmutableMap<String, Long> dataWithLong = ImmutableMap.of("connected_cell", 1L, "connected_wifi", 1L);
ImmutableMap<String, String> dataWithStrings = ImmutableMap.of("os_build", "PQ2A.190405.003");
long timestamp = System.currentTimeMillis() * 1000;
insertData(standardClient, tableName, timestamp, dataWithLong, dataWithStrings);
readData(standardClient, tableName);
// Clean up
deleteTable(adminClient, tableName);
}
use of com.google.cloud.bigtable.data.v2.BigtableDataClient in project java-bigtable by googleapis.
the class ConfigureConnectionPool method configureConnectionPool.
public static void configureConnectionPool(String projectId, String instanceId) {
// String projectId = "my-project-id";
// String instanceId = "my-instance-id";
BigtableDataSettings.Builder settingsBuilder = BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId);
settingsBuilder.stubSettings().setTransportChannelProvider(EnhancedBigtableStubSettings.defaultGrpcTransportProviderBuilder().setPoolSize(10).build());
BigtableDataSettings settings = settingsBuilder.build();
try (BigtableDataClient dataClient = BigtableDataClient.create(settings)) {
InstantiatingGrpcChannelProvider provider = (InstantiatingGrpcChannelProvider) settings.getStubSettings().getTransportChannelProvider();
int poolSize = provider.toBuilder().getPoolSize();
System.out.println(String.format("Connected with pool size of %d", poolSize));
} catch (IOException e) {
System.out.println("Error during ConfigureConnectionPool: \n" + e.toString());
}
}
Aggregations