MapReduce从HBase读写数据简单示例
就用单词计数这个例子,需要统计的单词存在HBase中的word表,MapReduce执行的时候从word表读取数据,统计结束后将结果写入到HBase的stat表中。
1、在eclipse中建立一个hadoop项目,然后从hbase的发布包中引入如下jar
hbase-0.94.13.jar
zookeeper-3.4.5.jar
protobuf-java-2.4.0a.jar
guava-11.0.2.jar
2、在HBase中建立相关的表和初始化测试数据
package cn.luxh.app; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put; /**
*
* @author Luxh
*
*/
public class InitData { public static void main(String[] args) throws IOException {
//创建一个word表,只有一个列族content
HBaseUtil.createTable("word","content"); //获取word表
HTable htable = HBaseUtil.getHTable("word");
htable.setAutoFlush(false); //创建测试数据
List<Put> puts = new ArrayList<Put>(); Put put1 = HBaseUtil.getPut("1","content",null,"The Apache Hadoop software library is a framework");
Put put2 = HBaseUtil.getPut("2","content",null,"The common utilities that support the other Hadoop modules");
Put put3 = HBaseUtil.getPut("3","content",null,"Hadoop by reading the documentation");
Put put4 = HBaseUtil.getPut("4","content",null,"Hadoop from the release page");
Put put5 = HBaseUtil.getPut("5","content",null,"Hadoop on the mailing list"); puts.add(put1);
puts.add(put2);
puts.add(put3);
puts.add(put4);
puts.add(put5); //提交测试数据
htable.put(puts);
htable.flushCommits();
htable.close();
//创建stat表,只有一个列祖result
HBaseUtil.createTable("stat","result");
}
}
1)代码中的HBaseUtil工具类参考:http://www.cnblogs.com/luxh/archive/2013/04/16/3025172.html
2)执行上面的程序后,查看HBase中是否已创建成功
hbase(main):012:0> list
TABLE
stat
word
2 row(s) in 0.4730 seconds
3)查看word中的测试数据
hbase(main):005:0> scan 'word'
ROW COLUMN+CELL
1 column=content:, timestamp=1385447676510, value=The Apache Hadoo
p software library is a framework
2 column=content:, timestamp=1385447676510, value=The common utili
ties that support the other Hadoop modules
3 column=content:, timestamp=1385447676510, value=Hadoop by readin
g the documentation
4 column=content:, timestamp=1385447676510, value=Hadoop from the
release page
5 column=content:, timestamp=1385447676510, value=Hadoop on the ma
iling list
5 row(s) in 5.7810 seconds
3、MapReduce程序
package cn.luxh.app; import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
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.Text;
import org.apache.hadoop.mapreduce.Job; /**
* @author Luxh
*
*/
public class WordStat { /**
* TableMapper<Text,IntWritable> Text:输出的key类型,IntWritable:输出的value类型
*/
public static class MyMapper extends TableMapper<Text,IntWritable>{ private static IntWritable one = new IntWritable(1);
private static Text word = new Text(); @Override
protected void map(ImmutableBytesWritable key, Result value,
Context context)
throws IOException, InterruptedException {
//表里面只有一个列族,所以我就直接获取每一行的值
String words = Bytes.toString(value.list().get(0).getValue());
StringTokenizer st = new StringTokenizer(words);
while (st.hasMoreTokens()) {
String s = st.nextToken();
word.set(s);
context.write(word, one);
}
}
} /**
* TableReducer<Text,IntWritable> Text:输入的key类型,IntWritable:输入的value类型,ImmutableBytesWritable:输出类型
*/
public static class MyReducer extends TableReducer<Text,IntWritable,ImmutableBytesWritable>{ @Override
protected void reduce(Text key, Iterable<IntWritable> values,
Context context)
throws IOException, InterruptedException { int sum = 0;
for(IntWritable val:values) {
sum+=val.get();
}
//添加一行记录,每一个单词作为行键
Put put = new Put(Bytes.toBytes(key.toString()));
//在列族result中添加一个标识符num,赋值为每个单词出现的次数
//String.valueOf(sum)先将数字转化为字符串,否则存到数据库后会变成\x00\x00\x00\x这种形式
//然后再转二进制存到hbase。
put.add(Bytes.toBytes("result"), Bytes.toBytes("num"), Bytes.toBytes(String.valueOf(sum)));
context.write(new ImmutableBytesWritable(Bytes.toBytes(key.toString())),put);
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration conf = HBaseConfiguration.create();
Job job = new Job(conf,"wordstat");
job.setJarByClass(Blog.class); Scan scan = new Scan();
//指定要查询的列族
scan.addColumn(Bytes.toBytes("content"),null);
//指定Mapper读取的表为word
TableMapReduceUtil.initTableMapperJob("word", scan, MyMapper.class, Text.class, IntWritable.class, job);
//指定Reducer写入的表为stat
TableMapReduceUtil.initTableReducerJob("stat", MyReducer.class, job);
System.exit(job.waitForCompletion(true)?0:1);
}
}
等待程序执行结束,查看统计表stat
hbase(main):014:0> scan 'stat'
ROW COLUMN+CELL
Apache column=result:num, timestamp=1385449492309, value=1
Hadoop column=result:num, timestamp=1385449492309, value=5
The column=result:num, timestamp=1385449492309, value=2
a column=result:num, timestamp=1385449492309, value=1
by column=result:num, timestamp=1385449492309, value=1
common column=result:num, timestamp=1385449492309, value=1
documentation column=result:num, timestamp=1385449492309, value=1
framework column=result:num, timestamp=1385449492309, value=1
from column=result:num, timestamp=1385449492309, value=1
is column=result:num, timestamp=1385449492309, value=1
library column=result:num, timestamp=1385449492309, value=1
list column=result:num, timestamp=1385449492309, value=1
mailing column=result:num, timestamp=1385449492309, value=1
modules column=result:num, timestamp=1385449492309, value=1
on column=result:num, timestamp=1385449492309, value=1
other column=result:num, timestamp=1385449492309, value=1
page column=result:num, timestamp=1385449492309, value=1
reading column=result:num, timestamp=1385449492309, value=1
release column=result:num, timestamp=1385449492309, value=1
software column=result:num, timestamp=1385449492309, value=1
support column=result:num, timestamp=1385449492309, value=1
that column=result:num, timestamp=1385449492309, value=1
the column=result:num, timestamp=1385449492309, value=4
utilities column=result:num, timestamp=1385449492309, value=1
24 row(s) in 0.7970 seconds
MapReduce从HBase读写数据简单示例的更多相关文章
- IO流读写数据简单示例
常用的字节输入流有:InputStream ,FileInputStream,BufferedInputStream 常用的字节输出流有:OutputStream,FileOutputStream,B ...
- HBase读写数据的详细流程及ROOT表/META表介绍
一.HBase读数据流程 1.Client访问Zookeeper,从ZK获取-ROOT-表的位置信息,通过访问-ROOT-表获取.META.表的位置,然后确定数据所在的HRegion位置: 2.Cli ...
- HtmlAgilityPack抓取搜房网数据简单示例
HtmlAgilityPack是一个开源的解析HTML元素的类库,最大的特点是可以通过XPath来解析HMTL,如果您以前用C#操作过XML,那么使用起HtmlAgilityPack也会得心应手.目前 ...
- django admin 导出数据简单示例
借鉴博客:https://www.lijinlong.cc/django/djxs/2101.html 具体代码实现: class TipReport(admin.ModelAdmin): actio ...
- js抽奖概率随机取出数据(简单示例)
在平常活动开发当中,经常会碰到抽奖等类似的js功能,那么下面我们随机取数组中的一条来展示出来. ( 一 ) 无概率问题 var gift_ = ['apple pro一台','iphoneX一台',' ...
- HBase读写的几种方式(二)spark篇
1. HBase读写的方式概况 主要分为: 纯Java API读写HBase的方式: Spark读写HBase的方式: Flink读写HBase的方式: HBase通过Phoenix读写的方式: 第一 ...
- 【转帖】HBase读写的几种方式(二)spark篇
HBase读写的几种方式(二)spark篇 https://www.cnblogs.com/swordfall/p/10517177.html 分类: HBase undefined 1. HBase ...
- HBase学习之路 (五)MapReduce操作Hbase
MapReduce从HDFS读取数据存储到HBase中 现有HDFS中有一个student.txt文件,格式如下 95002,刘晨,女,19,IS 95017,王风娟,女,18,IS 95018,王一 ...
- HBase Java简单示例
Hbase采用Java实现,原生客户端也是Java实现,其他语言需要通过thritf接口服务间接访问Hbase的数据. Hbase作为大数据存储数据库,其写能力非常强,加上Hbase本身就脱胎于Had ...
随机推荐
- Reporting Service 报表报 rsReportServerDatabaseError 错的解决方法
一.打开CRM的报表出现“报表服务器数据库内出错.此错误可能是因连接失败.超时或数据库中磁盘空间不足而导致的. (rsReportServerDatabaseError) 获取联机帮助 对象名 'Re ...
- [linux] linux 破解版confluence安装
OS centos 6.5 需要的安装包如下: jre-7u67-linux-x64.rpm atlassian-confluence-5.4.4-x64.bin mysql-connector- ...
- 【svn】 linux svn 强制提交注释
在svn版本库的hooks文件夹下面,复制模版pre-commit.tmpl cp pre-commit.tmpl pre-commit chmod +x pre-commit vi编辑,如下: #! ...
- CSS3中样式顺序
.box{ /*1*/ background: yellow; /*2*/ background: radial-gradient(ellise, yellow, red); } 就以上样式1和2的顺 ...
- c# win form 显示支付宝二维码图片
System.Net.WebClient web = new System.Net.WebClient(); byte[] buffer = web.DownloadData(网络图片的地址); we ...
- Nginx/LVS/HAProxy负载均衡软件的优缺点详解(转)
PS:Nginx/LVS/HAProxy是目前使用最广泛的三种负载均衡软件,本人都在多个项目中实施过,参考了一些资料,结合自己的一些使用经验,总结一下. 一般对负载均衡的使用是随着网站规模的提升根据不 ...
- android学习笔记46——File存储
File存储--IO操作文件 openFileOutput.openFileInput Context提供了如下两个方法来打开本应用程序的数据文件夹里面的文件IO流. 1.FileInputStrea ...
- [dts]Device Tree格式解析
转自:http://blog.csdn.net/airk000/article/details/21345159 目录: 1. 作用 2. 基本数据格式 3. 一些基本概念 4. 工作方式 a. 地址 ...
- HDMI的CEC是如何控制外围互联设备的
1. HDMI CEC算是一个相当庞大的系统,想了解还要从HDMI接口信号啊.物理地址啊.逻辑地址啊等等HDMI基础的东西说起. 2. 不过可以简单的这么理解,在HDMI CEC最小系统里,所有通过H ...
- jQurey基础简介
随着WEB2.0及ajax思想在互联网上的快速发展传播,陆续出现了一些优秀的Js框架,其中比较著名的有Prototype.YUI.jQuery. mootools.Bindows以及国内的JSVM框架 ...