hadoop2.7.0实践- WordCount

环境要求

说明:本文档为wordcount的mapreduce job编写及运行文档。

操作系统:Ubuntu14 x64位

Hadoop:Hadoop 2.7.0

Hadoop官网:http://hadoop.apache.org/releases.html

MapReduce参照官网步骤:

http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html#Source_Code

本章基于前一篇文章《hadoop2.7.0实践-环境搭建》。

1.安装Eclipse

1)下载eclipse

官网:http://www.eclipse.org/

2)解压eclipse包

$tar -xvf eclipse-jee-mars-R-linux-gtk-x86_64.tar.gz

3)启动eclipse

4)写测试程序

public class TestMore {

    public static void main(String[] args) {
        System.out.println("hello world!");
        System.out.println("I‘m so glad to see that");
    }
}

2.编写wordcount

1)jar包引入

eclipse的lib中引入的jar包

hadoop包下的share/hadoop下的各个目录都有jar包

hadoop-2.7.0/share/hadoop/common/hadoop-common-2.7.0.jar

hadoop-2.7.0/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.7.0.jar

2)编写worcount程序

对应源码

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCount {

  public static class TokenizerMapper
       extends Mapper<Object, Text, Text, IntWritable>{

    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, one);
      }
    }
  }

  public static class IntSumReducer
       extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values,
                       Context context
                       ) throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable val : values) {
        sum += val.get();
      }
      result.set(sum);
      context.write(key, result);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

3)导出jar包

取名wc.jar,直接导出到hadoop目录下。

3.运行wordcount

1)启动dfs服务

参照文件《hadoop2.7.0实践-环境搭建》。

进入hadoop目录,用cd命令。

$sbin/start-dfs.sh

对应查看网页:http://localhost:50070/

2)准备文件

hadoop-2.7.0/wctest/input目录中放入待统计文件file01

输入内容:hello world bye world

//创建hdfs目录,操作命令类似本地操作

$ bin/hdfs fs -mkdir /user
$ bin/hdfs fs -mkdir /user/a

//复制本地文件到hdfs中

$ bin/hdfs fs -put wctest/input /user/a/input

//备注:对应目录删除命令如下

delete dir:bin/hadoop fs -rm -f -r /user/a/input

对应文件http://localhost:50070/

3)启动yarn服务

$ sbin/start-yarn.sh

4)运行wordcount程序

$ bin/hadoop jar wc.jar WordCount /user/a/input /user/a/output

5)查看结果

$ bin/hadoop fs -cat /user/a/output/part-r-00000
bye 1
hello   1
world   2

常见错误及说明

1)未启动yarn时运行MapReduce程序

原因:已经配置了yarn,但没有启动引起的

调整:启动一下yarn

$ sbin/start-yarn.sh

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 01:44:22

hadoop2.7.0实践- WordCount的相关文章

hadoop2.6.0实践:002 检查伪分布式环境搭建

1.检查网络配置[[email protected] ~]# cat /etc/sysconfig/networkNETWORKING=yesHOSTNAME=hadoop-masterGATEWAY=192.168.126.2 [[email protected] ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhos

Hadoop2.6.0实践:000 环境搭建

##################### Centos6.4VM_01_os.rar ################################################准备工作/opt /opt/modules 软件安装目录 /opt/softwares 软件包(tar.bin.zip) /opt/tools(eclipse等) /opt/data(测试数据)/home/hadoop(工具和数据) ##################### Centos6.4VM_01_os.r

hadoop2.6.0实践:引入开发依赖的jar包

hadoop-2.5.0\share\hadoop\common  所有jar,hadoop-2.5.0\share\hadoop\common\lib  所有jar, hadoop-2.5.0\share\hadoop\hdfs  所有jar hadoop-2.5.0\share\hadoop\mapreduce  所有jar hadoop-2.5.0\share\hadoop\yarn  所有jar

hadoop2.6.0实践:A02 问题处理 util.NativeCodeLoader: Unable to load native-hadoop library for your platform

############################################################# hadoop "util.NativeCodeLoader: Unable to load native-hadoop library for your platform" hadoop安装完以后,经常会提示以下警告: WARN util.NativeCodeLoader: Unable to load native-hadoop library for your

hadoop2.7.0实践-环境搭建

文档说明 本文档为hadoop搭建实践文档,相关理论可到hadoop官网查看学习. 操作系统:Ubuntu14 x64位 Hadoop:Hadoop 2.7.0 Ubuntu官网:http://www.ubuntu.com/download/desktop 下载地址: http://211.167.105.77:83/1Q2W3E4R5T6Y7U8I9O0P1Z2X3C4V5B/releases.ubuntu.com/14.04.2/ubuntu-14.04.2-desktop-amd64.is

hadoop2.2.0的WordCount程序

package com.my.mapreduce.wordcount; import java.io.IOException; import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.

hadoop2.6.0实践:hadoop dfs -ls /

[[email protected] data]$ hadoop dfs -ls /DEPRECATED: Use of this script to execute hdfs command is deprecated.Instead use the hdfs command for it. Found 3 itemsdrwxr-xr-x - hadoop supergroup 0 2017-01-18 15:34 /examplesdrwx------ - hadoop supergroup

hadoop2.6.0实践:控制台入口url列表

hadoop web控制台页面的端口整理: 50070:hdfs文件管理 8088:ResourceManager 8042:NodeManager 19888:JobHistory(使用"mr-jobhistory-daemon.sh"来启动JobHistory Server)

hadoop2.6.0实践:003 检查hadoop是否可用

start-dfs.sh start-yarn.sh 1.检查hdfs hdfs dfs -ls / http://localhost:50070 2.运行例子程序 hdfs dfs -ls / hdfs dfs -mkdir -R /example/grep/input hdfs dfs -mkdir -R /example/grep/output hdfs dfs -copyFromLocal  /etc/profile  /example/grep/input hdfs dfs -rm -