环境
  虚拟机: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. 【PyQt5-Qt Designer】浅谈关闭窗口

    1.关闭全部窗口(主窗口+所有的子窗口) 在逻辑界面中写入 sys.exit(0) 2.关闭子窗口(其他窗口不关闭) self.close()

  2. byte数组存储到mysql

    public int AddVeinMessage(byte[] data)//插入数据库 { using (BCSSqlConnection = new MySqlConnection(strCon ...

  3. 【Loadrunner】Loadrnner 参数化策略

    Loadrnner 参数化策略 参数化策略 关键:类型+数据+策略 1.Select next row ( 如何取) 选择下一行 1)Sequential:顺序的 每个VU都从第一行开始,顺序依次向下 ...

  4. what's the 场外交易

    出自 MBA智库百科(https://wiki.mbalib.com/)   什么是场外交易 场外交易是指证券投资机构之间不通过股票交易所,而以电话.电传等方式相互进行的股票交易.           ...

  5. 技嘉主板BIOS恢复方法

    技嘉的dual bios技术的原理很简单,在main bios能启动的情况下,backup bios不会对bios进行检测.只有当main bios出现故障(这个故障一般是软件故障)才会从backup ...

  6. JavaScript 全栈工程师培训教程 - 阮一峰

    http://www.ruanyifeng.com/blog/2016/11/javascript.html https://github.com/ruanyf/jstraining 我现在的技术方向 ...

  7. Java 基础 引用数据类型 ArrayList集合

    引用数据类型(类) 分类 提到引用数据类型(类),其实我们对它并不陌生,如使用过的Scanner类.Random类. 我们可以把类的类型为两种: 第一种,Java为我们提供好的类,如Scanner类, ...

  8. PLSQL的SQL%NOTFOUND的使用场景

    SELECT * INTO v_ticketStorageRow FROM BDM_TICKET_STORAGE WHERE p_startTicketNo >= START_NO_ AND p ...

  9. 21-Python3 模块

    fibo.py文件 def fib(n): a,b = 0,1 while b<n: print(b,end='') a,b = b,a+b print() def fib2(n): resul ...

  10. [LeetCode] 586. Customer Placing the Largest Number of Orders_Easy tag;SQL

    Query the customer_number from the orders table for the customer who has placed the largest number o ...