use of com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient in project java-docs-samples by GoogleCloudPlatform.
the class BulkWrite method getNewTableIds.
// Increases or decreases the number of tables in the Bigtable instance based on the expected size
// and returns any newly created table ids.
private static List<String> getNewTableIds(BigtableTableAdminClient adminClient, double expectedSize) {
List<String> tableIds = adminClient.listTables();
List<String> newTableIds = new ArrayList<>();
double currentSize = tableIds.size() * TB_PER_TABLE;
if (currentSize >= expectedSize) {
int numTablesToDelete = (int) ((currentSize - expectedSize) / .5);
for (int i = 0; i < numTablesToDelete; i++) {
adminClient.deleteTable(tableIds.get(i));
}
System.out.printf("Deleted %d tables%n", numTablesToDelete);
} else {
int numTablesToCreate = (int) ((expectedSize - currentSize) / .5);
System.out.printf("Creating %d tables%n", numTablesToCreate);
for (int i = 0; i < numTablesToCreate; i++) {
String tableId = TABLE_PREFIX + UUID.randomUUID().toString().substring(0, 20);
CreateTableRequest createTableRequest = CreateTableRequest.of(tableId).addFamily(COLUMN_FAMILY);
adminClient.createTable(createTableRequest);
newTableIds.add(tableId);
System.out.println(tableId);
}
}
return newTableIds;
}
use of com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient in project java-bigtable by googleapis.
the class TestEnvRule method deleteInstance.
/**
* Delete an instance with all of its resources.
*/
private void deleteInstance(String instanceId) throws IOException, ExecutionException, InterruptedException {
BigtableTableAdminSettings settings = env().getTableAdminSettings().toBuilder().setInstanceId(instanceId).build();
// Delete all child resources (backups & clusters) that wont be automatically deleted
try (BigtableTableAdminClient tableAdmin = BigtableTableAdminClient.create(settings)) {
List<Cluster> clusters = env().getInstanceAdminClient().listClusters(instanceId);
boolean isFirstCluster = true;
for (Cluster cluster : clusters) {
deleteBackups(tableAdmin, cluster.getId());
// without clusters)
if (!isFirstCluster) {
try {
env().getInstanceAdminClient().deleteCluster(instanceId, cluster.getId());
} catch (NotFoundException ignored) {
}
}
isFirstCluster = false;
}
}
// Delete everything else
try {
env().getInstanceAdminClient().deleteInstance(instanceId);
} catch (NotFoundException ignored) {
}
}
use of com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient 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.admin.v2.BigtableTableAdminClient in project java-bigtable by googleapis.
the class MobileTimeSeriesBaseTest method createTable.
public static void createTable() throws IOException {
try (BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(projectId, instanceId)) {
CreateTableRequest createTableRequest = CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_NAME_STATS).addFamily(COLUMN_FAMILY_NAME_PLAN);
adminClient.createTable(createTableRequest);
} catch (IOException e) {
System.out.println("Error during createTable: \n" + e.toString());
throw (e);
}
}
Aggregations