MapReduce操作Hbase --table2file
官方手册:http://hbase.apache.org/book.html#mapreduce.example
简单的操作,将hbase表中的数据写入到文件中。
RunJob 源码:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableInputFormat;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; /**
* Created by Edward on 2016/6/29.
*/
public class RunJob implements Tool { private Configuration conf = null; @Override
public int run(String[] strings) throws Exception { Configuration conf = this.getConf(); FileSystem fs = FileSystem.get(conf); Job job = Job.getInstance(conf,"etl");
job.setJarByClass(RunJob.class); job.setInputFormatClass(TableInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setOutputKeyClass(TextOutputFormat.class); Scan scan = new Scan();
scan.setCaching(1024);
scan.setCacheBlocks(false); TableMapReduceUtil.initTableMapperJob("test1",
scan,
MyMapper.class,
Text.class,
Text.class,
job); Path path = new Path("/hbase_out");
if(fs.exists(path))
{
fs.delete(path,true);
} FileOutputFormat.setOutputPath(job, new Path("/hbase_out")); boolean b = job.waitForCompletion(true);
if(b)
{
System.out.println("执行成功");
}
return 0;
} @Override
public void setConf(Configuration configuration) { System.setProperty("HADOOP_USER_NAME","root");
configuration.set("hbase.zookeeper.quorum","node1,node2,node3");
configuration.set("mapred.jar","D:\\etl.jar"); this.conf = HBaseConfiguration.create(configuration);
} @Override
public Configuration getConf() {
return this.conf;
} public static void main(String[] args)
{
try {
ToolRunner.run(new Configuration(), new RunJob(), args);
} catch (Exception e) {
e.printStackTrace();
}
}
}
MyMapper代码:
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.io.Text; import java.io.IOException; /**
* Created by Edward on 2016/6/29.
*/
public class MyMapper extends TableMapper<Text, Text>{
@Override
protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException { String val = new String(value.getValue("info".getBytes(),"name".getBytes()));
String row = new String(value.getRow());
context.write(new Text(row), new Text(val));
}
}
MyReducer代码:
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; /**
* Created by Edward on 2016/6/29.
*/
public class MyReducer extends Reducer<Text,Text,Text,Text>{ @Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
for(Text t:values) {
context.write(key, t);
}
}
}
MapReduce操作Hbase --table2file的更多相关文章
- Mapreduce操作HBase
这个操作和普通的Mapreduce还不太一样,比如普通的Mapreduce输入可以是txt文件等,Mapreduce可以直接读取Hive中的表的数据(能够看见是以类似txt文件形式),但Mapredu ...
- HBase 相关API操练(三):MapReduce操作HBase
MapReduce 操作 HBase 在 HBase 系统上运行批处理运算,最方便和实用的模型依然是 MapReduce,如下图所示. HBase Table 和 Region 的关系类似 HDFS ...
- 7.MapReduce操作Hbase
7 HBase的MapReduce HBase中Table和Region的关系,有些类似HDFS中File和Block的关系.由于HBase提供了配套的与MapReduce进行交互的API如 Ta ...
- Hbase理论&&hbase shell&&python操作hbase&&python通过mapreduce操作hbase
一.Hbase搭建: 二.理论知识介绍: 1Hbase介绍: Hbase是分布式.面向列的开源数据库(其实准确的说是面向列族).HDFS为Hbase提供可靠的底层数据存储服务,MapReduce为Hb ...
- Hbase第五章 MapReduce操作HBase
容易遇到的坑: 当用mapReducer操作HBase时,运行jar包的过程中如果遇到 java.lang.NoClassDefFoundError 类似的错误时,一般是由于hadoop环境没有hba ...
- HBase学习之路 (五)MapReduce操作Hbase
MapReduce从HDFS读取数据存储到HBase中 现有HDFS中有一个student.txt文件,格式如下 95002,刘晨,女,19,IS 95017,王风娟,女,18,IS 95018,王一 ...
- hadoop2的mapreduce操作hbase数据
1.从hbase中取数据,再把计算结果插入hbase中 package com.yeliang; import java.io.IOException; import org.apache.hadoo ...
- 大数据入门第十四天——Hbase详解(三)hbase基本原理与MR操作Hbase
一.基本原理 1.hbase的位置 上图描述了Hadoop 2.0生态系统中的各层结构.其中HBase位于结构化存储层,HDFS为HBase提供了高可靠性的底层存储支持, MapReduce为HBas ...
- Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结
转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...
随机推荐
- [WEB地图] 2017高德地图API WEB开发(key申请,地图搭建)简约教程
前端时间因为公司需要研究 了一下百度的教程 然后写个简约的笔记记录一下自己学习的收获,只为了满足自己暂时的写作热情 高德地图WEB开发(key申请.api)简约教程 1.首先我们需要去“高德地 ...
- 转:xdebug在linux下的安装教程
原文:xdebug在linux下的安装教程 [注意,本人是PHP7.1.7 Nginx ,第7步没有做,但是xdebug.so就已经在PHP的扩展文件夹里面了.目录是phpinfo的extension ...
- 在JS中如何把毫秒转换成规定的日期时间格式
最近做的一个项目,从后台查询到日期数据传到前台JS中遍历之后显示的是以"14"开头的毫秒数,刚开始想用"simpleDateFormat"类处理,结果显示都不显 ...
- 关于 eval 的报错 Uncaught ReferenceError: False is not defined
var obj ={'id': 16, 'name': '管理员', 'delflag': False, 'grade': 1000000.0}VM3614:1 Uncaught ReferenceE ...
- Pig安装与应用
1. 参考说明 参考文档: http://pig.apache.org/docs/r0.17.0/start.html#build 2. 安装环境说明 2.1. 环境说明 CentOS7.4+ ...
- 612.1.003 ALGS4 | Stacks and Queues
Algorithm | Coursera - by Robert Sedgewick Type the code one by one! 不要拜读--只写最有感触的!而不是仅仅做一个笔记摘录员,那样毫 ...
- 131.008 Unsupervised Learning - Principle component Analysis |PCA | 非监督学习 - 主成分分析
@(131 - Machine Learning | 机器学习) PCA是一种特征选择方法,可将一组相关变量转变成一组基础正交变量 25 PCA的回顾和定义 Demo: when to use PCA ...
- JSON学习笔记-4
JSON 数组 1.访问数组 1.一次访问一个嵌套内容值var myObj, x; myObj = { "name":"网站", , "sites&q ...
- linux 获取命令或配置文件的帮助信息 man、whatis、apropos、--help
man /usr/bin/man man [命令或配置文件]获取帮助信息 man ls /-lman date/-d man services //不需要添加绝对路径/etc/services NAM ...
- 3org.springframework.beans.factory.BeanDefinitionStoreException异常
1.下面是我遇到的异常信息: 2017-03-25 18:01:11,322 [localhost-startStop-1][org.springframework.web.context.Conte ...