环境
  虚拟机:VMware 10
  Linux版本:CentOS-6.5-x86_64
  客户端:Xshell4
  FTP:Xftp4
  jdk8
  hadoop-2.6.5
  hbase-0.98.12.1-hadoop2

package wc;

import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class WCMapper extends Mapper<LongWritable, Text, Text, IntWritable> { @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] strs = value.toString().split(" ");
for (String string : strs) {
context.write(new Text(string), new IntWritable(1));
}
}
}
package wc;

import java.io.IOException;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text; public class WCReducer extends TableReducer<Text, IntWritable, ImmutableBytesWritable> { @Override
protected void reduce(Text text, Iterable<IntWritable> iterable, Context context)
throws IOException, InterruptedException { int sum = 0;
for (IntWritable it : iterable) {
sum += it.get();
}
//将mr结果输出到HBase
Put put = new Put(text.toString().getBytes());
put.add("cf".getBytes(), "ct".getBytes(), (sum + "").getBytes());
context.write(null, put); }
}
package wc;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; public class WCRunner { public static void main(String[] args) throws Exception { Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://node1:8020");
conf.set("hbase.zookeeper.quorum", "node1,node2,node3");
Job job = Job.getInstance(conf);
job.setJarByClass(WCRunner.class); // 指定mapper 和 reducer
job.setMapperClass(WCMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// 最后一个参数设置false
// adddependecyjars=false 表示不将jar上传分布式集群 即 本地执行
TableMapReduceUtil.initTableReducerJob("wc", WCReducer.class, job, null, null, null, null, false);
FileInputFormat.addInputPath(job, new Path("/user/hive/warehouse/wc/"));
job.waitForCompletion(true);
}
}

【Hbase学习之五】HBase MapReduce的更多相关文章

  1. Hbase 学习(一) hbase配置文件同步

    最近在狂啃hadoop的书籍,这部<hbase:权威指南>就进入我的视野里面了,啃吧,因为是英文的书籍,有些个人理解不对的地方,欢迎各位拍砖. HDFS和Hbase配置同步 hbase的配 ...

  2. HBase 学习之一 <<HBase使用客户端API动态创建Hbase数据表并在Hbase下导出执行>>

    HBase使用客户端API动态创建Hbase数据表并在Hbase下导出执行                       ----首先感谢网络能够给我提供一个开放的学习平台,如果没有网上的技术爱好者提供 ...

  3. HBase学习——3.HBase表设计

    1.建表高级属性 建表过程中常用的shell命令 1.1 BLOOMFILTER 默认是 NONE 是否使用布隆过虑及使用何种方式,布隆过滤可以每列族单独启用 使用HColumnDescriptor. ...

  4. HBase学习笔记-HBase性能研究(1)

    使用Java API与HBase集群交互时,需要构建HTable对象,使用该对象提供的方法来进行插入/删除/查询等操作.要创建HTable对象,首先要创建一个带有HBase集群信息的配置对象Confi ...

  5. HBase学习——4.HBase过滤器

    1.过滤器 基础API中的查询操作在面对大量数据的时候是非常苍白的,这里Hbase提供了高级的查询方法:Filter.Filter可以根据簇.列.版本等更多的条件来对数据进行过滤,基于Hbase本身提 ...

  6. hbase 学习(十二)非mapreduce生成Hfile,然后导入hbase当中

    最近一个群友的boss让研究hbase,让hbase的入库速度达到5w+/s,这可愁死了,4台个人电脑组成的集群,多线程入库调了好久,速度也才1w左右,都没有达到理想的那种速度,然后就想到了这种方式, ...

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

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

  8. HBase学习笔记-高级(一)

    HBase1. hbase.id记录了集群的唯一标识:hbase.version记录了文件格式的版本号2. split和.corrupt目录在日志分裂过程中使用,以便保存一些中间结果和损坏的日志在表目 ...

  9. HBase学习系列

    转自:http://www.aboutyun.com/thread-8391-1-1.html 问题导读: 1.hbase是什么? 2.hbase原理是什么? 3.hbase使用中会遇到什么问题? 4 ...

随机推荐

  1. linux crypt()函数使用总结

    原型: char *crypt(const char *key, const char *salt); 标准说明: crypt()算法会接受一个最长可达8字符的密钥(即key),并施以数据加密算法(D ...

  2. 用uart实现printf函数

    硬件:JZ2440 实现功能:用putchr()函数实现printf() start.s nand.c uart.c uart.h my_stdio.c my_stdio.h main.c start ...

  3. 使用监听器解决路径问题,例如在jsp页面引入js,css的web应用路径

    使用监听器解决路径问题,例如在jsp页面引入js,css的web应用路径 经常地,我们要在jsp等页面引入像js,css这样的文件,但是在服务器来访问的时候,这时间就有关到相对路径与绝对路径了.像网页 ...

  4. JavaScript学习(七)

  5. 如何代码隐藏email而用户又能看到

    我们有时在网站上留一个邮箱,然后漫天垃圾邮件,非常苦恼,这是因为爬虫通过代码匹配收集网页上的邮箱,那么有没办法代码隐藏email而用户又能看到呢?其实不会很难,如果你的网站是用wordpress搭建, ...

  6. Linux系统启动排错实验集合

    Centos6系统启动流程 1. post  加电自检  检查硬件环境 2. 选择一个硬件类型引导启动           mbr 446字节   grub  stage1 3. 加载boot分区的文 ...

  7. vue中让input框自动聚焦

    created(){ this.changfouce(); }, methods: { //在vue生命周期的created()钩子函数进行的DOM操作要放在Vue.nextTick()的回调函数中, ...

  8. vuex 子组件传值

    以下是基础的使用方法,详细且深入使用方法详细见博客:https://segmentfault.com/a/1190000015782272 Vuex官网地址:https://vuex.vuejs.or ...

  9. [django]django查询最佳实战

    from django.db.models import Max, Min, Sum, Avg, Count, Q, F Django中的F和Q函数 一.F介绍 作用:操作数据表中的某列值,F()允许 ...

  10. [py]字符串转换为列表

    字符串转换为列表 "[1,2,3]" ==> [1,2,3]