001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.hadoop.hbase.spark.example.hbasecontext;
018
019import org.apache.hadoop.conf.Configuration;
020import org.apache.hadoop.hbase.HBaseConfiguration;
021import org.apache.hadoop.hbase.TableName;
022import org.apache.hadoop.hbase.client.Delete;
023import org.apache.hadoop.hbase.spark.JavaHBaseContext;
024import org.apache.hadoop.hbase.util.Bytes;
025import org.apache.spark.SparkConf;
026import org.apache.spark.api.java.JavaRDD;
027import org.apache.spark.api.java.JavaSparkContext;
028import org.apache.spark.api.java.function.Function;
029
030import java.util.ArrayList;
031import java.util.List;
032
033/**
034 * This is a simple example of deleting records in HBase
035 * with the bulkDelete function.
036 */
037final public class JavaHBaseBulkDeleteExample {
038
039  private JavaHBaseBulkDeleteExample() {}
040
041  public static void main(String[] args) {
042    if (args.length < 1) {
043      System.out.println("JavaHBaseBulkDeleteExample  {tableName}");
044      return;
045    }
046
047    String tableName = args[0];
048
049    SparkConf sparkConf = new SparkConf().setAppName("JavaHBaseBulkDeleteExample " + tableName);
050    JavaSparkContext jsc = new JavaSparkContext(sparkConf);
051
052    try {
053      List<byte[]> list = new ArrayList<>(5);
054      list.add(Bytes.toBytes("1"));
055      list.add(Bytes.toBytes("2"));
056      list.add(Bytes.toBytes("3"));
057      list.add(Bytes.toBytes("4"));
058      list.add(Bytes.toBytes("5"));
059
060      JavaRDD<byte[]> rdd = jsc.parallelize(list);
061
062      Configuration conf = HBaseConfiguration.create();
063
064      JavaHBaseContext hbaseContext = new JavaHBaseContext(jsc, conf);
065
066      hbaseContext.bulkDelete(rdd,
067              TableName.valueOf(tableName), new DeleteFunction(), 4);
068    } finally {
069      jsc.stop();
070    }
071
072  }
073
074  public static class DeleteFunction implements Function<byte[], Delete> {
075    private static final long serialVersionUID = 1L;
076    public Delete call(byte[] v) throws Exception {
077      return new Delete(v);
078    }
079  }
080}