附录代码:

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导入和导出的更多相关文章

  1. HBase从hdfs导入数据

    需求:将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 一.hdfs中的数据是这样的 每一行的数据是这样的id ...

  2. HBase数据的导入和导出

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

  3. HBase 实战(1)--HBase的数据导入方式

    前言: 作为Hadoop生态系统中重要的一员, HBase作为分布式列式存储, 在线实时处理的特性, 备受瞩目, 将来能在很多应用场景, 取代传统关系型数据库的江湖地位. 本篇博文重点讲解HBase的 ...

  4. mapreduce方式操作hbase

    一.导入数据到hbase 1.配置hbase-site.xml指向hdfs <configuration> <property> <name>hbase.rootd ...

  5. HBase、HDFS和MapReduce架构异同简解

    HBase.HDFS和MapReduce架构异同 .. HBase(公司架构模型) HDFS2.0(公司架构模型) MR2.0(公司架构模型) MR1.0(公司架构模型) 中央 HMaster Nam ...

  6. HBase数据快速导入之ImportTsv&Bulkload

    导入数据最快的方式,可以略过WAL直接生产底层HFile文件 (环境:centos6.5.Hadoop2.6.0.HBase0.98.9) 1.SHELL方式 1.1 ImportTsv直接导入 命令 ...

  7. 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 ...

  8. HBase -- 基于HDFS的开源分布式NoSQL数据库

    HBase(Hadoop Database)是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,我们可以利用HBase技术在廉价的PC上搭建起大规模结构化存储集群.同Google的Bigtable ...

  9. HBase(三): Azure HDInsigt HBase表数据导入本地HBase

    目录: hdfs 命令操作本地 hbase Azure HDInsight HBase表数据导入本地 hbase hdfs命令操作本地hbase: 参见  HDP2.4安装(五):集群及组件安装 , ...

随机推荐

  1. Apache Spark Streaming的适用场景

    使用场景: Spark Streaming 适合需要历史数据和实时数据结合进行分析的应用场景,对于实时性要求不是特别高的场景也能够胜任.

  2. homework-01 "最大子数组之和"的问题求解过程

    写在前面:我的算法能力很弱,并且也是第一次写博文,总之希望自己能在这次的课程中学到很多贴近实践的东西吧. 1.这次的程序是python写的,这也算是我第一次正正经经地拿python来写东西,结果上来说 ...

  3. thymeleaf的属性优先级

    所有Thymeleaf属性定义一个数字优先,建立他们的顺序执行的标签.这个顺序是: Order Feature Attributes 1 Fragment inclusion th:includeth ...

  4. [iOS 多线程 & 网络 - 3.0] - 在线动画Demo

    A.需求 所有数据都从服务器下载 动画列表包含:图片.动画名标题.时长副标题 点击打开动画观看   code source: https://github.com/hellovoidworld/Vid ...

  5. POJ 2186 Popular Cows(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...

  6. [Windows驱动开发](二)基础知识——数据结构

    本节主要介绍驱动开发的一些基础知识. 1. 驱动程序的基本组成 1.1. 最经常见到的数据结构 a. DRIVER_OBJECT驱动对象 // WDK中对驱动对象的定义 // 每个驱动程序都会有一个唯 ...

  7. arcmap+vs2010

    esri为vs2010提供了addin开发模版,有几个关键的地方注意下 1.C:\Program Files (x86)\MSBuild\Esri\下存在ESRI.ArcGIS.AddIns.Serv ...

  8. [c++]程序的内存划分理解

    全局和静态数据区:用于存放全局变量和静态变量(全局变量和局部变量) 常量数据区:用于存放常量数据 代码区:用于存储代码 栈:用于局部变量和函数参数 堆:程序员申请(程序员控制的部分,new/delet ...

  9. 通过java发送http请求

    通常的http请求都是由用户点击某个连接或者按钮来发起的,但是在一些后台的Java程序中需要发送一些get或这post请求,因为不涉及前台页面,该怎么办呢? 下面为大家提供一个Java发送http请求 ...

  10. ADO.NET 快速入门(八):处理 Errors

    除了 Try/Catch 和 Exceptions 以外,新的 ADO.NET 数据框架也允许在 DataSet 的每行数据添加错误信息.如果 Updates 或者其他操作失败,SqlDataAdap ...