HBASE--MapReduce
1、查看 HBase 的 MapReduce 任务的执行
$ bin/hbase mapredcp
2、执行环境变量的导入
$ export HBASE_HOME= ~/hadoop_home/hbase-1.2.6
$ export HADOOP_HOME= ~/hadoop_home
$ export HADOOP_CLASSPATH=`${HBASE_HOME}/bin/hbase mapredcp`
本句的环境变量可以不用加入 ~/.profile里面
不过要在哪个shell里执行哪个才能用jar包
3) 运行官方的 MapReduce 任务
-- 案例一:统计 Student 表中有多少行数据
cd /home/hadoop/hadoop_home/hbase-1.2.6
(文件夹路径环境变量,只是使用没有配置,所以进入此路径下)
$ yarn jar lib/hbase-server-1.2.6.jar rowcounter student
案例二:使用 MapReduce 将本地数据导入到 HBase
(1) 在本地创建一个 tsv 格式的文件:fruit.tsv
1001 Apple Red
1002 Pear Yellow
1003 PineappleYellow
尖叫提示:上面的这个数据不要从文中直接复制,有格式错误
(2)
创建 HBase 表
hbase(main):001:0> create 'fruit','info
(3) 在 HDFS 中创建 input_fruit 文件夹并上传 fruit.tsv 文件
$ hdfs dfs -mkdir /input_fruit/
$ hdfs dfs -put fruit.tsv /input_fruit/
(4) 执行 MapReduce 到 HBase 的 fruit 表中
cd /home/hadoop/hadoop_home/hbase-1.2.6
$ yarn jar lib/hbase-server-1.2.6.jar importtsv
-Dimporttsv.columns=HBASE_ROW_KEY,info:name,info:color fruit
hdfs://master:9000/input_fruit
(5) 使用 scan 命令查看导入后的结果
hbase(main):001:0> scan ‘fruit’
2.5.2、自定义 HBase-MapReduce1
目标:将 fruit 表中的一部分数据,通过 MR 迁入到 fruit_mr 表中。
分步实现:
1) 构建 ReadFruitMapper 类,用于读取 fruit 表中的数据
package com.yjsj.hbase_mr;
import java.io.IOException;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
public class ReadFruitMapper extends TableMapper<ImmutableBytesWritable, Put> {
@Override
protected void map(ImmutableBytesWritable key, Result value, Context context)
throws IOException, InterruptedException {
//将 fruit 的 name 和 color 提取出来,相当于将每一行数据读取出来放入到 Put 对象中。
Put put = new Put(key.get());
//遍历添加 column 行
for(Cell cell: value.rawCells()){
//添加/克隆列族:info
if("info".equals(Bytes.toString(CellUtil.cloneFamily(cell)))){
//添加/克隆列:name
if("name".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))){
//将该列 cell 加入到 put 对象中
put.add(cell);
//添加/克隆列:color
}else if("color".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))){
//向该列 cell 加入到 put 对象中
put.add(cell);
}
}
}
//将从 fruit 读取到的每行数据写入到 context 中作为 map 的输出
context.write(key, put);
}
}
2) 构建 WriteFruitMRReducer 类,用于将读取到的 fruit 表中的数据写入到 fruit_mr 表中
package com.yjsj.hbase_mr;
import java.io.IOException;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.NullWritable;
public class WriteFruitMRReducer extends TableReducer<ImmutableBytesWritable, Put,
NullWritable> {
@Override
protected void reduce(ImmutableBytesWritable key, Iterable<Put> values, Context context)
throws IOException, InterruptedException {
//读出来的每一行数据写入到 fruit_mr 表中
for(Put put: values){
context.write(NullWritable.get(), put);
}
}
}
3) 构建 Fruit2FruitMRRunner extends Configured implements Tool 用于组装运行 Job
任务
class Txt2FruitRunner extends Configured implements Tool {
//组装 Job
public int run(String[] args) throws Exception {
//得到 Configuration
Configuration conf = this.getConf();
//创建 Job 任务
Job job = Job.getInstance(conf, this.getClass().getSimpleName());
job.setJarByClass(Fruit2FruitMRRunner.class);
//配置 Job
Scan scan = new Scan();
scan.setCacheBlocks(false);
scan.setCaching(500);
//设置 Mapper,注意导入的是 mapreduce 包下的,不是 mapred 包下的,后者是老
版本
TableMapReduceUtil.initTableMapperJob(
"fruit", //数据源的表名
scan, //scan 扫描控制器
ReadFruitMapper.class,//设置 Mapper 类
ImmutableBytesWritable.class,//设置 Mapper 输出 key 类型
Put.class,//设置 Mapper 输出 value 值类型
job//设置给哪个 JOB
);
//设置 Reducer
TableMapReduceUtil.initTableReducerJob("fruit_mr", WriteFruitMRReducer.class,
job);
//设置 Reduce 数量,最少 1 个
job.setNumReduceTasks(1);
boolean isSuccess = job.waitForCompletion(true);
if(!isSuccess){
throw new IOException("Job running with error");
}
return isSuccess ? 0 : 1;
}
4) 主函数中调用运行该 Job 任务
public static void main( String[] args ) throws Exception{
Configuration conf = HBaseConfiguration.create();
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master,node1,node2");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.master", "master:60000");
int status = ToolRunner.run(conf, new Fruit2FruitMRRunner(), args);
System.exit(status);
}
注:加入zookeeper代码
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master,node1,node2");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.master", "master:60000");
5) 打包运行任务(参考java打包,而且打包需要zip 格式一下)
$ ~/hadoop_home/bin/yarn jar ~/hadoop_home/TestHbase.jar
com.yjsj.hbase.mr1.Fruit2FruitMRRunner
尖叫提示:运行任务前,如果待数据导入的表不存在,则需要提前创建之。
尖叫提示:maven 打包命令:-P local clean package 或-P dev clean package install(将第三方
jar 包一同打包,需要插件:maven-shade-plugin)
2.5.3、自定义HBase-MapReduce2
目标:实现将 HDFS 中的数据写入到 HBase 表中。
分步实现:
1) 构建 ReadFruitFromHDFSMapper 于读取 HDFS 中的文件数据
package com.yjsj.hbase_mr2;
import java.io.IOException;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class ReadFruitFromHDFSMapper extends Mapper<LongWritable, Text,
ImmutableBytesWritable, Put> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException,
InterruptedException {
//从 HDFS 中读取的数据
String lineValue = value.toString();
//读取出来的每行数据使用\t 进行分割,存于 String 数组
String[] values = lineValue.split("\t");
//根据数据中值的含义取值
String rowKey = values[0];
String name = values[1];
String color = values[2];
//初始化 rowKey
ImmutableBytesWritable rowKeyWritable = new
ImmutableBytesWritable(Bytes.toBytes(rowKey));
//初始化 put 对象
Put put = new Put(Bytes.toBytes(rowKey));
//参数分别:列族、列、值
put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes(name));
put.add(Bytes.toBytes("info"), Bytes.toBytes("color"), Bytes.toBytes(color));
context.write(rowKeyWritable, put);
}
}
2) 构建 WriteFruitMRFromTxtReducer 类
package com.yjsj.hbase_mr2;
import java.io.IOException;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.NullWritable;
public class WriteFruitMRFromTxtReducer extends TableReducer<ImmutableBytesWritable, Put,
NullWritable> {
@Override
protected void reduce(ImmutableBytesWritable key, Iterable<Put> values, Context context)
throws IOException, InterruptedException {
//读出来的每一行数据写入到 fruit_hdfs 表中
for(Put put: values){
context.write(NullWritable.get(), put);
}
}
}
3) 创建 Txt2FruitRunner 组装 Job
class Txt2FruitRunner extends Configured implements Tool {
public int run(String[] args) throws Exception {
//得到 Configuration
Configuration conf = this.getConf();
//创建 Job 任务
Job job = Job.getInstance(conf, this.getClass().getSimpleName());
job.setJarByClass(Txt2FruitRunner.class);
Path inPath = new Path("hdfs://master:9000/input_fruit/fruit.tsv");
FileInputFormat.addInputPath(job, inPath);
//设置 Mapper
job.setMapperClass(ReadFruitFromHDFSMapper.class);
job.setMapOutputKeyClass(ImmutableBytesWritable.class);
job.setMapOutputValueClass(Put.class);
//设置 Reducer
TableMapReduceUtil.initTableReducerJob("fruit_hdfs", WriteFruitMRFromTxtReducer.class, job);
//设置 Reduce 数量,最少 1 个
job.setNumReduceTasks(1);
boolean isSuccess = job.waitForCompletion(true);
if(!isSuccess){
throw new IOException("Job running with error");
}
return isSuccess ? 0 : 1;
}
4) 调用执行 Job
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master,node1,node2");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.master", "master:60000");
int status = ToolRunner.run(conf, new Txt2FruitRunner(), args);
System.exit(status);
}
注:
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master,node1,node2");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.master", "master:60000");
5) 打包运行(参考java打包,而且打包需要zip 格式一下)
$ ~/hadoop_home/bin/yarn jar ~/softwares/jars/hbase-0.0.1-SNAPSHOT.jar
com.z.hbase.mr2.Txt2FruitRunner
尖叫提示:运行任务前,如果待数据导入的表不存在,则需要提前创建之。
尖叫提示:maven 打包命令:-P local clean package 或-P dev clean package install(将第三方
jar 包一同打包,需要插件:maven-shade-plugin)
2.6、与 Hive 的集成
2.6.1、HBase 与 Hive 的对比
1) Hive
(1) 数据仓库
Hive 的本质其实就相当于将 HDFS 中已经存储的文件在 Mysql 中做了一个双射关系,以方
便使用 HQL 去管理查询。
(2) 用于数据分析、清洗
Hive 适用于离线的数据分析和清洗,延迟较高。
(3) 基于 HDFS、MapReduce
Hive 存储的数据依旧在 DataNode 上,编写的 HQL 语句终将是转换为 MapReduce 代码执行。
2) HBase
(1) 数据库
是一种面向列存储的非关系型数据库。
(2) 用于存储结构化和非结构话的数据 适用于单表非关系型数据的存储,不适合做关联查
(3) 基于 HDFS
数据持久化存储的体现形式是 Hfile,存放于 DataNode 中,被 ResionServer 以 region 的形式
进行管理。
(4) 延迟较低,接入在线业务使用
面对大量的企业数据,HBase 可以直线单表大量数据的存储,同时提供了高效的数据访问速
度。
2.6.2、HBase 与 Hive 集成使用
尖叫提示:HBase 与 Hive 的集成在最新的两个版本中无法兼容。所以,我们只能含着泪勇
敢的重新编译:hive-hbase-handler-1.2.2.jar!!好气!!
环境准备
因为我们后续可能会在操作 Hive 的同时对 HBase 也会产生影响,所以 Hive 需要持有操作
HBase 的 Jar,那么接下来拷贝 Hive 所依赖的 Jar 包(或者使用软连接的形式)。
HBASE--MapReduce的更多相关文章
- 【Hbase学习之五】HBase MapReduce
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-2.6.5 hbase-0.98.12.1-h ...
- HBase MapReduce 一些 ClassNotFoundException 所缺少的jar包
我们在用 java 操作 HBase 时,可能会出现相关的 ClassNotFoundException 等异常信息,但是我们又不想把 HBase lib 下的所有jar包全部导入到工程,因为会有 ...
- org/apache/hadoop/hbase/mapreduce/TableReducer:Unsupported major.minor version52.0
问题详情: 问题原因: <dependency> <groupId>org.apache.hbase</groupId> <artifactId& ...
- Mapreduce的文件和hbase共同输入
Mapreduce的文件和hbase共同输入 package duogemap; import java.io.IOException; import org.apache.hadoop.co ...
- HBase with MapReduce (MultiTable Read)
hbase当中没有两表联查的操作,要实现两表联查或者在查询一个表的同时也需要访问另外一张表的时候,可以通过mapreduce的方式来实现,实现方式如下:由于查询是map过程,因此这个过程不需要设计re ...
- HBase with MapReduce (SummaryToFile)
上一篇文章是实现统计hbase单元值出现的个数,并将结果存放到hbase的表中,本文是将结果存放到hdfs上.其中的map实现与前文一直,连接:http://www.cnblogs.com/ljy20 ...
- HBase with MapReduce (Summary)
我们知道,hbase没有像关系型的数据库拥有强大的查询功能和统计功能,本文实现了如何利用mapreduce来统计hbase中单元值出现的个数,并将结果携带目标的表中, (1)mapper的实现 pac ...
- HBase with MapReduce (Read and Write)
上面一篇文章仅仅是介绍如何通过mapReduce来对HBase进行读的过程,下面将要介绍的是利用mapreduce进行读写的过程,前面我们已经知道map实际上是读过程,reduce是写的过程,然而ma ...
- HBase with MapReduce (Only Read)
最近在学习HBase,在看到了如何使用Mapreduce来操作Hbase,下面将几种情况介绍一下,具体的都可以参照官网上的文档说明.官网文档连接:http://hbase.apache.org/boo ...
- hbase与mapreduce集成
一:运行给定的案例 1.获取jar包里的方法 2.运行hbase自带的mapreduce程序 lib/hbase-server-0.98.6-hadoop2.jar 3.具体运行 4.运行一个小方法 ...
随机推荐
- 阅读《名师讲坛--Android开发实战经典》
一,专心,快速阅读一本书,直到深入理解,把书读厚,再读薄,你定会有收获. 二,20171214开始阅读<名师讲坛--Android开发实战经典>,但愿自己有所收获.从今天开始养成刻录学习写 ...
- 将自己数据转化为cifar10支持的lmdb
大家都知道,在caffe里面,要运行cifar10的例子就得先由cifar10的数据库.由于caffe为了提高运行效率,减少磁盘寻道时间等,统一了数据接口(lmdb,leveldb). 首先,看一下c ...
- leetcode hashmap
187. Repeated DNA Sequences 求重复的DNA序列 public List<String> findRepeatedDnaSequences(String s) { ...
- 2、keys相关命令
redis的官网http://redis.io是学习redis的重要资源库,所有命令都分门别类的罗列在了这里http://redis.io/commands. 1.数据库选择命令: SELECT in ...
- [转] 从数据库中读取图片并导入Excel文件,C#方式
原文地址, 作者 Lvyou1980 直接源码吧. using System; using System.IO; using System.Data; using System.Drawing; us ...
- [转] C#2010 在TreeView控件下显示路径下所有文件和文件夹
原文 张丹-小桥流水,C#2010 在TreeView控件下显示路径下所有文件和文件夹 C#2010学习过程中有所收获,便总结下来,希望能给和我一样在学习遇到困难的同学提供参考. 本文主要介绍两个自定 ...
- [转] C#实现在Sql Server中存储和读取Word文件 (Not Correct Modified)
出处 C#实现在Sql Server中存储和读取Word文件 要实现在Sql Server中实现将文件读写Word文件,需要在要存取的表中添加Image类型的列,示例表结构为: CREATE TABL ...
- Console2支援中文顯示的正式設定法
1.用regedit找到HKEY_CURRENT_USER\Console,把底下的Console2 command window機碼給砍了.2.Console2的View功能表中,有個Console ...
- effective javascript 学习心得
第2条:理解JavaScript的浮点数 1.js数字只有一种类型 2.见代码 /** * Created by Administrator on 2017/7/2. */ console.log(& ...
- C语言之单链表的使用
#include <stdio.h> #include <stdlib.h> typedef struct LNode{ int data; struct LNode *nex ...