官方手册: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的更多相关文章

  1. Mapreduce操作HBase

    这个操作和普通的Mapreduce还不太一样,比如普通的Mapreduce输入可以是txt文件等,Mapreduce可以直接读取Hive中的表的数据(能够看见是以类似txt文件形式),但Mapredu ...

  2. HBase 相关API操练(三):MapReduce操作HBase

    MapReduce 操作 HBase 在 HBase 系统上运行批处理运算,最方便和实用的模型依然是 MapReduce,如下图所示. HBase Table 和 Region 的关系类似 HDFS ...

  3. 7.MapReduce操作Hbase

    7 HBase的MapReduce   HBase中Table和Region的关系,有些类似HDFS中File和Block的关系.由于HBase提供了配套的与MapReduce进行交互的API如 Ta ...

  4. Hbase理论&&hbase shell&&python操作hbase&&python通过mapreduce操作hbase

    一.Hbase搭建: 二.理论知识介绍: 1Hbase介绍: Hbase是分布式.面向列的开源数据库(其实准确的说是面向列族).HDFS为Hbase提供可靠的底层数据存储服务,MapReduce为Hb ...

  5. Hbase第五章 MapReduce操作HBase

    容易遇到的坑: 当用mapReducer操作HBase时,运行jar包的过程中如果遇到 java.lang.NoClassDefFoundError 类似的错误时,一般是由于hadoop环境没有hba ...

  6. HBase学习之路 (五)MapReduce操作Hbase

    MapReduce从HDFS读取数据存储到HBase中 现有HDFS中有一个student.txt文件,格式如下 95002,刘晨,女,19,IS 95017,王风娟,女,18,IS 95018,王一 ...

  7. hadoop2的mapreduce操作hbase数据

    1.从hbase中取数据,再把计算结果插入hbase中 package com.yeliang; import java.io.IOException; import org.apache.hadoo ...

  8. 大数据入门第十四天——Hbase详解(三)hbase基本原理与MR操作Hbase

    一.基本原理 1.hbase的位置 上图描述了Hadoop 2.0生态系统中的各层结构.其中HBase位于结构化存储层,HDFS为HBase提供了高可靠性的底层存储支持, MapReduce为HBas ...

  9. Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结

    转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...

随机推荐

  1. 浏览器根对象document之字符串属性

    1.1 停止使用的属性 fgColor.linkColor.vlinkColor.alinkColor.bgColor. 1.2 文档地址 document.URL 与documentURI属性返回同 ...

  2. TextBlock 重写,当文本过长时,自动截断文本并出现Tooltip

    如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  3. js获取当前页面url信息方法(JS获取当前网址信息)

    设置或获取对象指定的文件名或路径. alert(window.location.pathname) 设置或获取整个 URL 为字符串. alert(window.location.href); 设置或 ...

  4. Ubuntu 安装python

    1. wget http://mirrors.sohu.com/python/3.6.0/Python-3.6.0.tar.xz wget https://www.python.org/ftp/pyt ...

  5. html + css + jquery实现简单的进度条实例

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...

  6. 3 个简单、优秀的 Linux 网络监视器

    作者: Carla Schroder 译者: LCTT geekpi 用 iftop.Nethogs 和 vnstat 了解更多关于你的网络连接. 你可以通过这三个 Linux 网络命令,了解有关你网 ...

  7. [翻译] JKLLockScreenViewController

    JKLLockScreenViewController https://github.com/tiny2n/JKLLockScreenViewController Overview It is Loc ...

  8. jumpserver 问题,给自己看的哦,你们不准看哟

    给自己看的,排版忽略! http://docs.jumpserver.org/zh/docs/setup_by_centos7.html 看完全部文档后解决不了再看下边的!!! docker 服务启动 ...

  9. Hadoop HBase概念学习系列之HBase里的长表VS宽表VS窄表(十五)

    有时候啊,HBase表的设计方案通常,还会考虑如下一些因素,当然,这只是考虑范围里的部分呢. 更多的行还是更多的版本?后者使用了HBase自带的功能.但是需要在列簇中定义最大版本数,这样做可能有风险. ...

  10. mysql的select查询语句

    1.简单查询 mysql> select * from students; +------------+----------+------+------+ | id | sname | sex ...