上一篇文章是实现统计hbase单元值出现的个数,并将结果存放到hbase的表中,本文是将结果存放到hdfs上。其中的map实现与前文一直,连接:http://www.cnblogs.com/ljy2013/p/4820056.html,下面主要介绍一下reduce的实现:

(1)reduce的实现

package com.datacenter.HbaseMapReduce.SummaryToFile;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class SummaryToFileReducer extends
Reducer<Text, IntWritable, Text, IntWritable> { @Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
int i = 0;
for (IntWritable val : values) {
i += val.get();
}
context.write(key, new IntWritable(i));
} }

(2)主类的实现也有些不同

package com.datacenter.HbaseMapReduce.SummaryToFile;

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.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class SummaryToFilemain {
static String rootdir = "hdfs://hadoop3:8020/hbase";
static String zkServer = "hadoop3";
static String port = "2181"; private static Configuration conf;
private static HConnection hConn = null; public static void HbaseUtil(String rootDir, String zkServer, String port) { conf = HBaseConfiguration.create();// 获取默认配置信息
conf.set("hbase.rootdir", rootDir);
conf.set("hbase.zookeeper.quorum", zkServer);
conf.set("hbase.zookeeper.property.clientPort", port); try {
hConn = HConnectionManager.createConnection(conf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
HbaseUtil(rootdir, zkServer, port); Job job = new Job(conf,"ExampleSummaryToFile");
job.setJarByClass(SummaryToFilemain.class); // class that contains mapper and reducer Scan scan = new Scan();
scan.setCaching(500); // 1 is the default in Scan, which will be bad for MapReduce jobs
scan.setCacheBlocks(false); // don't set to true for MR jobs
// set other scan attrs TableMapReduceUtil.initTableMapperJob(
"test", // input table
scan, // Scan instance to control CF and attribute selection
SummaryMapper.class, // mapper class
Text.class, // mapper output key
IntWritable.class, // mapper output value
job);
job.setReducerClass(SummaryToFileReducer.class); // reducer class
job.setNumReduceTasks(1); // at least one, adjust as required
FileOutputFormat.setOutputPath(job, new Path("hdfs://hadoop3:8020/user/liujiyu/score-test")); // adjust directories as required boolean b = job.waitForCompletion(true);
if (!b) {
throw new IOException("error with job!");
}
} }

HBase with MapReduce (SummaryToFile)的更多相关文章

  1. HBase with MapReduce (Summary)

    我们知道,hbase没有像关系型的数据库拥有强大的查询功能和统计功能,本文实现了如何利用mapreduce来统计hbase中单元值出现的个数,并将结果携带目标的表中, (1)mapper的实现 pac ...

  2. Hbase 技术细节笔记(上)

    欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:张秀云 前言 最近在跟进Hbase的相关工作,由于之前对Hbase并不怎么了解,因此系统地学习了下Hbase,为了加深对Hbase的 ...

  3. 深入HBase架构解析(二)【转】

    转自:http://www.blogjava.net/DLevin/archive/2015/08/22/426950.html 前言 这是<深入HBase架构解析(一)>的续,不多废话, ...

  4. [转]毕设- 深入HBase架构解析(二)

    深入HBase架构解析(二) 前言 这是<深入HBase架构解析(一)>的续,不多废话,继续.... HBase读的实现 通过前文的描述,我们知道在HBase写时,相同Cell(RowKe ...

  5. HBase框架基础(四)

    * HBase框架基础(四) 上一节我们介绍了如何使用HBase搞一些MapReduce小程序,其主要作用呢是可以做一些数据清洗和分析或者导入数据的工作,这一节我们来介绍如何使用HBase与其他框架进 ...

  6. HBase框架基础(三)

    * HBase框架基础(三) 本节我们继续讨论HBase的一些开发常识,以及HBase与其他框架协调使用的方式.在开始之前,为了框架之间更好的适配,以及复习之前HBase的配置操作,请使用cdh版本的 ...

  7. HBase框架基础(一)

    * HBase框架基础(一) 官方网址:http://hbase.apache.org/ * HBase是什么妖怪? 要解释HBase,我们就先说一说经常接触到的RDBMS,即关系型数据库: ** m ...

  8. HBase框架基础(二)

    * HBase框架基础(二) 上一节我们了解了HBase的架构原理和模块组成,这一节我们先来聊一聊HBase的读写数据的过程. * HBase的读写流程及3个机制 HBase的读数据流程: 1.HRe ...

  9. MapReduce(二)

    MapReduce(二) mapreduce 将Text转化为对象进行处理数据. 根据一来说,将date,classname,name,subject,score变为对象属性 我的数据是:是有重复的. ...

随机推荐

  1. 芯航线FPGA学习套件之4*4矩阵键盘模块测试手册

    芯航线FPGA学习套件之4*4矩阵键盘模块测试手册   本手册以简明扼要的方式介绍芯航线FPGA学习套件提供的矩阵键盘模块的测试方法:   连接开发板,如下所示: 2.将矩阵键盘模块与开发板按如下图所 ...

  2. C# Cookie工具类

    /// <summary> /// Cookies赋值 /// </summary> /// <param name="strName">主键& ...

  3. ArcgisAdd-In开发入门实例

    1.开发环境 Vs2012+Arcgis10.2+win7 64bit 2.实现代码 首先在VS2012中新建一个解决方案,命名AddInTest. 接着,给解决方案AddInTest新建一个项目: ...

  4. 安装 phoneGap

    npm config set registry http://registry.cnpmjs.org 使用最新版本PhoneGap ,决定使用官方提供的方法安装一次. 官方提供方法有些地方没有提到,因 ...

  5. jquery之empty()方法详解

    empty()函数用于清空每个匹配元素内的所有内容. empty()函数将会移除每个匹配元素的所有子节点(包括文本节点.注释节点等所有类型的节点). 该函数属于jQuery对象(实例). 语法 jQu ...

  6. sqlite创建表

    create table bike (id ) primary key, password ));

  7. createjs 更新

    使用0.8.2版本 不在需要单独 引入 MovieClip 文件了

  8. 深入浅出设计模式——职责链模式(Chain of Responsibility Pattern)

    模式动机 职责链可以是一条直线.一个环或者一个树形结构,最常见的职责链是直线型,即沿着一条单向的链来传递请求.链上的每一个对象都是请求处理者,职责链模式可以将请求的处理者组织成一条链,并使请求沿着链传 ...

  9. webform简单控件

    表单元素: 文本类: text password textarea hidden text,password,textarea实现控件:textbox   textmode属性选择password或m ...

  10. 如何让IE8的菜单栏调到最上方

    如何让IE8的菜单栏调到最上方 运行gpedit.msc,在"用户配置"/"管理模板"/"Windows Components"/" ...