HBase MapReduce实例分析

        转载自:http://www.cnblogs.com/NicholasLee/archive/2012/09/14/2685053.html


  跟Hadoop的无缝集成使得使用MapReduce对HBase的数据进行分布式计算非常方便,本文将介绍HBase下 MapReduce开发要点。很好理解本文前提是你对Hadoop MapReduce有一定的了解,如果你是初次接触Hadoop MapReduce编程,可以参考"第一个MapReduce应用" 这篇文章来建立基本概念。

一、Java代码

       
package hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;

public class WordCountHBase {

    public static class Map extends
            Mapper<LongWritable, Text, Text, IntWritable> {
        private IntWritable i = new IntWritable(1);

        public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            String s[] = value.toString().trim().split(" ");
            // 将输入的每行以空格分开
            for (String m : s) {
                context.write(new Text(m), i);
            }
        }
    }

    public static class Reduce extends
            TableReducer<Text, IntWritable, NullWritable> {
        public void reduce(Text key, Iterable<IntWritable> values,
                Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable i : values) {
                sum += i.get();
            }
            Put put = new Put(Bytes.toBytes(key.toString()));
            // Put实例化,每一个词存一行
            put.add(Bytes.toBytes("content"), Bytes.toBytes("count"),
                    Bytes.toBytes(String.valueOf(sum)));
            // 列族为content,列为count,列值为数目
            context.write(NullWritable.get(), put);
        }
    }

    public static void createHBaseTable(String tableName) throws IOException {
        HTableDescriptor htd = new HTableDescriptor(tableName);
        HColumnDescriptor col = new HColumnDescriptor("content");
        htd.addFamily(col);
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "libin2");
        HBaseAdmin admin = new HBaseAdmin(conf);
        if (admin.tableExists(tableName)) {
            System.out.println("table exists, trying to recreate table......");
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
        }
        System.out.println("create new table:" + tableName);
        admin.createTable(htd);
    }

    public static void main(String[] args) throws IOException,
            InterruptedException, ClassNotFoundException {
        String tableName = "WordCount";
        Configuration conf = new Configuration();
        conf.set(TableOutputFormat.OUTPUT_TABLE, tableName);
        createHBaseTable(tableName);
        String input = args[0];
        Job job = new Job(conf, "WordCount table with " + input);
        job.setJarByClass(WordCountHBase.class);
        job.setNumReduceTasks(3);
        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TableOutputFormat.class);
        FileInputFormat.addInputPath(job, new Path(input));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
二、把java代码打成jar包


如果同时用到了两个jar包,需要在两个jar包之间加一个":"分隔符。


 

三、运行程序


运行WordCountHBase.jar可能会报错:java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/HTableDescriptor

解决方法(把hbase的核心jar包和hbase自带的Zookeeperjar包拷贝到hadoop的安装目录\lib下,然后重启服务):


然后再次执行




四、查看HBase表中的数据


  如果表中有保存好的MapReduce处理后的数据,说明成功!本文通过实例分析演示了使用MapReduce分析HBase的数据,需要注意的这只是一种常规的方式(分析表中的数据存到另外的表中),实际上不局限于此,不过其他方式跟此类似。



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值