一、目的

把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. 彻底删除mysql服务

    http://wenku.baidu.com/link?url=XEOFkNXElJV6FoBDOs7m7BlDUv9-ZuLbRbeVwbMa7AXa8ukZ6oIpiYYy3gNnITmP911M ...

  2. [HDU 5113] Black And White (dfs+剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5113 题目大意:给你N*M的棋盘,K种颜色,每种颜色有c[i]个(sigma(c[i]) = N*M) ...

  3. GL_INVALID_VALUE(0X501)

    当android应该打开GPU的支持后, 有些手机会出现黑屏.闪屏等现象. 跟踪控制台, 会打印日志GL_INVALID_VALUE(0X501). 参考资料:http://mobile.riaos. ...

  4. [Android疑难杂症]动态改变Background后Padding无效的问题

    前言 在Layout中指定好background和padding以后,程序里面动态修改background之后padding就失效了,貌似是一个BUG,这里找到了一篇英文文章,简单翻译分享一下. 声明 ...

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

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

  6. html标签属性

    clientWidth = width + paddingclientHeight = height + paddingoffsetWidth = width + padding + borderof ...

  7. php dirname(__FILE__) 获取当前文件的绝对路径

    dirname(__FILE__) 取到的是当前文件的绝对路径,也就是说,比起相对路径,查找速度是最快的. 比如当前文件是放在(d:\www\)下,文件名是test.php.  测试的代码如下: &l ...

  8. 【转】交叉编译faac共享库

    转自:http://blog.csdn.net/cjj198561/article/details/38382889 编译准备 1.代码下载 在mac下面执行:wget http://download ...

  9. Orchard官方文档翻译(一) 总览

    原文地址:http://docs.orchardproject.net/ 最近想要学习了解orchard,但却没有找到相关的中文文档,只有英文文档.于是决定自行翻译,以便日后方便翻阅. 转载请注明原作 ...

  10. 解决在 MVC  局部视图中加载 ueditor 编辑器时, 编辑器加载不出的 bug

    在 MVC 局部视图中,有时我们需要 加载 ueditor 编辑器,或进行局部刷新, 但是在加载局部视图后,ueditor 编辑器加载不出,这是由于 ueditor 使用的缓存,只要清空缓存,重新实例 ...