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.Iterator; 021import java.util.List; 022 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.Cell; 025import org.apache.hadoop.hbase.HBaseConfiguration; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.client.Get; 028import org.apache.hadoop.hbase.client.Result; 029import org.apache.hadoop.hbase.spark.JavaHBaseContext; 030import org.apache.hadoop.hbase.util.Bytes; 031import org.apache.spark.SparkConf; 032import org.apache.spark.api.java.JavaRDD; 033import org.apache.spark.api.java.JavaSparkContext; 034import org.apache.spark.api.java.function.Function; 035 036/** 037 * This is a simple example of getting records in HBase 038 * with the bulkGet function. 039 */ 040final public class JavaHBaseBulkGetExample { 041 042 private JavaHBaseBulkGetExample() {} 043 044 public static void main(String[] args) { 045 if (args.length < 1) { 046 System.out.println("JavaHBaseBulkGetExample {tableName}"); 047 return; 048 } 049 050 String tableName = args[0]; 051 052 SparkConf sparkConf = new SparkConf().setAppName("JavaHBaseBulkGetExample " + tableName); 053 JavaSparkContext jsc = new JavaSparkContext(sparkConf); 054 055 try { 056 List<byte[]> list = new ArrayList<>(5); 057 list.add(Bytes.toBytes("1")); 058 list.add(Bytes.toBytes("2")); 059 list.add(Bytes.toBytes("3")); 060 list.add(Bytes.toBytes("4")); 061 list.add(Bytes.toBytes("5")); 062 063 JavaRDD<byte[]> rdd = jsc.parallelize(list); 064 065 Configuration conf = HBaseConfiguration.create(); 066 067 JavaHBaseContext hbaseContext = new JavaHBaseContext(jsc, conf); 068 069 hbaseContext.bulkGet(TableName.valueOf(tableName), 2, rdd, new GetFunction(), 070 new ResultFunction()); 071 } finally { 072 jsc.stop(); 073 } 074 } 075 076 public static class GetFunction implements Function<byte[], Get> { 077 078 private static final long serialVersionUID = 1L; 079 080 public Get call(byte[] v) throws Exception { 081 return new Get(v); 082 } 083 } 084 085 public static class ResultFunction implements Function<Result, String> { 086 087 private static final long serialVersionUID = 1L; 088 089 public String call(Result result) throws Exception { 090 Iterator<Cell> it = result.listCells().iterator(); 091 StringBuilder b = new StringBuilder(); 092 093 b.append(Bytes.toString(result.getRow())).append(":"); 094 095 while (it.hasNext()) { 096 Cell cell = it.next(); 097 String q = Bytes.toString(cell.getQualifierArray()); 098 if (q.equals("counter")) { 099 b.append("(") 100 .append(Bytes.toString(cell.getQualifierArray())) 101 .append(",") 102 .append(Bytes.toLong(cell.getValueArray())) 103 .append(")"); 104 } else { 105 b.append("(") 106 .append(Bytes.toString(cell.getQualifierArray())) 107 .append(",") 108 .append(Bytes.toString(cell.getValueArray())) 109 .append(")"); 110 } 111 } 112 return b.toString(); 113 } 114 } 115}