|
| 1 | +package com.google.cloud.bigtable.example.bulk_delete; /** |
| 2 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 3 | + * <p/> |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * <p/> |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * <p/> |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +import com.google.cloud.bigtable.beam.AbstractCloudBigtableTableDoFn; |
| 22 | +import com.google.cloud.bigtable.beam.CloudBigtableConfiguration; |
| 23 | +import com.google.cloud.bigtable.beam.CloudBigtableIO; |
| 24 | +import com.google.cloud.bigtable.beam.CloudBigtableTableConfiguration; |
| 25 | +import com.google.common.collect.Lists; |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Collections; |
| 29 | +import org.apache.beam.sdk.Pipeline; |
| 30 | +import org.apache.beam.sdk.options.PipelineOptionsFactory; |
| 31 | +import org.apache.beam.sdk.transforms.Create; |
| 32 | +import org.apache.beam.sdk.transforms.DoFn; |
| 33 | +import org.apache.beam.sdk.transforms.ParDo; |
| 34 | +import org.apache.hadoop.hbase.TableName; |
| 35 | +import org.apache.hadoop.hbase.client.Delete; |
| 36 | +import org.apache.hadoop.hbase.client.Mutation; |
| 37 | +import org.apache.hadoop.hbase.client.Result; |
| 38 | +import org.apache.hadoop.hbase.client.Scan; |
| 39 | +import org.apache.hadoop.hbase.client.Table; |
| 40 | +import org.apache.hadoop.hbase.filter.KeyOnlyFilter; |
| 41 | + |
| 42 | +public class Main { |
| 43 | + public static void main(String[] args) { |
| 44 | + JobOptions jobOptions = PipelineOptionsFactory.fromArgs(args) |
| 45 | + .withValidation() |
| 46 | + .as(JobOptions.class); |
| 47 | + |
| 48 | + CloudBigtableTableConfiguration bigtableConfig = new CloudBigtableTableConfiguration.Builder() |
| 49 | + .withProjectId(jobOptions.getProject()) |
| 50 | + .withInstanceId(jobOptions.getBigtableInstanceId()) |
| 51 | + .withTableId(jobOptions.getBigtableTableId()) |
| 52 | + .build(); |
| 53 | + |
| 54 | + ArrayList<String> prefixes = Lists.newArrayList("prefix1", "prefix2", "prefix3"); |
| 55 | + |
| 56 | + // randomize the prefixes to avoid hotspoting a region. |
| 57 | + Collections.shuffle(prefixes); |
| 58 | + |
| 59 | + Pipeline pipeline = Pipeline.create(jobOptions); |
| 60 | + |
| 61 | + pipeline.apply(Create.of(prefixes)) |
| 62 | + .apply("Scan prefix", ParDo.of(new ScanPrefixDoFn(bigtableConfig, bigtableConfig.getTableId()))) |
| 63 | + .apply("Create mutations", ParDo.of(new DeleteKeyDoFn())) |
| 64 | + .apply("Delete keys", CloudBigtableIO.writeToTable(bigtableConfig)); |
| 65 | + |
| 66 | + pipeline.run().waitUntilFinish(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Query Bigtable for all of the keys that start with the given prefix. |
| 71 | + */ |
| 72 | + static class ScanPrefixDoFn extends AbstractCloudBigtableTableDoFn<String, byte[]> { |
| 73 | + private final String tableId; |
| 74 | + |
| 75 | + public ScanPrefixDoFn(CloudBigtableConfiguration config, String tableId) { |
| 76 | + super(config); |
| 77 | + this.tableId = tableId; |
| 78 | + } |
| 79 | + |
| 80 | + @ProcessElement |
| 81 | + public void processElement(ProcessContext c) throws IOException { |
| 82 | + Scan scan = new Scan() |
| 83 | + .setRowPrefixFilter(c.element().getBytes()) |
| 84 | + .setFilter(new KeyOnlyFilter()); |
| 85 | + |
| 86 | + Table table = getConnection().getTable(TableName.valueOf(tableId)); |
| 87 | + |
| 88 | + for (Result result : table.getScanner(scan)) { |
| 89 | + c.output(result.getRow()); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Converts a row key into a delete mutation to be written to Bigtable. |
| 96 | + */ |
| 97 | + static class DeleteKeyDoFn extends DoFn<byte[], Mutation> { |
| 98 | + @ProcessElement |
| 99 | + public void processElement(ProcessContext c) { |
| 100 | + c.output(new Delete(c.element())); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
0 commit comments