Search in sources :

Example 1 with InstanceName

use of com.google.bigtable.admin.v2.InstanceName in project beam by apache.

the class BigtableClientWrapper method createTable.

void createTable(String tableName, String familyName) {
    Table.Builder tableBuilder = Table.newBuilder();
    tableBuilder.putColumnFamilies(familyName, ColumnFamily.newBuilder().build());
    String instanceName = bigtableOptions.getInstanceName().toString();
    com.google.bigtable.admin.v2.CreateTableRequest.Builder createTableRequestBuilder = com.google.bigtable.admin.v2.CreateTableRequest.newBuilder().setParent(instanceName).setTableId(tableName).setTable(tableBuilder.build());
    tableAdminClient.createTable(createTableRequestBuilder.build());
}
Also used : Table(com.google.bigtable.admin.v2.Table) RowUtils.byteString(org.apache.beam.sdk.io.gcp.bigtable.RowUtils.byteString)

Example 2 with InstanceName

use of com.google.bigtable.admin.v2.InstanceName in project beam by apache.

the class BigtableWriteIT method testE2EBigtableWrite.

@Test
public void testE2EBigtableWrite() throws Exception {
    final String tableName = bigtableOptions.getInstanceName().toTableNameStr(tableId);
    final String instanceName = bigtableOptions.getInstanceName().toString();
    final int numRows = 1000;
    final List<KV<ByteString, ByteString>> testData = generateTableData(numRows);
    createEmptyTable(instanceName, tableId);
    Pipeline p = Pipeline.create(options);
    p.apply(GenerateSequence.from(0).to(numRows)).apply(ParDo.of(new DoFn<Long, KV<ByteString, Iterable<Mutation>>>() {

        @ProcessElement
        public void processElement(ProcessContext c) {
            int index = c.element().intValue();
            Iterable<Mutation> mutations = ImmutableList.of(Mutation.newBuilder().setSetCell(Mutation.SetCell.newBuilder().setValue(testData.get(index).getValue()).setFamilyName(COLUMN_FAMILY_NAME)).build());
            c.output(KV.of(testData.get(index).getKey(), mutations));
        }
    })).apply(BigtableIO.write().withBigtableOptions(bigtableOptions).withTableId(tableId));
    p.run();
    // Test number of column families and column family name equality
    Table table = getTable(tableName);
    assertThat(table.getColumnFamiliesMap().keySet(), Matchers.hasSize(1));
    assertThat(table.getColumnFamiliesMap(), Matchers.hasKey(COLUMN_FAMILY_NAME));
    // Test table data equality
    List<KV<ByteString, ByteString>> tableData = getTableData(tableName);
    assertThat(tableData, Matchers.containsInAnyOrder(testData.toArray()));
}
Also used : Table(com.google.bigtable.admin.v2.Table) ByteString(com.google.protobuf.ByteString) KV(org.apache.beam.sdk.values.KV) Mutation(com.google.bigtable.v2.Mutation) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) Pipeline(org.apache.beam.sdk.Pipeline) Test(org.junit.Test)

Example 3 with InstanceName

use of com.google.bigtable.admin.v2.InstanceName in project java-bigtable by googleapis.

the class BigtableInstanceAdminClient method listAppProfilesAsync.

/**
 * Asynchronously lists all app profiles of the specified instance.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * ApiFuture<List<AppProfile>> appProfilesFuture = client.listAppProfilesAsync("my-instance");
 *
 * List<AppProfile> appProfiles = appProfileFuture.get();
 * }</pre>
 *
 * @see AppProfile
 */
@SuppressWarnings("WeakerAccess")
public ApiFuture<List<AppProfile>> listAppProfilesAsync(String instanceId) {
    String instanceName = NameUtil.formatInstanceName(projectId, instanceId);
    ListAppProfilesRequest request = ListAppProfilesRequest.newBuilder().setParent(instanceName).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<ListAppProfilesPage> firstPageFuture = ApiFutures.transform(stub.listAppProfilesPagedCallable().futureCall(request), new ApiFunction<ListAppProfilesPagedResponse, ListAppProfilesPage>() {

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

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

        @Override
        public ApiFuture<List<com.google.bigtable.admin.v2.AppProfile>> apply(ListAppProfilesPage 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.AppProfile>, List<AppProfile>>() {

        @Override
        public List<AppProfile> apply(List<com.google.bigtable.admin.v2.AppProfile> input) {
            List<AppProfile> results = Lists.newArrayListWithCapacity(input.size());
            for (com.google.bigtable.admin.v2.AppProfile appProfile : input) {
                results.add(AppProfile.fromProto(appProfile));
            }
            return results;
        }
    }, MoreExecutors.directExecutor());
}
Also used : AppProfile(com.google.cloud.bigtable.admin.v2.models.AppProfile) ListAppProfilesRequest(com.google.bigtable.admin.v2.ListAppProfilesRequest) ApiFuture(com.google.api.core.ApiFuture) ListAppProfilesPage(com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPage) ListAppProfilesPagedResponse(com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPagedResponse) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 4 with InstanceName

use of com.google.bigtable.admin.v2.InstanceName in project java-bigtable by googleapis.

the class BaseBigtableTableAdminClientTest method createTableExceptionTest.

@Test
public void createTableExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockBigtableTableAdmin.addException(exception);
    try {
        InstanceName parent = InstanceName.of("[PROJECT]", "[INSTANCE]");
        String tableId = "tableId-1552905847";
        Table table = Table.newBuilder().build();
        client.createTable(parent, tableId, table);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InstanceName(com.google.bigtable.admin.v2.InstanceName) Table(com.google.bigtable.admin.v2.Table) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 5 with InstanceName

use of com.google.bigtable.admin.v2.InstanceName in project java-bigtable by googleapis.

the class BaseBigtableTableAdminClientTest method createTableFromSnapshotTest.

@Test
public void createTableFromSnapshotTest() throws Exception {
    Table expectedResponse = Table.newBuilder().setName(TableName.of("[PROJECT]", "[INSTANCE]", "[TABLE]").toString()).putAllClusterStates(new HashMap<String, Table.ClusterState>()).putAllColumnFamilies(new HashMap<String, ColumnFamily>()).setRestoreInfo(RestoreInfo.newBuilder().build()).build();
    Operation resultOperation = Operation.newBuilder().setName("createTableFromSnapshotTest").setDone(true).setResponse(Any.pack(expectedResponse)).build();
    mockBigtableTableAdmin.addResponse(resultOperation);
    InstanceName parent = InstanceName.of("[PROJECT]", "[INSTANCE]");
    String tableId = "tableId-1552905847";
    SnapshotName sourceSnapshot = SnapshotName.of("[PROJECT]", "[INSTANCE]", "[CLUSTER]", "[SNAPSHOT]");
    Table actualResponse = client.createTableFromSnapshotAsync(parent, tableId, sourceSnapshot).get();
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockBigtableTableAdmin.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateTableFromSnapshotRequest actualRequest = ((CreateTableFromSnapshotRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertEquals(tableId, actualRequest.getTableId());
    Assert.assertEquals(sourceSnapshot.toString(), actualRequest.getSourceSnapshot());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : InstanceName(com.google.bigtable.admin.v2.InstanceName) Table(com.google.bigtable.admin.v2.Table) AbstractMessage(com.google.protobuf.AbstractMessage) HashMap(java.util.HashMap) CreateTableFromSnapshotRequest(com.google.bigtable.admin.v2.CreateTableFromSnapshotRequest) ByteString(com.google.protobuf.ByteString) Operation(com.google.longrunning.Operation) SnapshotName(com.google.bigtable.admin.v2.SnapshotName) ColumnFamily(com.google.bigtable.admin.v2.ColumnFamily) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)21 InstanceName (com.google.bigtable.admin.v2.InstanceName)20 ByteString (com.google.protobuf.ByteString)13 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)10 AbstractMessage (com.google.protobuf.AbstractMessage)10 StatusRuntimeException (io.grpc.StatusRuntimeException)10 Table (com.google.bigtable.admin.v2.Table)7 AppProfile (com.google.bigtable.admin.v2.AppProfile)3 ColumnFamily (com.google.bigtable.admin.v2.ColumnFamily)3 Operation (com.google.longrunning.Operation)3 HashMap (java.util.HashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3 Cluster (com.google.bigtable.admin.v2.Cluster)2 CreateTableFromSnapshotRequest (com.google.bigtable.admin.v2.CreateTableFromSnapshotRequest)2 DeleteInstanceRequest (com.google.bigtable.admin.v2.DeleteInstanceRequest)2 ListAppProfilesRequest (com.google.bigtable.admin.v2.ListAppProfilesRequest)2 SnapshotName (com.google.bigtable.admin.v2.SnapshotName)2 ListAppProfilesPagedResponse (com.google.cloud.bigtable.admin.v2.BaseBigtableInstanceAdminClient.ListAppProfilesPagedResponse)2 ApiFuture (com.google.api.core.ApiFuture)1 CreateAppProfileRequest (com.google.bigtable.admin.v2.CreateAppProfileRequest)1