在HBase里使用MapReduce例子

« 如何在Hadoop中使用Streaming编写MapReduce | 首页 | Nutch中MapReduce的分析 »

在HBase里使用MapReduce例子

作者:马士华 发表于:2008-03-05 19:10 最后更新于:2008-03-07 12:15
版权声明:可以任意转载,转载时请务必以超链接形式标明文章 原始出处和作者信息。
http://www.hadoop.org.cn/mapreduce/hbase-mapreduce/

 

  我在Hadoop的用户邮件列表中看到一些国内的用户在讯问一些关于如何操作的HBase的问题,还看到了HBase中没有Example。觉得有必要跟大家分享自己的经验。

在下面的例子中我们分析Apache的log并把这些log进行分析并把分析完的结果按用户IP为ROW,把log中用户的访问时间,请求方法,用户请求的协议,用户的浏览器,服务状态等写到HBase的表中。

 

首先我们要在HBase中建立我们的一个表来存储数据。

 
 
  public static void creatTable(String table) throws IOException{
	    HConnection conn = HConnectionManager.getConnection(conf);
	    HBaseAdmin admin = new HBaseAdmin(conf);
	    if(!admin.tableExists(new Text(table))){
	      System.out.println("1. " + table + " table creating ... please wait");
	      HTableDescriptor tableDesc = new HTableDescriptor(table);
	      tableDesc.addFamily(new HColumnDescriptor("http:"));
	      tableDesc.addFamily(new HColumnDescriptor("url:"));
	      tableDesc.addFamily(new HColumnDescriptor("referrer:"));
	      admin.createTable(tableDesc);
	    } else {
	      System.out.println("1. " + table + " table already exists.");
	    }
	    System.out.println("2. access_log files fetching using map/reduce");
  }
 

然后我们运行一个MapReduce任务来取得log中的每一行数据。因为我们只要取得数据而不需要对结果进行规约,我们只要编写一个Map程序即可。

 
 
  public static class MapClass extends MapReduceBase implements
      Mapper<WritableComparable, Text, Text, Writable> {
 
    @Override
    public void configure(JobConf job) {
      tableName = job.get(TABLE, "");
    }
 
    public void map(WritableComparable key, Text value,
        OutputCollector<Text, Writable> output, Reporter reporter)
        throws IOException {
      try {
    	 AccessLogParser log = new AccessLogParser(value.toString());
        if(table==null)
        	table = new HTable(conf, new Text(tableName));
        long lockId = table.startUpdate(new Text(log.getIp()));
        table.put(lockId, new Text("http:protocol"), log.getProtocol().getBytes());
        table.put(lockId, new Text("http:method"), log.getMethod().getBytes());
        table.put(lockId, new Text("http:code"), log.getCode().getBytes());
        table.put(lockId, new Text("http:bytesize"), log.getByteSize().getBytes());
        table.put(lockId, new Text("http:agent"), log.getAgent().getBytes());
        table.put(lockId, new Text("url:" + log.getUrl()), log.getReferrer().getBytes());
        table.put(lockId, new Text("referrer:" + log.getReferrer()), log.getUrl().getBytes());
 
        table.commit(lockId, log.getTimestamp());
      } catch (ParseException e) {
        e.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
 

我们在Map程序中对于传进来的每一行先交给AccessLogParser去处理在AccessLogParser德构造器中用一个正则表达式"([^ ]*) ([^ ]*) ([^ ]*) //[([^]]*)//] /"([^/"]*)/" " ([^ ]*) ([^ ]*) /"([^/"]*)/" /"([^/"]*)/".*"来匹配每一行的log。接下来我们把这些AccessLogParser处理出来的结果更新到HBase的表中去,好的,我们的程序写完了。我们要启动一个MapReduce的话我们要对工作进行配置。

 
 
  public static void runMapReduce(String table,String dir) throws IOException{
	  Path tempDir = new Path("log/temp");
	  Path InputDir = new Path(dir);
	  FileSystem fs = FileSystem.get(conf);
	  JobConf jobConf = new JobConf(conf, LogFetcher.class);
	  jobConf.setJobName("apache log fetcher");
	  jobConf.set(TABLE, table);
	  Path[] in = fs.listPaths(InputDir);
	  if (fs.isFile(InputDir)) {
	      jobConf.setInputPath(InputDir);
	  } else {
	      for (int i = 0; i < in.length; i++) {
	        if (fs.isFile(in[i])) {
	          jobConf.addInputPath(in[i]);
	        } else {
	          Path[] sub = fs.listPaths(in[i]);
	          for (int j = 0; j < sub.length; j++) {
	            if (fs.isFile(sub[j])) {
	              jobConf.addInputPath(sub[j]);
	            }
	          }
	        }
	      }
	    }
	    jobConf.setOutputPath(tempDir);
	    jobConf.setMapperClass(MapClass.class);
 
	    JobClient client = new JobClient(jobConf);
	    ClusterStatus cluster = client.getClusterStatus();
	    jobConf.setNumMapTasks(cluster.getMapTasks());
	    jobConf.setNumReduceTasks(0);
 
	    JobClient.runJob(jobConf);
	    fs.delete(tempDir);
	    fs.close();
  }
 

在上面的代码中我们先产生一个jobConf对象,然后设定我们的InputPath和OutputPath,告诉MapReduce我们的Map类,设定我们用多少个Map任务和Reduce任务,然后我们不任务提交给JobClient,关于MapReduce跟详细的资料在Hadoop Wiki上。

下载:源码和已编译好的jar文件example-src.tgz

例子的运行命令是:


bin/hadoop jar examples.jar logfetcher <access_log file or directory> <table_name>

如何运行上面的应用程序呢?我们假定解压缩完Hadoop分发包的目录为%HADOOP%

拷贝%HADOOP%/contrib/hbase/bin下的文件到%HADOOP%/bin下,拷贝%HADOOP%/contrib/hbase/conf的文件到%HADOOP%/conf下,拷贝%HADOOP%/src/contrib/hbase/lib的文件到%HADOOP%/lib下,拷贝%HADOOP%/src/contrib/hbase/hadoop-*-hbase.jar的文件到%HADOOP%/lib下.然后编辑配置文件hbase-site.xml设定你的hbase.master例子:192.168.2.92:60000。把这些文件分发到运行Hadoop的机器上去。在regionservers文件添加上这些已分发过的地址。运行bin/start-hbase.sh命令启动HBase,把你的apache log文件拷贝到HDFS的apache-log目录下,等启动完成后运行下面的命令。


bin/hadoop jar examples.jar logfetcher apache-log apache

访问http://localhost:50030/能看到你的MapReduce任务的运行情况,访问http://localhost:60010/能看到HBase的运行情况。

hbaseguiinterface.jpg

等任务MapReduce完成后访问http://localhost:60010/hql.jsp,在Query输入框中输入SELECT * FROM apache limit=50;。将会看到已经插入表中的数据。

hqlguiinterface.jpg


相关文章

引用通告

如果您想引用这篇文章到您的Blog,
请复制下面的链接,并放置到您发表文章的相应界面中。
http://www.hadoop.org.cn/mapreduce/hbase-mapreduce/trackback/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值