HBase数据导出到HDFS
一、目的
把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的更多相关文章
- 用mapreduce实现将mysql数据导出到HDFS上
因为业务需要,需要将一批mysql数据导入到HBASE,现在先将数据从Mysql导出到HDFS. 版本:hadoop CDH4.5,Hbase-0.946 1.实体类 YqBean 是我的实体类,请根 ...
- hbase数据导出和恢复 设置双master + 查看hbase表占用磁盘大小
1.备份TETST111hbase org.apache.hadoop.hbase.mapreduce.Export TEST111 /do1/hh2.drop 掉test111表 -- 只能dro ...
- HBase数据的导入和导出
查阅了几篇中英文资料,发现有的地方说的不是很全部,总结在此,共有两种命令行的方式来实现数据的导入导出功能,即备份和还原. 1 HBase本身提供的接口 其调用形式为: 1)导入 ./hbase org ...
- HBase数据导入导出工具
hbase中自带一些数据导入.导出工具 1. ImportTsv直接导入 1.1 hbase中建表 create 'testtable4','cf1','cf2' 1.2 准备数据文件data.txt ...
- Hive及HBase数据迁移
一. Hive数据迁移 场景:两个Hadoop平台集群之间Hive表迁移. 基本思路:Hive表元数据和文件数据export到HDFS文件,通过Distcp将HDFS迁移到另一个集群的HDFS文件,再 ...
- Hive三种不同的数据导出的方式
转自:http://blog.chinaunix.net/uid-27177626-id-4653808.html Hive三种不同的数据导出的方式,根据导出的地方不一样,将这些方法分为三类:(1)导 ...
- hive的数据导入与数据导出:(本地,云hdfs,hbase),列分隔符的设置,以及hdfs上传给pig如何处理
hive表的数据源有四种: hbase hdfs 本地 其他hive表 而hive表本身有两种: 内部表和外部表. 而hbase的数据在hive中,可以建立对应的外部表(参看hive和hbase整合) ...
- Hbase数据导入导出
平时用于从生产环境hbase到导出数据到测试环境. 导入数据: import java.io.BufferedReader; import java.io.File; import java.io.F ...
- HBase 学习之一 <<HBase使用客户端API动态创建Hbase数据表并在Hbase下导出执行>>
HBase使用客户端API动态创建Hbase数据表并在Hbase下导出执行 ----首先感谢网络能够给我提供一个开放的学习平台,如果没有网上的技术爱好者提供 ...
随机推荐
- C Primer Plus(第五版)8
第 8 章 字符输入/输出和输入确认 在本章中你将学习下列内容: · 有关输入,输出以及缓冲和非缓冲输入之间的区别的更多内容. · 从键盘模拟文件结尾条件的方法. · 如何重定向将你的程序与文件相连接 ...
- LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 在Yarn上运行spark-shell和spark-sql命令行
转载自:http://lxw1234.com/archives/2015/08/448.htm 如果你已经有一个正常运行的Hadoop Yarn环境,那么只需要下载相应版本的Spark,解压之后做为S ...
- (转)C# MD5
本文原地址:http://blog.csdn.net/zhoufoxcn/article/details/1497099 作者:周公 代码如下: using System; using System. ...
- FAQ: Automatic Statistics Collection (文档 ID 1233203.1)
In this Document Purpose Questions and Answers What kind of statistics do the Automated tasks ...
- 30. Distinct Subsequences
Distinct Subsequences OJ: https://oj.leetcode.com/problems/distinct-subsequences/ Given a string S a ...
- convert NameValueCollection/Dictionary<string, object> to JSON string
public static class WebExtension { public static T Decode<T>(this RequestBase res) { Type type ...
- Unity AssetBundles and Resources指引 (一)
本文内容主要翻译自下面这篇文章 https://unity3d.com/cn/learn/tutorials/topics/best-practices/guide-assetbundles-and- ...
- BP神经网络学习笔记_附源代码
BP神经网络基本原理: 误差逆传播(back propagation, BP)算法是一种计算单个权值变化引起网络性能变化的较为简单的方法.由于BP算法过程包含从输出节点开始,反向地向第一隐含层(即最接 ...
- oracle修改连接空闲自动断开
空闲3分钟自动断开 SELECT * FROM DBA_PROFILES; CREATE PROFILE KILLIDLE LIMIT IDLE_TIME ; alter PROFILE DEFAUL ...