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 java.util.ArrayList;
020import java.util.List;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseConfiguration;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.Put;
026import org.apache.hadoop.hbase.spark.JavaHBaseContext;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.apache.spark.SparkConf;
029import org.apache.spark.api.java.JavaRDD;
030import org.apache.spark.api.java.JavaSparkContext;
031import org.apache.spark.api.java.function.Function;
032
033/**
034 * This is a simple example of putting records in HBase
035 * with the bulkPut function.
036 */
037final public class JavaHBaseBulkPutExample {
038
039  private JavaHBaseBulkPutExample() {}
040
041  public static void main(String[] args) {
042    if (args.length < 2) {
043      System.out.println("JavaHBaseBulkPutExample  " +
044              "{tableName} {columnFamily}");
045      return;
046    }
047
048    String tableName = args[0];
049    String columnFamily = args[1];
050
051    SparkConf sparkConf = new SparkConf().setAppName("JavaHBaseBulkPutExample " + tableName);
052    JavaSparkContext jsc = new JavaSparkContext(sparkConf);
053
054    try {
055      List<String> list = new ArrayList<>(5);
056      list.add("1," + columnFamily + ",a,1");
057      list.add("2," + columnFamily + ",a,2");
058      list.add("3," + columnFamily + ",a,3");
059      list.add("4," + columnFamily + ",a,4");
060      list.add("5," + columnFamily + ",a,5");
061
062      JavaRDD<String> rdd = jsc.parallelize(list);
063
064      Configuration conf = HBaseConfiguration.create();
065
066      JavaHBaseContext hbaseContext = new JavaHBaseContext(jsc, conf);
067
068      hbaseContext.bulkPut(rdd,
069              TableName.valueOf(tableName),
070              new PutFunction());
071    } finally {
072      jsc.stop();
073    }
074  }
075
076  public static class PutFunction implements Function<String, Put> {
077
078    private static final long serialVersionUID = 1L;
079
080    public Put call(String v) throws Exception {
081      String[] cells = v.split(",");
082      Put put = new Put(Bytes.toBytes(cells[0]));
083
084      put.addColumn(Bytes.toBytes(cells[1]), Bytes.toBytes(cells[2]),
085              Bytes.toBytes(cells[3]));
086      return put;
087    }
088
089  }
090}