编写MapReduce程序访问HBase 遇到的问题与解决方法

        根据工作需求,需要测试 MapReduce 程序访问HBase 的性能。由于本人面对MapReduce,HBase都是新手,所以在这个过程中遇到了很多问题,主要如下 :

  1. MapReduce 程序如何引用第三方 jar 包

  2. MapReduce 访问HBase 的安全认证问题 (kerberos)

  3. Hadoop HBase 的conf文件的设定问题




第一个问题解决办法:(这个问题在网上解决办法很多)

  1. 1.  使用 -libjars 参数。  hadoop jar  xxx.jar  -libjars  (xx.jar,yy.jar 逗号隔开)  .....

    然后,在程序中执行:

    1
    2
    3
    TableMapReduceUtil.addDependencyJars(job);
    TableMapReduceUtil.addDependencyJars(job.getConfiguration(),
              org.apache.hadoop.hbase.util.Bytes.class);
  2. 2.   在程序中通过tmpjars参数设定

             job.getConfiguration().set("tmpjars",jars);

             jars为所有外部jar包字符串形式,英文逗号分隔。

             在运行hadoop命令时,可通过参数的方式,把所需要的外部jar包(绝对路径)引入,然后再mapreduce程序中对该参数进行处理,并通过

             job.getConfiguration().set("tmpjars",jars);进行设置。

  3. 3.  方法三(没试过):http://blog.sina.com.cn/s/blog_6638b10d0101bn53.html

  4. 4.  直接在MapReduce程序中 进行 DistributedCache.addFileToClassPath() 编程,将需要的jar放进去, 现在还不会

  5. 5.  第三方jar文件和自己的程序打包到一个jar文件中,程序通过job.getJar()将获得整个文件并将其传至hdfs上,试过 没成功,估计我太2了。

  6. 6.   我的方法:HADOOP_CLASSPATH=`${HBASE_HOME}/bin/hbase classpath` ,直接将当前session的HADOOP_CLASSPATH进行修改,添加HBase的CLASSPATH.

    好处: 简单方便,对第三个问题的解决有帮助。


第二个问题解决办法:


    ​其实解决这个问题很简单,只是新手对HBASE编程不熟造成的。

    ​只需要在 job 设置好以后, 执行 TableMapReduceUtil.initCredentials(job); 该语句为 job 获取访问HBase 的授权Token。


第三个问题解决办法:


    ​之所以有这个问题,是因为:访问hbase时,需要制定hbase集群的 quorum,security 配置等等。如果直接在 jobconf 中设定这些属性, 那么后续配置更改以后,会造成程序维护的困难。那么比较好的办法就是所有配置还是从配置文件中读取,然后合并hbase conf 到 job conf中。  HBaseConfiguration.merge(conf, HBaseConfiguration.create(conf));  需要设置 HADOOP_CLASSPATH, 如前所述。


最终程序的一个框架如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class HBaseMapTest extends Configured implements Tool {
     
    private static Configuration conf;
    public static class RandomReadMapper extends
            Mapper<Object, Text, Text, Text> {
  
        HTable testTable = null;
        public void setup(Context context) throws IOException,
                InterruptedException {
            super.setup(context);
            try {
                testTable = new HTable(context.getConfiguration(),"");        //获取table
            catch (Exception e) {
                System.out.println("Error Info: " + e.getMessage());
            }
        }
  
        public void map(Object arg0, Text arg1, Context context)
                throws IOException, InterruptedException {    
            //htable op                                                       //hbase 操作
        }
    }
  
        public int run(String[] args) throws Exception {
  
            conf = getConf();
            HBaseConfiguration.merge(conf, HBaseConfiguration.create(conf));   //合并 jobconf  hbase_conf
              
            final Job job = new Job(conf, "HBaseMRTest");
            job.setJarByClass(HBaseMapTest.class);
              
            job.setMapperClass(RandomReadMapper.class);
            job.setNumReduceTasks(0);
      
            job.setSpeculativeExecution(false);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(LongWritable.class);
            FileInputFormat.addInputPath(job, inDir);
            FileOutputFormat.setOutputPath(job, outDir);
      
            TableMapReduceUtil.addDependencyJars(job);                          //解决jar包 依赖
            TableMapReduceUtil.addDependencyJars(job.getConfiguration(),
                      org.apache.hadoop.hbase.util.Bytes.class);
            TableMapReduceUtil.initCredentials(job);                            //给job赋予访问hbase的权限
              
            job.waitForCompletion(true);
            return 0;
        }
  
    public static void main(String[] args) throws Exception {
        int res = ToolRunner.run(new Configuration(), new HBaseMapTest(), args);
        System.exit(res);
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值