代码:

/**
* 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();
} } }
 
 
打成jar包,放到linux主机服务器上
执行下面命令

[root@node4 Desktop]# hadoop jar hbaseAndMapR.jar com.zhang.hbaseandmapreduce.HBaseAndMapReduce
遇到问题:
注意:
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
这两行代码设置的key,value类型,是设置mapper的输出key和value.
---------------------------------------------------------------------
》1:出现了三种异常
这个是在hadoop-root-namenode-node4.out出现的异常
(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
同时hbase的日志中也出现异常
MB of 1 GB physical memory used; 812.3 MB of 2.1 GB virtual memory used
 
从日志上这些日志都是在说内存的问题,在看了hadoop的异常日志发现内存挺正常的,所以觉得应该不是hadoop的内存不够才导致出现这种异常,但是发现hadoop有超时的异常,所以我修改了mapper的超时时间;既然不是hadoop的内存问题那就应该是hbase的内存问题了,所以我修改了hbase的配置
<property>
<name>mapred.task.timeout</name>
<value>180000</value>
</property>
 
habse--env.sh 修改了以下内容
# 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的更多相关文章

  1. HBase概念学习(七)HBase与Mapreduce集成

    这篇文章是看了HBase权威指南之后,依据上面的解说搬下来的样例,可是略微有些不一样. HBase与mapreduce的集成无非就是mapreduce作业以HBase表作为输入,或者作为输出,也或者作 ...

  2. 基于 Eclipse 的 MapReduce 开发环境搭建

    文 / vincentzh 原文连接:http://www.cnblogs.com/vincentzh/p/6055850.html 上周末本来要写这篇的,结果没想到上周末自己环境都没有搭起来,运行起 ...

  3. Hadoop MapReduce开发最佳实践(上篇)

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  4. 【Hadoop学习之八】MapReduce开发

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 伪分布式:HDFS和YARN 伪分 ...

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

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

  6. [转] Hadoop MapReduce开发最佳实践(上篇)

    前言 本文是Hadoop最佳实践系列第二篇,上一篇为<Hadoop管理员的十个最佳实践>. MapRuduce开发对于大多数程序员都会觉得略显复杂,运行一个WordCount(Hadoop ...

  7. hadoop程序MapReduce之WordCount

    需求:统计一个文件中所有单词出现的个数. 样板:word.log文件中有hadoop hive hbase hadoop hive 输出:hadoop 2 hive 2 hbase 1 MapRedu ...

  8. HBase设计与开发

    HBase设计与开发 @(HBase) 适合HBase应用的场景 成熟的数据分析主题,查询模式已经确定且不会轻易改变. 传统数据库无法承受负载. 简单的查询模式. 基本概念 行健:是hbase表自带的 ...

  9. MaxCompute Studio提升UDF和MapReduce开发体验

    原文链接:http://click.aliyun.com/m/13990/ UDF全称User Defined Function,即用户自定义函数.MaxCompute提供了很多内建函数来满足用户的计 ...

随机推荐

  1. Codeforces Round #166 (Div. 2) A. Beautiful Year【暴力枚举/逆向思维/大于当前数且每个位数不同】

    A. Beautiful Year time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. Python与数据库[2] -> 关系对象映射/ORM[0] -> ORM 与 sqlalchemy 模块

    ORM 与 sqlalchemy 1 关于ORM / About ORM 1.1 ORM定义 / Definition of ORM ORM(Object Relational Mapping),即对 ...

  3. redis的lua脚本拓展,返回nil及其判断

    redis自带的lua脚本 127.0.0.1:6379> hget team wyc "{\"name\":\"wyycc\",\" ...

  4. SpringAop名词解释+基于xml的配置

    1,AOP名词解释 2,AOP演示 (1)导包: (2)准备目标对象 package com.songyan.service; import org.aspectj.lang.ProceedingJo ...

  5. 集合框架(06)Arrays

    Arrays Arrays:用于操作数组的工具类,里面都是静态方法 ---数组变集合 1.asList:将数组变成List集合 把数组变成list集合的好处?可以使用集合的思想和方法来操作数组中的元素 ...

  6. 在java代码中设置margin

    我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...

  7. openfire源码修改后如何打包部署到linux服务器上

    原文:http://blog.csdn.net/jinzhencs/article/details/50457152 1.linux版本的3.10.3解压部署启动(过程略,参考我的另一篇博文http: ...

  8. NSPredicate 应用 --数组如何一键去重,如何一行代码筛选,请慢慢看来

    1.去重 NSArray * uniqueVarValueArray= [origArray valueForKeyPath:@"@distinctUnionOfObjects.VarKey ...

  9. bash: /bin/bash^M: bad interpreter: No such file or directory

    在windows下编写shell脚本在linux下运行会出报错: [hadoop@master data]$ ./load_ods_table.sh -bash: ./load_ods_table.s ...

  10. 最长公共字串算法, 文本比较算法, longest common subsequence(LCS) algorithm

    ''' merge two configure files, basic file is aFile insert the added content of bFile compare to aFil ...