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下导出执行 ----首先感谢网络能够给我提供一个开放的学习平台,如果没有网上的技术爱好者提供 ...
随机推荐
- 《一课经济学》书摘笔记IV
有出口,才有钱进口:没有进口,就没有机会出口,因为外国人没有美元可以用来买美国的产品.要想扩大出口,就必须有更多的进口,否则收不到货款.要想压缩进口,出口也会随之被压缩.所以,当我们决定减少进口的时候 ...
- WPF异步调用
this.Dispatcher.BeginInvoke(new Action(()=> this.textBlock1.Text = DateTime.Now.ToString("HH ...
- ORACLE错误一览表【转】
http://blog.itpub.net/26892340/viewspace-722178/
- 502 Bad Gateway什么意思
http://baike.baidu.com/link?url=U2ijg5T5PG_tTkY67mqfx07co7qGqvMB32rbLwq4S2ThBSRIWWvU76Y0Mb8Z3z6nbViN ...
- ownDocument和documentElement
<!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...
- Android——requestWindowFeature
requestWindowFeature可以设置的值有:1.DEFAULT_FEATURES:系统默认状态,一般不需要指定2.FEATURE_CONTEXT_MENU:启用ContextMenu,默认 ...
- fw:sed的高级用法
转的,找不到原创了.... sed高级用法 <收藏> 首先,应该明白模式空间的定义.模式空间就是读入行所在的缓存,sed对文本行进行的处理都是在这个缓存中进行的.这对接下来 的学习是有帮助 ...
- 将你的代码上传 Bintray 仓库
在 Android Studio 中,我们通常可以利用 gradle 来导入别人写的第三方库,通常可以简单得使用一句话就能搞定整个导包过程, 比如: compile 'net.cpacm.moneyt ...
- MSP430F149学习之路——时钟1
1.看门狗产生方波 #include <msp430x14x.h> void main() { WDTCTL = WDT_MDLY_32; IE1 |= WDTIE; P1DIR |= B ...
- 二模09day2解题报告
T1.domino骨牌 n张有黑有白的骨牌排一排,连续三张同色排一起就不好看,求共多少方案不好看. 分析一下,f[3]=2,f[4]=6,f[n]:如果n-1==n 那么方案数为f[n-2],如果不同 ...