Search in sources :

Example 11 with BigtableTableAdminClient

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;
}
Also used : ArrayList(java.util.ArrayList) CreateTableRequest(com.google.cloud.bigtable.admin.v2.models.CreateTableRequest)

Example 12 with BigtableTableAdminClient

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) {
    }
}
Also used : BigtableTableAdminSettings(com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings) Cluster(com.google.cloud.bigtable.admin.v2.models.Cluster) NotFoundException(com.google.api.gax.rpc.NotFoundException) BigtableTableAdminClient(com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient)

Example 13 with BigtableTableAdminClient

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);
}
Also used : BigtableTableAdminSettings(com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings) BigtableInstanceAdminClient(com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient) ByteString(com.google.protobuf.ByteString) BigtableTableAdminClient(com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) BigtableDataSettings(com.google.cloud.bigtable.data.v2.BigtableDataSettings) BigtableInstanceAdminSettings(com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings)

Example 14 with BigtableTableAdminClient

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);
    }
}
Also used : BigtableTableAdminClient(com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient) IOException(java.io.IOException) CreateTableRequest(com.google.cloud.bigtable.admin.v2.models.CreateTableRequest)

Aggregations

BigtableTableAdminClient (com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient)12 IOException (java.io.IOException)6 CreateTableRequest (com.google.cloud.bigtable.admin.v2.models.CreateTableRequest)5 BigtableTableAdminSettings (com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings)4 BigtableDataClient (com.google.cloud.bigtable.data.v2.BigtableDataClient)4 BeforeClass (org.junit.BeforeClass)4 ByteString (com.google.protobuf.ByteString)3 BigtableInstanceAdminClient (com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient)2 BulkMutation (com.google.cloud.bigtable.data.v2.models.BulkMutation)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 CreateBackupMessage (com.example.cloud.bigtable.scheduledbackups.resource.CreateBackupMessage)1 NotFoundException (com.google.api.gax.rpc.NotFoundException)1 BigtableInstanceAdminSettings (com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings)1 Backup (com.google.cloud.bigtable.admin.v2.models.Backup)1 Cluster (com.google.cloud.bigtable.admin.v2.models.Cluster)1 CreateBackupRequest (com.google.cloud.bigtable.admin.v2.models.CreateBackupRequest)1 CreateInstanceRequest (com.google.cloud.bigtable.admin.v2.models.CreateInstanceRequest)1 Instance (com.google.cloud.bigtable.admin.v2.models.Instance)1 RestoreTableRequest (com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest)1