6. HBase 与 MapReduce 集成

6.1 官方 HBase 与 MapReduce 集成

  1. 查看 HBase 的 MapReduce 任务的执行:bin/hbase mapredcp;
  2. 环境变量的导入
    1. 临时生效,在命令行执行操作:

      • export HBASE_HOME=/opt/module/hbase-1.3.4;
      • export HADOOP_HOME=/opt/module/hadoop-2.8.5;
      • export HADOOP_CLASSPATH=${HBASE_HOME}/bin/hbase mapredcp;
    2. 永久生效,在/etc/profile配置
      • export HBASE_HOME=/opt/module/hbase-1.3.4;
      • export HADOOP_HOME=/opt/module/hadoop-2.8.5;
      • 并在hadoop-env.sh配置:export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:/opt/module/hbase/lib/*
  3. 运行官方的 MapReduce 任务
// ===== 案例一:统计Student表中有多少行数据 (`opt/module/hbase-1.3.4/` 目录下)
/opt/module/hadoop-2.8.5/bin/yarn jar ./lib/hbase-server-1.3.4.jar rowcounter student // ===== 案例二:使用 MapReduce 将本地数据导入到 HBASE
// 1. 本地创建一个fruit.tsv文件
1001 Apple Red
1002 Pear Yellow
1003 Pineapple Yellow // 2. 创建 HBase 表
create 'fruit','info' // 3. 在 HDFS 中创建 input_fruit 文件夹并上传 fruit.tsv 文件
/opt/module/hadoop-2.8.5/bin/hdfs dfs -mkdir /input_fruit
/opt/module/hadoop-2.8.5/bin/hdfs dfs -put fruit.tsv /input_fruit/ // 4. 执行 MapReduce, 将 fruit.tsv 导入到 HBase 的 fruit 表中
/opt/module/hadoop-2.8.5/bin/yarn jar ./lib/hbase-server-1.3.4.jar importtsv -Dimporttsv.columns=HBASE_ROW_KEY,info:name,info:color fruit hdfs://IP地址/input_fruit

6.2 自定义HBase-MapReduce

  • 需求:将 fruit 表中的部分数据,通过MR迁入到 fruit_mr 表中
// 1. 创建 FruitMapper 类,用于读取 fruit 表中的数据
public class FruitMapper extends TableMapper<ImmutableBytesWritable, Put>{ @Override
protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
// 创建put对象
Put put = new Put(key.get()); Cell[] cells = value.rawCells(); for(Cell cell : cells) {
if("name".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))) {
put.add(cell);
}
} context.write(key, put);
}
} // 2. 创建 FruitReducer 类,用于写入
public class FruitReducer extends TableReducer<ImmutableBytesWritable, Put, NullWritable>{ @Override
protected void reduce(ImmutableBytesWritable key, Iterable<Put> values, Context context) throws IOException, InterruptedException {
for (Put value : values) {
context.write(NullWritable.get(), value);
}
}
} // 3. 创建 FruitDriver 类,用于执行 mapper 和 reducer
public class FruitDriver extends Configuration implements Tool{ private Configuration configuration = null; @Override
public void setConf(Configuration conf) {
this.configuration = conf;
} @Override
public Configuration getConf() {
return configuration;
} @Override
public int run(String[] args) throws Exception {
// 获取任务对象
Job job = Job.getInstance(configuration); // 指定 Driver类
job.setJarByClass(FruitDriver.class); // 指定 Mapper
TableMapReduceUtil.initTableMapperJob("fruit", new Scan(), FruitMapper.class, ImmutableBytesWritable.class, Put.class, job); // 指定 Reducer
TableMapReduceUtil.initTableReducerJob("fruit_mr", FruitReducer.class, job); // 提交
boolean result = job.waitForCompletion(true); return result ? 0 : 1;
} public static void main(String[] args) throws Exception { Configuration configuration = HBaseConfiguration.create();
ToolRunner.run(configuration, new FruitDriver(), args);
}
} // 4. 打成 fruit.jar包
// 5. HBase 中创建 fruit_mr 表
create 'fruit_mr','info' // 6. 在 /opt/module/hbase 中执行:
/opt/module/hadoop-2.8.5/bin/yarn jar ./fruit.jar com.noodles.mr1.FruitDriver(Driver的类名)

6.3 自定义 HBase-MapReduce2

  • 需求:实现将 HDFS 中的数据写入到 HBase 表中
// 1. 创建 Mapper, 用于读取 HDFS 上的文件
public class HDFSMapper extends Mapper<LongWritable, Text, NullWritable, Put>{ @Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, NullWritable, Put>.Context context)
throws IOException, InterruptedException {
// 获取一行数据
String line = value.toString(); // 切割
String[] split = line.split("\t"); // 封装 Put 对象
Put put = new Put(Bytes.toBytes(split[0]));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes(split[1]));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("color"), Bytes.toBytes(split[2])); // 写出去
context.write(NullWritable.get(), put);
}
} // 2. 创建 Reducer, 用于写入
public class HDFSReducer extends TableReducer<NullWritable, Put, NullWritable>{ @Override
protected void reduce(NullWritable key, Iterable<Put> values,
Reducer<NullWritable, Put, NullWritable, Mutation>.Context context) throws IOException, InterruptedException { // 写出数据
for(Put value : values) {
context.write(NullWritable.get(), value);
}
}
} // 3. 创建Driver
public class HDFSDriver extends Configuration implements Tool{ private Configuration configuration = null; @Override
public void setConf(Configuration conf) {
this.configuration = conf;
} @Override
public Configuration getConf() {
return configuration;
} @Override
public int run(String[] args) throws Exception { // 获取 Job 对象
Job job = Job.getInstance(configuration); // 设置主类
job.setJarByClass(HDFSDriver.class); // 设置 Mapper
job.setMapperClass(HDFSMapper.class);
job.setMapOutputKeyClass(NullWritable.class);
job.setMapOutputValueClass(Put.class); // 设置 Reducer
TableMapReduceUtil.initTableReducerJob("fruit2", HDFSReducer.class, job); // 设置输入路径
// import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
FileInputFormat.setInputPaths(job, args[0]); // 提交
boolean result = job.waitForCompletion(true); return result ? 0 : 1;
} public static void main(String[] args) throws Exception { Configuration configuration = HBaseConfiguration.create();
ToolRunner.run(configuration, new HDFSDriver(), args); }
} // 4. 打成 fruit.jar包
// 5. HBase 中创建 fruit2 表
create 'fruit2','info' // 6. 在 /opt/module/hbase 中执行:
/opt/module/hadoop-2.8.5/bin/yarn jar ./fruit.jar com.noodles.mr2.HDFSDriver(Driver的类名) /input_fruit/fruit.tsv(文件路径)

HBase 与 MapReduce 集成的更多相关文章

  1. HBase概念学习(七)HBase与Mapreduce集成

    这篇文章是看了HBase权威指南之后,依据上面的解说搬下来的样例,可是略微有些不一样. HBase与mapreduce的集成无非就是mapreduce作业以HBase表作为输入,或者作为输出,也或者作 ...

  2. hbase与mapreduce集成

    一:运行给定的案例 1.获取jar包里的方法 2.运行hbase自带的mapreduce程序 lib/hbase-server-0.98.6-hadoop2.jar 3.具体运行 4.运行一个小方法 ...

  3. 074 hbase与mapreduce集成

    一:运行给定的案例 1.获取jar包里的方法 2.运行hbase自带的mapreduce程序 lib/hbase-server-0.98.6-hadoop2.jar 3.具体运行 注意命令:mapre ...

  4. 【HBase】HBase与MapReduce集成——从HDFS的文件读取数据到HBase

    目录 需求 步骤 一.创建maven工程,导入jar包 二.开发MapReduce程序 三.结果 需求 将HDFS路径 /hbase/input/user.txt 文件的内容读取并写入到HBase 表 ...

  5. hbase运行mapreduce设置及基本数据加载方法

    hbase与mapreduce集成后,运行mapreduce程序,同时需要mapreduce jar和hbase jar文件的支持,这时我们需要通过特殊设置使任务可以同时读取到hadoop jar和h ...

  6. 【HBase】HBase与MapReduce的集成案例

    目录 需求 步骤 一.创建maven工程,导入jar包 二.开发MapReduce程序 三.运行结果 HBase与MapReducer集成官方帮助文档:http://archive.cloudera. ...

  7. 大数据技术之_11_HBase学习_02_HBase API 操作 + HBase 与 Hive 集成 + HBase 优化

    第6章 HBase API 操作6.1 环境准备6.2 HBase API6.2.1 判断表是否存在6.2.2 抽取获取 Configuration.Connection.Admin 对象的方法以及关 ...

  8. Hbase与hive集成与对比

    HBase与Hive的对比 1.Hive (1) 数据仓库 Hive的本质其实就相当于将HDFS中已经存储的文件在Mysql中做了一个双射关系,以方便使用HQL去管理查询. (2) 用于数据分析.清洗 ...

  9. 《OD大数据实战》HBase整合MapReduce和Hive

    一.HBase整合MapReduce环境搭建 1. 搭建步骤1)在etc/hadoop目录中创建hbase-site.xml的软连接.在真正的集群环境中的时候,hadoop运行mapreduce会通过 ...

随机推荐

  1. SSM ehcache 配置 mapper 文件出错

    异常 十二月 26, 2017 1:44:49 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertie ...

  2. Maven 异常 druid jar冲突

    异常: 十二月 25, 2017 11:04:41 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropert ...

  3. Pytest权威教程-更改标准(Python)测试发现

    目录 更改标准(Python)测试发现 在测试收集过程中忽略路径 测试期间收集的测试取消 保留从命令行指定的重复路径 更改目录递归 更改命名约定 将cmdline参数解释为Python包 找出收集的东 ...

  4. Iptables 之二扩展模块 nat

    问题一:如果开发被动模式的ftp服务? 21号端口是命令连接端口,数据连接端口不固定 三步骤: (1)装载ftp追踪时的专用的模块 /lib/modules/$(uname-r)/kernel/ker ...

  5. P5049 旅行(数据加强版)(基环树)

    做法 把环找出来,如果在环上(u,v)两点的时候,u的其他子树都走完了,v上第一个还有除v存在的子树没走完的 祖先,祖先的最小子节点小于v,则回去 Code #include<bits/stdc ...

  6. SweetAlert 2 全网最详细的使用方法

    官网链接 SweetAlert2 官网链接 准备阶段 CDN js 如果该 链接 时间久远了 , 可以在官网去找找最新的 可以把 js 复制出来 自己新建一个文件 然后 引用到 html 中 1. 带 ...

  7. 值得H5前端学习的60个JS插件(含DEMO演示)

    下面也可以说是H5前端学习的js插件大全.基本包含了大部分的前端最前沿的js插件和库. 布局 SuperEmbed.js- 是一个Javascript库,可检测出网页上的内嵌视频并使他们能够变成响应式 ...

  8. spark学习记录-1

    mapreduce的限制 适合“一趟”计算操作 很难组合和嵌套操作符号 无法表示迭代操作 ======== 由于复制.序列化和磁盘IO导致mapreduce慢 复杂的应用.流计算.内部查询都因为map ...

  9. MAC 上抓取网页数据的工具有哪些?

    我希望能够从网页上, 比如气象局数据, 财经数据等等, 我看到官方提供的数据都比较混乱, 有的是一个php文件, 有的是一个文本, 有的干脆不提供数据, 我想问, Mac上, 用什么工具去抓数据, 以 ...

  10. 第2课第2节_Java面向对象编程_封装性_P【学习笔记】

    摘要:韦东山android视频学习笔记  面向对象程序的三大特性之封装性:把属性和方法封装在一个整体,同时添加权限访问. 1.封装性的简单程序如下,看一下第19行,如果我们不对age变量进行权限的管控 ...