hbase和mapreduce开发 WordCount
代码:
/**
* hello world by world 测试数据
* @author a
*
*/
public class DefinedMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
throws IOException, InterruptedException {
long num=1L;
if(null!=value){
String strValue=value.toString();
String arrValue[]=strValue.split(" ");
if(arrValue.length==4){
for(int i=0;i<arrValue.length;i++){
context.write(new Text(arrValue[i].toString()), new LongWritable(num));
}
}
}
}
}
public class DefinedReduce extends TableReducer{
@Override
protected void reduce(Object arg0, Iterable values, Context arg2) throws IOException, InterruptedException {
if(null!=values){
long num=0l;
Iterator<LongWritable> it=values.iterator();
while(it.hasNext()){
LongWritable count=it.next();
num+=Long.valueOf(count.toString());
}
Put put=new Put(String.valueOf(arg0).getBytes());//设置行键
put.add("context".getBytes(), "count".getBytes(), String.valueOf(num).getBytes());
arg2.write(arg0, put);
}
}
}
package com.zhang.hbaseandmapreduce; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; public class HBaseAndMapReduce {
public static void createTable(String tableName){
Configuration conf=HBaseConfiguration.create();
HTableDescriptor htable=new HTableDescriptor(tableName);
HColumnDescriptor hcol=new HColumnDescriptor("context");
try {
HBaseAdmin admin=new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
System.out.println(tableName+" 已经存在");
return;
}
htable.addFamily(hcol);
admin.createTable(htable);
System.out.println(tableName+" 创建成功");
} catch (IOException e) {
e.printStackTrace();
} }
public static void main(String[] args) {
String tableName="workCount";
Configuration conf=new Configuration();
conf.set(TableOutputFormat.OUTPUT_TABLE, tableName);
conf.set("hbase.zookeeper.quorum", "192.168.177.124:2181");
createTable(tableName);
try {
Job job=new Job(conf);
job.setJobName("hbaseAndMapReduce");
job.setJarByClass(HBaseAndMapReduce.class);//jar的运行主类
job.setOutputKeyClass(Text.class);//mapper key的输出类型
job.setOutputValueClass(LongWritable.class);//mapper value的输出类型
job.setMapperClass(DefinedMapper.class);
job.setReducerClass(DefinedReduce.class);
job.setInputFormatClass(org.apache.hadoop.mapreduce.lib.input.TextInputFormat.class);
job.setOutputFormatClass(TableOutputFormat.class);
FileInputFormat.addInputPath(job, new Path("/tmp/dataTest/data.text"));
System.exit(job.waitForCompletion(true) ? 0:1);
} catch (Exception e) {
e.printStackTrace();
} } }
[root@node4 Desktop]# hadoop jar hbaseAndMapR.jar com.zhang.hbaseandmapreduce.HBaseAndMapReduce
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
(1)2017-01-07 06:53:33,493 INFO org.apache.hadoop.yarn.server.nodemanager.containermanager.monitor.ContainersMonitorImpl: Memory usage of ProcessTree 14615 for container-id container_1483797859000_0001_01_000001: 80.9 MB of 2 GB physical memory used; 1.7 GB of 4.2 GB virtual memory used
(2)Detected pause in JVM or host machine (eg GC): pause of approximately 3999ms
(3)AttemptID:attempt_1462439785370_0055_m_000001_0 Timed out after 600 secs
MB of 1 GB physical memory used; 812.3 MB of 2.1 GB virtual memory used
<property>
<name>mapred.task.timeout</name>
<value>180000</value>
</property>
# The maximum amount of heap to use. Default is left to JVM default.
export HBASE_HEAPSIZE=2G
# Uncomment below if you intend to use off heap cache. For example, to allocate 8G of
# offheap, set the value to "8G".
export HBASE_OFFHEAPSIZE=2G


hbase和mapreduce开发 WordCount的更多相关文章
- HBase概念学习(七)HBase与Mapreduce集成
这篇文章是看了HBase权威指南之后,依据上面的解说搬下来的样例,可是略微有些不一样. HBase与mapreduce的集成无非就是mapreduce作业以HBase表作为输入,或者作为输出,也或者作 ...
- 基于 Eclipse 的 MapReduce 开发环境搭建
文 / vincentzh 原文连接:http://www.cnblogs.com/vincentzh/p/6055850.html 上周末本来要写这篇的,结果没想到上周末自己环境都没有搭起来,运行起 ...
- Hadoop MapReduce开发最佳实践(上篇)
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- 【Hadoop学习之八】MapReduce开发
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 伪分布式:HDFS和YARN 伪分 ...
- Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结
转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...
- [转] Hadoop MapReduce开发最佳实践(上篇)
前言 本文是Hadoop最佳实践系列第二篇,上一篇为<Hadoop管理员的十个最佳实践>. MapRuduce开发对于大多数程序员都会觉得略显复杂,运行一个WordCount(Hadoop ...
- hadoop程序MapReduce之WordCount
需求:统计一个文件中所有单词出现的个数. 样板:word.log文件中有hadoop hive hbase hadoop hive 输出:hadoop 2 hive 2 hbase 1 MapRedu ...
- HBase设计与开发
HBase设计与开发 @(HBase) 适合HBase应用的场景 成熟的数据分析主题,查询模式已经确定且不会轻易改变. 传统数据库无法承受负载. 简单的查询模式. 基本概念 行健:是hbase表自带的 ...
- MaxCompute Studio提升UDF和MapReduce开发体验
原文链接:http://click.aliyun.com/m/13990/ UDF全称User Defined Function,即用户自定义函数.MaxCompute提供了很多内建函数来满足用户的计 ...
随机推荐
- CF978B File Name【数组操作/序列判断连续出现>=3次的‘x’个数】
CF978B File Name [分析]:设置计数器cnt,计数x的个数:遇到非x,若cnt>=3的话累加多出的个数,计数器清零:若最后cnt>=3说明没遇到非x无法清零,那后部分就都是 ...
- 51 nod 1419 最小公倍数挑战【数论/互质+思维】
1419 最小公倍数挑战 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 几天以前,我学习了最小公倍数.玩得挺久了 ...
- Codeforces 954H Path Counting(DP)
题目链接 Path Counting 题意 给定一棵高度为$n$的树,给出每一层的每个点的儿子个数(某一层的所有点儿子个数相同). 令$f_{k}$为长度为$k$的路径条数,求$f_{1}, ...
- How to not display “Commit point reached - logical record count” counts
You can use the keyword silent, which is available in the options clause. You can set the followin ...
- [POJ 2397] Spiderman
Link: POJ 2397 传送门 Solution: 设$dp[i][j]$表示第$i$步走到$j$高度时经过的最高高度 分向上走和向下走两种方式转移即可 注意记录路径,最后输出时要逆序输出 (逆 ...
- @selector和SEL
遇到selector发现不是很明白,网上搜到的零零星星的介绍也不成体系,索性自己翻译一下,加深一下印象.原文来自官方API文档下的Selectors. Selectors 在OC中,selector有 ...
- 搭建Git本地服务器(转)
http://www.cnblogs.com/trying/archive/2012/06/28/2863758.html 当前任务,学习中... 公司小范围用法: 服务器上做的: 在服务器上建立一 ...
- wget jdk
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...
- Manacher回文串算法学习记录
FROM: http://hi.baidu.com/chenwenwen0210/item/482c84396476f0e02f8ec230 #include<stdio.h> #inc ...
- 2017.10.13 unable to open debugger port(127.0.0.1:10308)
参考来自:http://blog.csdn.net/qq_34360219/article/details/76169653 1.场景 突然间IDEA就跑不起项目了,报了如下的错误:unable to ...