Search in sources :

Example 1 with BigtableTableAdminClient

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

the class ReadsTest method beforeClass.

@BeforeClass
public static void beforeClass() throws IOException {
    projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
    instanceId = requireEnv(INSTANCE_ENV);
    try (BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(projectId, instanceId)) {
        CreateTableRequest createTableRequest = CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_NAME);
        adminClient.createTable(createTableRequest);
        try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
            BulkMutation bulkMutation = BulkMutation.create(TABLE_ID).add("phone#4c410523#20190501", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190405.003")).add("phone#4c410523#20190502", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190405.004")).add("phone#4c410523#20190505", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 0).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190406.000")).add("phone#5c10102#20190501", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190401.002")).add("phone#5c10102#20190502", Mutation.create().setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_cell".getBytes()), TIMESTAMP_NANO, 1).setCell(COLUMN_FAMILY_NAME, ByteString.copyFrom("connected_wifi".getBytes()), TIMESTAMP_NANO, 0).setCell(COLUMN_FAMILY_NAME, "os_build", TIMESTAMP_NANO, "PQ2A.190406.000"));
            dataClient.bulkMutateRows(bulkMutation);
        }
    } catch (Exception e) {
        System.out.println("Error during beforeClass: \n" + e.toString());
        throw (e);
    }
}
Also used : BulkMutation(com.google.cloud.bigtable.data.v2.models.BulkMutation) BigtableTableAdminClient(com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) CreateTableRequest(com.google.cloud.bigtable.admin.v2.models.CreateTableRequest) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 2 with BigtableTableAdminClient

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

the class MemcachedTest method beforeClass.

@BeforeClass
public static void beforeClass() {
    projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
    instanceId = requireEnv(INSTANCE_ENV);
    try (BigtableTableAdminClient adminClient = BigtableTableAdminClient.create(projectId, instanceId)) {
        CreateTableRequest createTableRequest = CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_NAME);
        adminClient.createTable(createTableRequest);
        try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
            String rowkey = "phone#4c410523#20190501";
            RowMutation rowMutation = RowMutation.create(TABLE_ID, rowkey).setCell(COLUMN_FAMILY_NAME, "os_build", "PQ2A.190405.003");
            dataClient.mutateRow(rowMutation);
        }
        String[] dockerCommand = (String.format("docker run --name %s -itd --rm --publish 11211:11211 sameersbn/memcached:latest", MEMCACHED_CONTAINER_NAME)).split(" ");
        Process process = new ProcessBuilder(dockerCommand).start();
        process.waitFor();
    } catch (Exception e) {
        System.out.println("Error during beforeClass: \n" + e.toString());
    }
}
Also used : RowMutation(com.google.cloud.bigtable.data.v2.models.RowMutation) BigtableTableAdminClient(com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient) CreateTableRequest(com.google.cloud.bigtable.admin.v2.models.CreateTableRequest) BeforeClass(org.junit.BeforeClass)

Example 3 with BigtableTableAdminClient

use of com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient 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 4 with BigtableTableAdminClient

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

the class BigtableBackupIT method createAndPopulateTestTable.

private static Table createAndPopulateTestTable(BigtableTableAdminClient tableAdmin, BigtableDataClient dataClient) throws InterruptedException {
    String tableId = PrefixGenerator.newPrefix("BigtableBackupIT#createAndPopulateTestTable");
    Table testTable = tableAdmin.createTable(CreateTableRequest.of(tableId).addFamily("cf1"));
    // Populate test data.
    byte[] rowBytes = new byte[1024];
    Random random = new Random();
    random.nextBytes(rowBytes);
    try (Batcher<RowMutationEntry, Void> batcher = dataClient.newBulkMutationBatcher(tableId)) {
        for (int i = 0; i < 10; i++) {
            batcher.add(RowMutationEntry.create("test-row-" + i).setCell("cf1", ByteString.EMPTY, ByteString.copyFrom(rowBytes)));
        }
    }
    return testTable;
}
Also used : Table(com.google.cloud.bigtable.admin.v2.models.Table) Random(java.util.Random) RowMutationEntry(com.google.cloud.bigtable.data.v2.models.RowMutationEntry) ByteString(com.google.protobuf.ByteString)

Example 5 with BigtableTableAdminClient

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

the class BigtableBackupIT method crossInstanceRestoreTest.

@Test
public void crossInstanceRestoreTest() throws InterruptedException, IOException, ExecutionException, TimeoutException {
    String backupId = prefixGenerator.newPrefix();
    String restoredTableId = prefixGenerator.newPrefix();
    // Create the backup
    tableAdmin.createBackup(CreateBackupRequest.of(targetCluster, backupId).setSourceTableId(testTable.getId()).setExpireTime(Instant.now().plus(Duration.ofHours(6))));
    Stopwatch stopwatch = Stopwatch.createStarted();
    // Set up a new instance to test cross-instance restore. The backup will be restored here
    String targetInstance = prefixGenerator.newPrefix();
    instanceAdmin.createInstance(CreateInstanceRequest.of(targetInstance).addCluster(targetInstance, testEnvRule.env().getSecondaryZone(), 1, StorageType.SSD).setDisplayName("backups-dest-test-instance").addLabel("state", "readytodelete").setType(Type.PRODUCTION));
    try (BigtableTableAdminClient destTableAdmin = testEnvRule.env().getTableAdminClientForInstance(targetInstance)) {
        // Wait 2 minutes so that the RestoreTable API will trigger an optimize restored
        // table operation.
        Thread.sleep(Duration.ofMinutes(2).minus(Duration.ofMillis(stopwatch.elapsed(TimeUnit.MILLISECONDS))).toMillis());
        try {
            RestoreTableRequest req = RestoreTableRequest.of(testEnvRule.env().getInstanceId(), targetCluster, backupId).setTableId(restoredTableId);
            RestoredTableResult result = destTableAdmin.restoreTable(req);
            assertWithMessage("Incorrect restored table id").that(result.getTable().getId()).isEqualTo(restoredTableId);
            assertWithMessage("Incorrect instance id").that(result.getTable().getInstanceId()).isEqualTo(targetInstance);
            // The assertion might be missing if the test is running against a HDD cluster or an
            // optimization is not necessary.
            assertWithMessage("Empty OptimizeRestoredTable token").that(result.getOptimizeRestoredTableOperationToken()).isNotNull();
            destTableAdmin.awaitOptimizeRestoredTable(result.getOptimizeRestoredTableOperationToken());
            destTableAdmin.getTable(restoredTableId);
        } finally {
            tableAdmin.deleteBackup(targetCluster, backupId);
            instanceAdmin.deleteInstance(targetInstance);
        }
    }
}
Also used : RestoreTableRequest(com.google.cloud.bigtable.admin.v2.models.RestoreTableRequest) Stopwatch(com.google.common.base.Stopwatch) RestoredTableResult(com.google.cloud.bigtable.admin.v2.models.RestoredTableResult) ByteString(com.google.protobuf.ByteString) BigtableTableAdminClient(com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient) Test(org.junit.Test)

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