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 ...
随机推荐
- android开启线程的误区
发现一些刚学android的人,和我当初一样,对android的线程会存在着一定误区. 在android中,开启新线程时,一些人会用以下方法: new Handler().post(r); 但是这样并 ...
- linux 链接命令
ln link /bin/ln -s 创建软链接ln -s [原文件] [链接文件] 软链接 ln -s /etc/issue /tmp/issue.soft硬链接ln /etc/issue /tmp ...
- SQL Server的优点与缺点
一般来说索引会加快查询速度,但会影响插入,修改,删除的数据,且占用物理空间;所以我们应该合理的创建索引,而且应该先创建聚合索引,再创建非聚合索引.要在经常进行查询的列上创建索引,而且如果表列较少的话要 ...
- socat 的神奇使用方式
目的是实现科* 学 * 上*网,现在记录一下流程 先在服务器上安装(比如美国,香港,台湾,马来的云主机)squid,easy_rsa, centos 下可以用yum直接安装 $ yum install ...
- Oracle EBS 更改物料说明后,在MTL_SYSTEM_ITEMS_B表中无变化
需要再中文和英文环境同时修改: 程序里,可以通过初始session语言环境来解决.
- Python项目生成requirements.txt的多种方式
我相信任何软件程序都会有依赖的类库,尤其现在开源如此的火爆,因为一个项目可能会有无很多的依赖的包 这个时候难道我们都要一个一个的去找到安装吗?即使你找到了依赖的包 但是呢模块的版本又有很多难道你都要装 ...
- Azure Cosmos DB 使用费用参考
之前在学习Cosmos DB 中SQL API(DocumentDB) 的时候,也就是之前做的一些笔记,看到有使用费用的一些介绍,就有兴趣的去了解了下,做了一下简单的总结. 想了解更多或是购买使用的还 ...
- Replace-iOS
Replace-iOS https://github.com/MartinRGB/Replace-iOS 看了下demo,运行起来超卡...... Simply Implement Zee Young ...
- [翻译] DCPathButton
DCPathButton https://github.com/Tangdixi/DCPathButton DCPathButton 2.0 is a menu button for iOS. Des ...
- 可以触发点击事件并变色的UILabel
可以触发点击事件并变色的UILabel 谁说UILabel不能够当做button处理点击事件呢?今天,笔者就像大家提供一个改造过的,能够触发点击事件并变色的UILabel:) 效果图: 还能当做计时器 ...