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 ...
随机推荐
- IT小鲜肉 Widgets Tree 单选、多选、相关回调函数、获取选中的节点功能
写一个树控件并没有想象中的那么容易,今天又花了我一个1个多小时,主要为IT小鲜肉 Widgets Tree控件添加了 单选.多选.选择前和选择后两个回调函数.获取选中节点的功能.后面会继续努力完善这个 ...
- AngularJS学习 之 安装
1. 安装好Node.js 2. 安装好Git 3. 安装好Yeoman 以管理员身份打开cmd 输入 npm install -g yo 回车即可开始安装Yeoman,具体的安装行为最好看官网的介绍 ...
- css3动画运用
https://daneden.github.io/animate.css/ https://minimamente.com/example/magic_animations/ http://i ...
- Python 底层原理知识
1.Python是如何进行内存管理的? 答:从三个方面来说,一对象的引用计数机制,二垃圾回收机制,三内存池机制 一.对象的引用计数机制 Python内部使用引用计数,来保持追踪内存中的对象,所有对象都 ...
- 漂亮的ActionBar效果
Newsstand—这个应用引进了新的方式,使得ActionBar达到了新的水平.如果你打开这个应用的发布页,你会注意到不带图标的ActionBar是半透明的,而且和一个大的图片集(一个大的杂志图标, ...
- linux centos7最小化安装桥接模式网络设置、xshell、xftf
一.网络连接设置1.桥接模式 使用电脑真实网卡,可以和自己的电脑连接,也可以和外部网络连接2.NAT模式 使用wmware network adapter vmnet8虚拟网卡,可以和自己的电脑连接, ...
- 签署您的应用--手动签署 APK
签署您的应用 本文内容 证书和密钥库 签署您的调试构建 调试证书的有效期 管理您的密钥 使用 Google Play 应用签名 自行管理您的密钥和密钥库 签署 APK 生成密钥和密钥库 手动签署 AP ...
- ASP.NET MVC 实现区域 项目分离 (比较好的方式)
说明: ZRT.Web 是前台网站,目录[D:\ZRT.Web\] ZRT.Admin 是后台管理,目录[D:\ZRT.Web\Applications\Admin\],删除文件[Global.asa ...
- leetCode题解之Product of Array Except Self
1.题目描述 2.题目分析 每个元素对应的积应该是 它 前面的每个元素的积,和后面的每个元素的积 3.代码 vector<int> productExceptSelf(vector< ...
- Entity Framework之DB First方式
EF(Entity Framework的简称,下同)有三种方式,分别是:DataBase First. Model First和Code First. 下面是Db First的方式: 1. 数据库库中 ...