Search in sources :

Example 1 with Table

use of com.google.cloud.bigtable.admin.v2.models.Table in project java-docs-samples by GoogleCloudPlatform.

the class CreateBackupTestIT method setUp.

@BeforeClass
public static void setUp() throws IOException {
    projectId = requireEnv(PROJECT_ENV);
    try (BigtableInstanceAdminClient instanceAdmin = BigtableInstanceAdminClient.create(projectId)) {
        CreateInstanceRequest request = CreateInstanceRequest.of(INSTANCE_ID).addCluster(CLUSTER_ID, ZONE_ID, 1, StorageType.SSD);
        Instance instance = instanceAdmin.createInstance(request);
    } catch (IOException e) {
        System.out.println("Error during BeforeClass while creating instance: \n" + e.toString());
        throw (e);
    }
    try (BigtableTableAdminClient tableAdmin = BigtableTableAdminClient.create(projectId, INSTANCE_ID)) {
        // Create a table.
        tableAdmin.createTable(CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_NAME));
    } catch (IOException e) {
        System.out.println("Error during BeforeClass while creating table: \n" + e.toString());
        throw (e);
    }
    // Get the sample's base directory (the one containing a pom.xml file)
    String baseDir = System.getProperty("basedir");
    // Emulate the function locally by running the Functions Framework Maven plugin
    emulatorProcess = new ProcessBuilder().command("mvn", "function:run").directory(new File(baseDir)).start();
}
Also used : Instance(com.google.cloud.bigtable.admin.v2.models.Instance) BigtableInstanceAdminClient(com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient) IOException(java.io.IOException) BigtableTableAdminClient(com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient) CreateInstanceRequest(com.google.cloud.bigtable.admin.v2.models.CreateInstanceRequest) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 2 with Table

use of com.google.cloud.bigtable.admin.v2.models.Table in project java-bigtable by googleapis.

the class BigtableTableAdminClient method listTablesAsync.

/**
 * Asynchronously lists all table IDs in the instance.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * ApiFuture<List<String>> listFuture = client.listTables();
 *
 * ApiFutures.addCallback(
 *   listFuture,
 *   new ApiFutureCallback<List<String>>() {
 *     public void onSuccess(List<String> tableIds) {
 *       System.out.println("Got list of tables:");
 *       for (String tableId : tableIds) {
 *         System.out.println(tableId);
 *       }
 *     }
 *
 *     public void onFailure(Throwable t) {
 *       t.printStackTrace();
 *     }
 *   },
 *   MoreExecutors.directExecutor()
 * );
 * }</pre>
 */
public ApiFuture<List<String>> listTablesAsync() {
    ListTablesRequest request = ListTablesRequest.newBuilder().setParent(NameUtil.formatInstanceName(projectId, instanceId)).build();
    // TODO(igorbernstein2): try to upstream pagination spooling or figure out a way to expose the
    // paginated responses while maintaining the wrapper facade.
    // Fetches the first page.
    ApiFuture<ListTablesPage> firstPageFuture = ApiFutures.transform(stub.listTablesPagedCallable().futureCall(request), new ApiFunction<ListTablesPagedResponse, ListTablesPage>() {

        @Override
        public ListTablesPage apply(ListTablesPagedResponse response) {
            return response.getPage();
        }
    }, MoreExecutors.directExecutor());
    // Fetches the rest of the pages by chaining the futures.
    ApiFuture<List<com.google.bigtable.admin.v2.Table>> allProtos = ApiFutures.transformAsync(firstPageFuture, new ApiAsyncFunction<ListTablesPage, List<com.google.bigtable.admin.v2.Table>>() {

        List<com.google.bigtable.admin.v2.Table> responseAccumulator = Lists.newArrayList();

        @Override
        public ApiFuture<List<com.google.bigtable.admin.v2.Table>> apply(ListTablesPage page) {
            // Add all entries from the page
            responseAccumulator.addAll(Lists.newArrayList(page.getValues()));
            // If this is the last page, just return the accumulated responses.
            if (!page.hasNextPage()) {
                return ApiFutures.immediateFuture(responseAccumulator);
            }
            // Otherwise fetch the next page.
            return ApiFutures.transformAsync(page.getNextPageAsync(), this, MoreExecutors.directExecutor());
        }
    }, MoreExecutors.directExecutor());
    // Wraps all of the accumulated protos.
    return ApiFutures.transform(allProtos, new ApiFunction<List<com.google.bigtable.admin.v2.Table>, List<String>>() {

        @Override
        public List<String> apply(List<com.google.bigtable.admin.v2.Table> protos) {
            List<String> results = Lists.newArrayListWithCapacity(protos.size());
            for (com.google.bigtable.admin.v2.Table proto : protos) {
                results.add(NameUtil.extractTableIdFromTableName(proto.getName()));
            }
            return results;
        }
    }, MoreExecutors.directExecutor());
}
Also used : ListTablesPage(com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage) Table(com.google.cloud.bigtable.admin.v2.models.Table) ListTablesPagedResponse(com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse) ApiFuture(com.google.api.core.ApiFuture) ListTablesRequest(com.google.bigtable.admin.v2.ListTablesRequest) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 3 with Table

use of com.google.cloud.bigtable.admin.v2.models.Table in project java-bigtable by googleapis.

the class HelloWorld method createTable.

/**
 * Demonstrates how to create a table.
 */
public void createTable() {
    // Checks if table exists, creates table if does not exist.
    if (!adminClient.exists(tableId)) {
        System.out.println("Creating table: " + tableId);
        CreateTableRequest createTableRequest = CreateTableRequest.of(tableId).addFamily(COLUMN_FAMILY);
        adminClient.createTable(createTableRequest);
        System.out.printf("Table %s created successfully%n", tableId);
    }
// [END bigtable_hw_create_table]
}
Also used : CreateTableRequest(com.google.cloud.bigtable.admin.v2.models.CreateTableRequest)

Example 4 with Table

use of com.google.cloud.bigtable.admin.v2.models.Table in project java-bigtable by googleapis.

the class TableAdminExample method getTableMeta.

/**
 * Demonstrates how to get a table's metadata.
 */
public void getTableMeta() {
    System.out.println("\nPrinting table metadata");
    // Gets table metadata, and applies a view to the table fields.
    try {
        Table table = adminClient.getTable(tableId);
        System.out.println("Table: " + table.getId());
        Collection<ColumnFamily> columnFamilies = table.getColumnFamilies();
        for (ColumnFamily columnFamily : columnFamilies) {
            System.out.printf("Column family: %s%nGC Rule: %s%n", columnFamily.getId(), columnFamily.getGCRule().toString());
        }
    } catch (NotFoundException e) {
        System.err.println("Failed to retrieve table metadata for a non-existent table: " + e.getMessage());
    }
// [END bigtable_get_table_metadata]
}
Also used : Table(com.google.cloud.bigtable.admin.v2.models.Table) NotFoundException(com.google.api.gax.rpc.NotFoundException) ColumnFamily(com.google.cloud.bigtable.admin.v2.models.ColumnFamily)

Example 5 with Table

use of com.google.cloud.bigtable.admin.v2.models.Table in project java-bigtable by googleapis.

the class TableAdminExample method createTable.

/**
 * Demonstrates how to create a table with the specified configuration.
 */
public void createTable() {
    // Checks if table exists, creates table if does not exist.
    if (!adminClient.exists(tableId)) {
        System.out.println("Table does not exist, creating table: " + tableId);
        CreateTableRequest createTableRequest = CreateTableRequest.of(tableId).addFamily("cf");
        Table table = adminClient.createTable(createTableRequest);
        System.out.printf("Table: %s created successfully%n", table.getId());
    }
// [END bigtable_create_table]
}
Also used : Table(com.google.cloud.bigtable.admin.v2.models.Table) CreateTableRequest(com.google.cloud.bigtable.admin.v2.models.CreateTableRequest)

Aggregations

Test (org.junit.Test)17 Table (com.google.cloud.bigtable.admin.v2.models.Table)16 ByteString (com.google.protobuf.ByteString)9 ColumnFamily (com.google.cloud.bigtable.admin.v2.models.ColumnFamily)5 CreateTableRequest (com.google.cloud.bigtable.admin.v2.models.CreateTableRequest)5 ListTablesRequest (com.google.bigtable.admin.v2.ListTablesRequest)4 Table (com.google.bigtable.admin.v2.Table)4 ListTablesPagedResponse (com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPagedResponse)4 BigtableTableAdminClient (com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient)4 NotFoundException (com.google.api.gax.rpc.NotFoundException)3 ModifyColumnFamiliesRequest (com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest)3 ColumnFamily (com.google.bigtable.admin.v2.ColumnFamily)2 ListTablesResponse (com.google.bigtable.admin.v2.ListTablesResponse)2 ListTablesPage (com.google.cloud.bigtable.admin.v2.BaseBigtableTableAdminClient.ListTablesPage)2 BigtableInstanceAdminClient (com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient)2 BigtableTableAdminSettings (com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings)2 Instance (com.google.cloud.bigtable.admin.v2.models.Instance)2 RestoreTableRequest (com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest)2 RestoredTableResult (com.google.cloud.bigtable.admin.v2.models.RestoredTableResult)2 AbstractMessage (com.google.protobuf.AbstractMessage)2