MapReduce的方式进行HBase向HDFS导入和导出
附录代码:
HBase---->HDFS
import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; public class HBase2HDFS { public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
Job job = Job.getInstance(conf, HBase2HDFS.class.getSimpleName());
job.setJarByClass(HBase2HDFS.class);
//MR有输入和输出,输入一般是FileInputFormat等...但是在HBase中需要用到一个特殊的工具类是TableMapReduceUtil
TableMapReduceUtil.initTableMapperJob(args[0], new Scan(), HBase2HDFSMapper.class,
Text.class, Text.class, job);
//HBase中的具体操作打到MR的job中.
TableMapReduceUtil.addDependencyJars(job);
job.setMapperClass(HBase2HDFSMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//FileOutputFormat.setOutputPath(job, new Path("/t1-out"));
job.setNumReduceTasks(0);
job.waitForCompletion(true); }
static class HBase2HDFSMapper extends TableMapper<Text, Text>{
private Text rowKeyText = new Text();
private Text value = new Text(); //这个TableMapper中的两个泛型是Map阶段的输出..HBase中的数据要想进入HBase,几乎都用引号引起来.
//TableMapper是Mapper类的一个子类.这个类用来定义前面的两个泛型参数.
@Override
protected void map(
ImmutableBytesWritable key,
Result result,
Mapper<ImmutableBytesWritable, Result, Text, Text>.Context context)
throws IOException, InterruptedException {
//结果都在result对象,用raw方法从result对象中找到数据. 这个raw()方法已经过时了.
/*
KeyValue[] raw = result.raw();
for (KeyValue keyValue : raw) {
keyValue.getValue();
}
*/
/*
* 想输出的数据格式如下: 1 zhangsan 13 (行键,name,age)
* 2 lisi 14
*/ //要想精确的获得某一列的值,要根据行键,列族,列的时间戳.
//getColumnLatestCell 是获得最新的时间戳的值 相当于时间戳已经定义好了.
byte[] nameBytes = result.getColumnLatestCell("cf".getBytes(), "name".getBytes()).getValue();
byte[] ageBytes = result.getColumnLatestCell("cf".getBytes(), "age".getBytes()).getValue(); rowKeyText.set(key.get());
value.set(new String(nameBytes) + "\t" + new String(ageBytes));
context.write(new Text(key.get()), value);
//这里已经把数据搞成了 1 name age 的形式....就不需要写Reduce
}
}
}
HDFS---->HBase 通过MR导入到HBase
import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; public class HDFS2HBaseImport { public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set(TableOutputFormat.OUTPUT_TABLE, args[0]); Job job = Job.getInstance(conf, HDFS2HBaseImport.class.getSimpleName());
job.setJarByClass(HDFS2HBaseImport.class); //数据到底放到哪一张表中,还是要用到TableMapReduceUtil类.
TableMapReduceUtil.addDependencyJars(job);
job.setMapperClass(HDFS2HBaseMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setReducerClass(HDFS2HBaseReducer.class);
job.setOutputFormatClass(TableOutputFormat.class);
FileInputFormat.setInputPaths(job, args[1]);
job.waitForCompletion(true);
} static class HDFS2HBaseMapper extends Mapper<LongWritable, Text, Text, Text>{
private Text rowKeyText = new Text();
private Text value = new Text(); @Override
protected void map(LongWritable key, Text text,
Mapper<LongWritable, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
String[] splits = text.toString().split("\t");
rowKeyText.set(splits[0]);
value.set(splits[1] + "\t" + splits[2]);//name\tage
context.write(rowKeyText, value);
}
}
//Reduce继承的是和在导出的时候Map extends TableMapper 对应的 因为导入的是HBase中,所以后面的参数用NullWritable代替
static class HDFS2HBaseReducer extends TableReducer<Text, Text, NullWritable> {
@Override
protected void reduce(Text k2, Iterable<Text> v2s,
Reducer<Text, Text, NullWritable, Mutation>.Context context)
throws IOException, InterruptedException {
//向HBase中插入数据一定要用到Put对象.
Put put = new Put(k2.getBytes()); for (Text text : v2s) {
String[] splits = text.toString().split("\t");
//加载列和对应的值
put.add("cf".getBytes(), "name".getBytes(), splits[0].getBytes());
put.add("cf".getBytes(), "age".getBytes(), splits[1].getBytes());
context.write(NullWritable.get(), put);//一个参数是key,一个是对应的value.
//导入HBase不需要key...直接用NullWritable对象和封装好数据的put对象.
}
}
}
}
MapReduce的方式进行HBase向HDFS导入和导出的更多相关文章
- HBase从hdfs导入数据
需求:将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 一.hdfs中的数据是这样的 每一行的数据是这样的id ...
- HBase数据的导入和导出
查阅了几篇中英文资料,发现有的地方说的不是很全部,总结在此,共有两种命令行的方式来实现数据的导入导出功能,即备份和还原. 1 HBase本身提供的接口 其调用形式为: 1)导入 ./hbase org ...
- HBase 实战(1)--HBase的数据导入方式
前言: 作为Hadoop生态系统中重要的一员, HBase作为分布式列式存储, 在线实时处理的特性, 备受瞩目, 将来能在很多应用场景, 取代传统关系型数据库的江湖地位. 本篇博文重点讲解HBase的 ...
- mapreduce方式操作hbase
一.导入数据到hbase 1.配置hbase-site.xml指向hdfs <configuration> <property> <name>hbase.rootd ...
- HBase、HDFS和MapReduce架构异同简解
HBase.HDFS和MapReduce架构异同 .. HBase(公司架构模型) HDFS2.0(公司架构模型) MR2.0(公司架构模型) MR1.0(公司架构模型) 中央 HMaster Nam ...
- HBase数据快速导入之ImportTsv&Bulkload
导入数据最快的方式,可以略过WAL直接生产底层HFile文件 (环境:centos6.5.Hadoop2.6.0.HBase0.98.9) 1.SHELL方式 1.1 ImportTsv直接导入 命令 ...
- Sqoop_mysql,hive,hdfs导入导出操作
前言: 搭建环境,这里使用cdh版hadoop+hive+sqoop+mysql 下载 hadoop-2.5.0-cdh5.3.6.tar.gz hive-0.13.1-cdh5.3.6.tar.gz ...
- HBase -- 基于HDFS的开源分布式NoSQL数据库
HBase(Hadoop Database)是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,我们可以利用HBase技术在廉价的PC上搭建起大规模结构化存储集群.同Google的Bigtable ...
- HBase(三): Azure HDInsigt HBase表数据导入本地HBase
目录: hdfs 命令操作本地 hbase Azure HDInsight HBase表数据导入本地 hbase hdfs命令操作本地hbase: 参见 HDP2.4安装(五):集群及组件安装 , ...
随机推荐
- ES6学习小计
1.增加了for of语法,对应C#里的foreach,注意ES5中的 for in只会传递0,1,2.....序号,并且是字符for-of循环语句通过方法调用来遍历各种集合.数组.Maps对象.Se ...
- Android问题-DelphiXE8新建AVD出现“no system images installed for this target”
相关资料: 1.http://www.cnblogs.com/yc-755909659/p/4080645.html 问题现象:创建Android模拟器提不”no system images inst ...
- Unity3D之空间转换学习笔记(二):基础数学
这期笔记我们专注Unity提供的各种数学相关的类来学习. 时间Time API文档地址:http://docs.unity3d.com/ScriptReference/Time.html 时间加/减速 ...
- JAVA 正则表达式 (超详细)
(PS:这篇文章为转载,我不喜欢转载的但我觉得这篇文章实在是超赞了,就转了过来,这篇可以说是学习JAVA正则表达的必读篇.作者是个正真有功力的人,阅读愉快) 在Sun的Java JDK 1.40版本中 ...
- UVa 11234 Expressions (二叉树重建&由叶往根的层次遍历)
画图出来后结果很明显 xyPzwIM abcABdefgCDEF sample output wzyxIPM gfCecbDdAaEBF * + - x y z w F B E a A d D b c ...
- 关于GridBagLayout的讲解哦
13-08-29 17:01:10| 分类: Java | 标签:gridbaglayout gridbagconstraints 添加方法 |字号 订阅 GridBagLayout ...
- 读《C# 和 Java 的比较》有感
网上的一篇<C# 和 Java 的比较>(或者叫<Java 和 C# 的比较>)写的挺不错的,今天忽然搜索到. 自己刚刚接触C#,也不由自主地随时都拿来和Java做对比,所以就 ...
- Spark Core源代码分析: Spark任务运行模型
DAGScheduler 面向stage的调度层,为job生成以stage组成的DAG,提交TaskSet给TaskScheduler运行. 每个Stage内,都是独立的tasks,他们共同运行同一个 ...
- Android自己定义DataTimePicker(日期选择器)
Android自己定义DataTimePicker(日期选择器) 笔者有一段时间没有发表关于Android的文章了,关于Android自己定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中.本篇 ...
- 彩票APP将演绎“快鱼吃慢鱼”的发展轨迹
思科CEO钱伯斯有句名言,叫"快鱼吃慢鱼",他觉得"在Internet经济下,大公司不一定打败小公司,可是快的一定会打败慢的."对此观点,笔者表示部分认同,首先 ...