Search in sources :

Example 11 with Table

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

the class BigtableTableAdminClientIT method createTable.

@Test
public void createTable() {
    assume().withMessage("Emulator doesn't return proper responses for CreateTable").that(testEnvRule.env()).isNotInstanceOf(EmulatorEnv.class);
    CreateTableRequest createTableReq = CreateTableRequest.of(tableId).addFamily("cf1").addFamily("cf2", GCRULES.maxVersions(10)).addSplit(ByteString.copyFromUtf8("b")).addSplit(ByteString.copyFromUtf8("q"));
    Table tableResponse = tableAdmin.createTable(createTableReq);
    assertEquals(tableId, tableResponse.getId());
    Map<String, ColumnFamily> columnFamilyById = Maps.newHashMap();
    for (ColumnFamily columnFamily : tableResponse.getColumnFamilies()) {
        columnFamilyById.put(columnFamily.getId(), columnFamily);
    }
    assertEquals(2, tableResponse.getColumnFamilies().size());
    assertFalse(columnFamilyById.get("cf1").hasGCRule());
    assertTrue(columnFamilyById.get("cf2").hasGCRule());
    assertEquals(10, ((VersionRule) columnFamilyById.get("cf2").getGCRule()).getMaxVersions());
}
Also used : Table(com.google.cloud.bigtable.admin.v2.models.Table) ByteString(com.google.protobuf.ByteString) CreateTableRequest(com.google.cloud.bigtable.admin.v2.models.CreateTableRequest) ColumnFamily(com.google.cloud.bigtable.admin.v2.models.ColumnFamily) Test(org.junit.Test)

Example 12 with Table

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

the class BigtableTableAdminClientIT method modifyFamilies.

@Test
public void modifyFamilies() {
    tableAdmin.createTable(CreateTableRequest.of(tableId));
    ModifyColumnFamiliesRequest modifyFamiliesReq = ModifyColumnFamiliesRequest.of(tableId).addFamily("mf1").addFamily("mf2", GCRULES.maxAge(Duration.ofSeconds(1000, 20000))).updateFamily("mf1", GCRULES.union().rule(GCRULES.maxAge(Duration.ofSeconds(100))).rule(GCRULES.maxVersions(1))).addFamily("mf3", GCRULES.intersection().rule(GCRULES.maxAge(Duration.ofSeconds(2000))).rule(GCRULES.maxVersions(10))).addFamily("mf4", GCRULES.intersection().rule(GCRULES.maxAge(Duration.ofSeconds(360)))).addFamily("mf5").addFamily("mf6").dropFamily("mf5").dropFamily("mf6").addFamily("mf7");
    Table tableResponse = tableAdmin.modifyFamilies(modifyFamiliesReq);
    Map<String, ColumnFamily> columnFamilyById = Maps.newHashMap();
    for (ColumnFamily columnFamily : tableResponse.getColumnFamilies()) {
        columnFamilyById.put(columnFamily.getId(), columnFamily);
    }
    assertEquals(5, columnFamilyById.size());
    assertNotNull(columnFamilyById.get("mf1"));
    assertNotNull(columnFamilyById.get("mf2"));
    assertEquals(2, ((UnionRule) columnFamilyById.get("mf1").getGCRule()).getRulesList().size());
    assertEquals(1000, ((DurationRule) columnFamilyById.get("mf2").getGCRule()).getMaxAge().getSeconds());
    assertEquals(20000, ((DurationRule) columnFamilyById.get("mf2").getGCRule()).getMaxAge().getNano());
    assertEquals(2, ((IntersectionRule) columnFamilyById.get("mf3").getGCRule()).getRulesList().size());
    assertEquals(360, ((DurationRule) columnFamilyById.get("mf4").getGCRule()).getMaxAge().getSeconds());
    assertNotNull(columnFamilyById.get("mf7"));
}
Also used : Table(com.google.cloud.bigtable.admin.v2.models.Table) IntersectionRule(com.google.cloud.bigtable.admin.v2.models.GCRules.IntersectionRule) ByteString(com.google.protobuf.ByteString) UnionRule(com.google.cloud.bigtable.admin.v2.models.GCRules.UnionRule) DurationRule(com.google.cloud.bigtable.admin.v2.models.GCRules.DurationRule) ModifyColumnFamiliesRequest(com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest) ColumnFamily(com.google.cloud.bigtable.admin.v2.models.ColumnFamily) Test(org.junit.Test)

Example 13 with Table

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

the class BulkWrite method bulkWrite.

static void bulkWrite(BigtableOptions options) throws IOException, GeneralSecurityException {
    BigtableTableAdminSettings adminSettings = BigtableTableAdminSettings.newBuilder().setProjectId(options.getProject()).setInstanceId(options.getBigtableInstanceId()).build();
    BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(adminSettings);
    int clusterNodeCount = getClusterNodeCount(options.getProject(), options.getBigtableInstanceId());
    List<String> newTableIds = getNewTableIds(adminClient, options.getBigtableSize());
    // If the specified size of Bigtable is already met, don't run the pipeline.
    if (newTableIds.isEmpty()) {
        return;
    }
    long numRows = (long) ((TB_PER_TABLE * ONE_TB) / (MB_PER_ROW * ONE_MB));
    long rate = clusterNodeCount * MB_PER_SEC / newTableIds.size();
    String generateLabel = String.format("Generate %d rows at %dMB per second for %d tables", numRows, rate, newTableIds.size());
    String mutationLabel = String.format("Create mutations that write %d MB to each row", MB_PER_ROW);
    System.out.println(generateLabel);
    System.out.println(mutationLabel);
    Pipeline p = Pipeline.create(options);
    PCollection<Mutation> mutations = p.apply(generateLabel, GenerateSequence.from(0).to(numRows).withRate(rate, Duration.standardSeconds(1))).apply(mutationLabel, ParDo.of(new CreateMutationFn()));
    for (String tableId : newTableIds) {
        mutations.apply(String.format("Write data to table %s", tableId), CloudBigtableIO.writeToTable(new CloudBigtableTableConfiguration.Builder().withProjectId(options.getProject()).withInstanceId(options.getBigtableInstanceId()).withTableId(tableId).build()));
    }
    p.run();
}
Also used : BigtableTableAdminSettings(com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings) BigtableTableAdminClient(com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient) Mutation(org.apache.hadoop.hbase.client.Mutation) Pipeline(org.apache.beam.sdk.Pipeline)

Example 14 with Table

use of com.google.cloud.bigtable.admin.v2.models.Table 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 15 with Table

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

the class AbstractBigtableAdmin method modifyTable.

/**
 * {@inheritDoc}
 */
@Override
public void modifyTable(TableName tableName, HTableDescriptor newDescriptor) throws IOException {
    if (isTableAvailable(tableName)) {
        try {
            ModifyColumnFamiliesRequest request = buildModifications(newDescriptor, getTableDescriptor(tableName)).build();
            FutureUtil.unwrap(adminClientWrapper.modifyFamiliesAsync(request));
        } catch (Throwable throwable) {
            throw new IOException(String.format("Failed to modify table '%s'", tableName.getNameAsString()), throwable);
        }
    } else {
        throw new TableNotFoundException(tableName);
    }
}
Also used : TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) IOException(java.io.IOException) ModifyColumnFamiliesRequest(com.google.cloud.bigtable.admin.v2.models.ModifyColumnFamiliesRequest)

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