一、目的

把hbase中某张表的数据导出到hdfs上一份。

实现方式这里介绍两种:一种是自己写mr程序来完成,一种是使用hbase提供的类来完成。

二、自定义mr程序将hbase数据导出到hdfs上

2.1首先看看hbase中t1表中的数据:

2.2mr的代码如下:

比较重要的语句是

job.setNumReduceTasks(0);//为什么要设置reduce的数量是0呢?读者可以自己考虑下
TableMapReduceUtil.initTableMapperJob(args[0], new Scan(),HBaseToHdfsMapper.class ,Text.class, Text.class, job);//这行语句指定了mr的输入是hbase的哪张表,scan可以对这个表进行filter操作。

public class HBaseToHdfs {
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
Job job = Job.getInstance(conf, HBaseToHdfs.class.getSimpleName());
job.setJarByClass(HBaseToHdfs.class); job.setMapperClass(HBaseToHdfsMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setNumReduceTasks(0); TableMapReduceUtil.initTableMapperJob(args[0], new Scan(),HBaseToHdfsMapper.class ,Text.class, Text.class, job);
//TableMapReduceUtil.addDependencyJars(job); job.setOutputFormatClass(TextOutputFormat.class);
FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true);
} public static class HBaseToHdfsMapper extends TableMapper<Text, Text> {
private Text outKey = new Text();
private Text outValue = new Text();
@Override
protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
//key在这里就是hbase的rowkey
byte[] name = null;
byte[] age = null;
byte[] gender = null;
byte[] birthday = null;
try {
name = value.getColumnLatestCell("f1".getBytes(), "name".getBytes()).getValue();
} catch (Exception e) {}
try {
age = value.getColumnLatestCell("f1".getBytes(), "age".getBytes()).getValue();
} catch (Exception e) {}
try {
gender = value.getColumnLatestCell("f1".getBytes(), "gender".getBytes()).getValue();
} catch (Exception e) {}
try {
birthday = value.getColumnLatestCell("f1".getBytes(), "birthday".getBytes()).getValue();
} catch (Exception e) {}
outKey.set(key.get());
String temp = ((name==null || name.length==0)?"NULL":new String(name)) + "\t" + ((age==null || age.length==0)?"NULL":new String(age)) + "\t" + ((gender==null||gender.length==0)?"NULL":new String(gender)) + "\t" + ((birthday==null||birthday.length==0)?"NULL":new String(birthday));
System.out.println(temp);
outValue.set(temp);
context.write(outKey, outValue);
} }
}

2.3打包执行

hadoop jar hbaseToDfs.jar com.lanyun.hadoop2.HBaseToHdfs t1 /t1

2.4查看hdfs上的文件

(my_python_env)[root@hadoop26 ~]# hadoop fs -cat /t1/part*
zhangsan male NULL
lisi NULL NULL NULL
wangwu NULL NULL NULL
zhaoliu NULL NULL

至此,导出成功

三、使用hbase自带的工具进行导出

hbase自带的工具是:org.apache.hadoop.hbase.mapreduce.Export
3.1如何使用这个工具呢?查看帮助信息

(my_python_env)[root@hadoop26 ~]# hbase org.apache.hadoop.hbase.mapreduce.Export
ERROR: Wrong number of arguments:
Usage: Export [-D <property=value>]* <tablename> <outputdir> [<versions> [<starttime> [<endtime>]] [^[regex pattern] or [Prefix] to filter]]

3.2使用工具来导出

hbase org.apache.hadoop.hbase.mapreduce.Export t1 /t2

至此已经完成导出。

HBase数据导出到HDFS的更多相关文章

  1. 用mapreduce实现将mysql数据导出到HDFS上

    因为业务需要,需要将一批mysql数据导入到HBASE,现在先将数据从Mysql导出到HDFS. 版本:hadoop CDH4.5,Hbase-0.946 1.实体类 YqBean 是我的实体类,请根 ...

  2. hbase数据导出和恢复 设置双master + 查看hbase表占用磁盘大小

    1.备份TETST111hbase org.apache.hadoop.hbase.mapreduce.Export TEST111 /do1/hh2.drop 掉test111表  -- 只能dro ...

  3. HBase数据的导入和导出

    查阅了几篇中英文资料,发现有的地方说的不是很全部,总结在此,共有两种命令行的方式来实现数据的导入导出功能,即备份和还原. 1 HBase本身提供的接口 其调用形式为: 1)导入 ./hbase org ...

  4. HBase数据导入导出工具

    hbase中自带一些数据导入.导出工具 1. ImportTsv直接导入 1.1 hbase中建表 create 'testtable4','cf1','cf2' 1.2 准备数据文件data.txt ...

  5. Hive及HBase数据迁移

    一. Hive数据迁移 场景:两个Hadoop平台集群之间Hive表迁移. 基本思路:Hive表元数据和文件数据export到HDFS文件,通过Distcp将HDFS迁移到另一个集群的HDFS文件,再 ...

  6. Hive三种不同的数据导出的方式

    转自:http://blog.chinaunix.net/uid-27177626-id-4653808.html Hive三种不同的数据导出的方式,根据导出的地方不一样,将这些方法分为三类:(1)导 ...

  7. hive的数据导入与数据导出:(本地,云hdfs,hbase),列分隔符的设置,以及hdfs上传给pig如何处理

    hive表的数据源有四种: hbase hdfs 本地 其他hive表 而hive表本身有两种: 内部表和外部表. 而hbase的数据在hive中,可以建立对应的外部表(参看hive和hbase整合) ...

  8. Hbase数据导入导出

    平时用于从生产环境hbase到导出数据到测试环境. 导入数据: import java.io.BufferedReader; import java.io.File; import java.io.F ...

  9. HBase 学习之一 <<HBase使用客户端API动态创建Hbase数据表并在Hbase下导出执行>>

    HBase使用客户端API动态创建Hbase数据表并在Hbase下导出执行                       ----首先感谢网络能够给我提供一个开放的学习平台,如果没有网上的技术爱好者提供 ...

随机推荐

  1. Celery 使用简介

    转自:http://liuzxc.github.io/blog/celery/ Celery 是一个简单.灵活且可靠的,处理大量消息的分布式系统,它是一个专注于实时处理的任务队列, 同时也支持任务调度 ...

  2. Flask-SQLAlchemy 学习总结

    初始化和配置 ORM(Object Relational Mapper) 对象关系映射.指将面对对象得方法映射到数据库中的关系对象中.Flask-SQLAlchemy是一个Flask扩展,能够支持多种 ...

  3. (easy)LeetCode 263.Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  4. Python访问私有变量

    代码: class Counter(object): __secount=0 publicfs=0 def getcount(self): self.__secount+=1 self.publicf ...

  5. (转)WebApi自动生成在线文档WebApiTestClient

    原文链接:http://www.cnblogs.com/landeanfen/p/5210356.html 前言:这两天在整WebApi的服务,由于调用方是Android客户端,Android开发人员 ...

  6. mysql 使用说明-1

    以下内容是根据官方文档翻译的,执行截图是博主自己实测结果. 3.1 Connecting to and Disconnecting from the Server 连接,断开数据库 要连接到mysql ...

  7. C语言sizeof陷阱

    执行以下程序,查看输出: #include <stdio.h> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) int  ...

  8. json 和 pickel 详解

    一.json json,用于字符串 和 python数据类型间进行转换 Json模块提供了四个功能:dumps.dump.loads.load 1.json.loads()用于将字符串形式的字典,列表 ...

  9. c# 清空txt文本文件的值

    FileStream fs1 = null; try { fs1 = new FileStream(@"C:\db.txt", FileMode.Truncate, FileAcc ...

  10. hadoop fs管理文件权限

    sudo addgroup Hadoop#添加一个hadoop组sudo usermod -a -G hadoop larry#将当前用户加入到hadoop组 修改hadoop目录的权限sudo ch ...