HBase与MapReducer集成官方帮助文档:http://archive.cloudera.com/cdh5/cdh/5/hbase-1.2.0-cdh5.14.0/book.html


需求

在HBase先创建一张表myuser2 —— create 'myuser2','f1',然后读取myuser表中的数据,将myuser表中f1列族name列age列的数据写入到表myuser2中


步骤

一、创建maven工程,导入jar包

<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories> <dependencies> <dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0-mr1-cdh5.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0-cdh5.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.0-cdh5.14.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<!-- <verbal>true</verbal>-->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*/RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

二、开发MapReduce程序

定义一个main类——HbaseReadWrite

package cn.itcast.mr.demo1;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; public class HbaseReadWrite extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
//创建Job对象
Job job = Job.getInstance(super.getConf(), "HbaseMapReduce");
//创建Scan对象,这里如果不设置过滤器,就是全表查询,因为在Mapper类中已经设置了判断条件,所以这里不需要设置过滤器
Scan scan = new Scan(); /**
* 这是自定义Map逻辑的工具类
* 这里需要五个参数:
* tablename 就是 要读取数据的表名
* scan 就是 HBASE 在java代码 实现增删改查时用来设置过滤器,获取数据等的
* 接着就是自己定义的Mapper类,k2和v2的输出类型
* 最后是Job对象
*/
TableMapReduceUtil.initTableMapperJob("myuser",scan,HbaseReadMapper.class, Text.class, Put.class,job); /**
* 这是自定义Reduce逻辑的工具类
* 这里只需要三个参数即可
* tablename 就是要写入数据的表名
* 然后一个自定义的reduce类和job对象
*/
TableMapReduceUtil.initTableReducerJob("myuser2",HbaseWriteReducer.class,job); //提交任务
boolean b = job.waitForCompletion(true); return b?0:1;
} /**
* main方法,负责run的退出
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Configuration configuration = HBaseConfiguration.create();
//一定记得要在configuration中设置zookeeper的地址,否则无法连接
configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
int run = ToolRunner.run(configuration, new HbaseReadWrite(), args);
System.exit(run);
}
}

自定义Mapper逻辑,定义一个Mapper类——HbaseReadMapper

package cn.itcast.mr.demo1;

import org.apache.hadoop.hbase.Cell;
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;
import org.apache.hadoop.io.Text; import java.io.IOException;
import java.util.List; public class HbaseReadMapper extends TableMapper<Text, Put> {
/**
*
* @param key ke2输出类型为Text,因为是rowKey
* @param result v2输出类型为Put,因为Hbase插入数据都是Put对象
* @param context
* @throws IOException
* @throws InterruptedException
*/
@Override
protected void map(ImmutableBytesWritable key, Result result, Context context) throws IOException, InterruptedException {
//获取Hbase表中rowKey的字节
byte[] rowKeyBytes = key.get();
//将rowKey字节转换为字符串,因为k2输出类型为Text
String rowKey = Bytes.toString(rowKeyBytes); //新建Put对象
Put put = new Put(rowKeyBytes);
//获取Hbase所有数据
List<Cell> cells = result.listCells();
//循环遍历到每一条数据
for (Cell cell : cells) {
//获取cell的列族
byte[] family = cell.getFamily();
//获取cell的列
byte[] qualifier = cell.getQualifier();
//判断cell的列族和列值,拿到需要的数据
if ("f1".equals(Bytes.toString(family))){
if ("name".equals(Bytes.toString(qualifier)) || "age".equals(Bytes.toString(qualifier))){
put.add(cell);
}
}
}
//判断Put是否为空
if (!put.isEmpty()){
context.write(new Text(rowKey),put);
}
}
}

自定义Reducer逻辑,定义一个Reducer类——HbaseWriterReduce

package cn.itcast.mr.demo1;

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.Text; import java.io.IOException; public class HbaseWriteReducer extends TableReducer<Text, Put, ImmutableBytesWritable> {
/**
*
* @param key 输入值,k2为Text,也就是rowKey
* @param values 输入值,v2为Put
* @param context
* @throws IOException
* @throws InterruptedException
*/
@Override
protected void reduce(Text key, Iterable<Put> values, Context context) throws IOException, InterruptedException {
// ImmutableBytesWritable是用来封装rowKey的
ImmutableBytesWritable immutableBytesWritable = new ImmutableBytesWritable();
// key就是rowKey
immutableBytesWritable.set(key.getBytes());
// 循环遍历拿到每一个put对象,输出即可
for (Put put : values) {
context.write(immutableBytesWritable,put);
}
}
}

三、运行结果

【HBase】HBase与MapReduce的集成案例的更多相关文章

  1. HBase与Sqoop集成案例

    HBase与Sqoop集成 案例:将RDBMS中的数据抽取到HBase中 Step1.配置sqoop-env.sh如下: Step2.在Mysql中创建一张数据库library,一张表book CRE ...

  2. MapReduce 单词统计案例编程

    MapReduce 单词统计案例编程 一.在Linux环境安装Eclipse软件 1.   解压tar包 下载安装包eclipse-jee-kepler-SR1-linux-gtk-x86_64.ta ...

  3. hadoop笔记之MapReduce的应用案例(利用MapReduce进行排序)

    MapReduce的应用案例(利用MapReduce进行排序) MapReduce的应用案例(利用MapReduce进行排序) 思路: Reduce之后直接进行结果合并 具体样例: 程序名:Sort. ...

  4. hadoop笔记之MapReduce的应用案例(WordCount单词计数)

    MapReduce的应用案例(WordCount单词计数) MapReduce的应用案例(WordCount单词计数) 1. WordCount单词计数 作用: 计算文件中出现每个单词的频数 输入结果 ...

  5. Apache的HBase与cdh的sqoop集成(不建议不同版本之间的集成)

    1.修改sqoop的配资文件 2.从mysql导入到hbase(import) bin/sqoop import \ --connect jdbc:mysql://linux-hadoop3.ibei ...

  6. 基于Hbase数据的Mapreduce程序环境开发

    一.实验目标 编写Mapreduce程序,以Hbase表数据为Map输入源,计算结果输出到HDFS或者Hbase表中. 在非CDH5的Hadoop集群环境中,将编写好的Mapreduce程序整个工程打 ...

  7. hbase自带mapreduce计数表行数功能

    $HBASE_HOME/bin/hbase org.apache.hadoop.hbase.mapreduce.RowCounter ‘tablename’ mapreduce来计数,很快的!!!

  8. Hbase理论&&hbase shell&&python操作hbase&&python通过mapreduce操作hbase

    一.Hbase搭建: 二.理论知识介绍: 1Hbase介绍: Hbase是分布式.面向列的开源数据库(其实准确的说是面向列族).HDFS为Hbase提供可靠的底层数据存储服务,MapReduce为Hb ...

  9. 076 Apache的HBase与cdh的sqoop集成(不建议不同版本之间的集成)

    1.修改sqoop的配资文件 2.从mysql导入到hbase(import) bin/sqoop import \ --connect jdbc:mysql://linux-hadoop3.ibei ...

随机推荐

  1. [一起读源码]走进C#并发队列ConcurrentQueue的内部世界 — .NET Core篇

    在上一篇<走进C#并发队列ConcurrentQueue的内部世界>中解析了Framework下的ConcurrentQueue实现原理,经过抛砖引玉,得到了一众大佬的指点,找到了.NET ...

  2. Oracle NULL值

    NULL值,用来描述记录中没有定义内容的字段值.在Oracle中,判断某个条件的值时,返回值可能是TRUE.FALSE或UNKNOWN. 如果查询一个列的值是否等于20,而该列的值为NULL,那么就是 ...

  3. 如何把Excel表暴力拆分了,python两段代码帮你搞定

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:老方玩编程 PS:如有需要Python学习资料的小伙伴可以加点击下方 ...

  4. 统计子串数量,Python基础

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:陈YL PS:如有需要Python学习资料的小伙伴可以加点击下方链接自 ...

  5. E - Roaming Atcoder

    题解:https://blog.csdn.net/qq_40655981/article/details/104459253 题目大意:n个房间,,每个房间都有一个人,一共k天,在一天,一个人可以到任 ...

  6. Anadi and Domino--codeforces div2

    题目链接:https://codeforces.com/contest/1230/problem/C 题目大意:21枚多米诺牌,给你一个图,将多米诺牌放到图的边上,由同一个点发出的所有边,边上多米诺牌 ...

  7. Linux学习笔记(二)文件操作命令

    文件操作命令 touch stat cat more less head tail ln touch 英文原意: change file timestamps 功能: 修改文件的时间戳 语法: tou ...

  8. 14.移动端图片浏览组件 react-wx-images-viewer

    安装 npm install --save react-wx-images-viewer 使用 import WxImageViewer from 'react-wx-images-viewer'; ...

  9. AI-web-1靶机过关记录

    靶机地址:172.16.1.195 Kali地址:172.16.1.107 1.信息收集 端口扫描: 目录扫描: 发现robots.txt敏感文件,查看 存在/m3diNf0/,/se3reTdir7 ...

  10. pytorch seq2seq闲聊机器人beam search返回结果

    decoder.py """ 实现解码器 """ import heapq import torch.nn as nn import con ...