Hadoop中单词统计案例

news/2024/5/20 4:51:47 标签: hadoop, mapreduce, hdfs

需要的软件和工程代码下载地址:

Hadoop中单词统计案例(访问密码:7567):

https://url56.ctfile.com/d/34653256-48746892-4c8f2e?p=7567 icon-default.png?t=M4ADhttps://url56.ctfile.com/d/34653256-48746892-4c8f2e?p=7567
 

一、搭建本地环境

1、下载准备两个工具

Hadoop-2.7.3.tar.gz

Hadoop-2.7.3-winutils.exe.rar

2、将Hadoop-2.7.3-winutils.exe.rar解压后,其中的两个文件进行拷贝

Hadoop.dll

Wintuils.exe

3、将Hadoop-2.7.3.tar.gz解压后,找到bin目录,把上面的两个文件Hadoop.dll、Wintuils.exe拷贝到当前位置

4、配置Hadoop的环境变量

5、找到Hadoop中的日志文件log4j.properties拷贝到我们新建的Eclipse中的Maven项目中,这个日志文件是方便我们使用的,不需要写太多的配置,直接借用Hadoop中文件内容,也可以自己创建该日志文件,编写里面的内容。

(1)Hadoop中日志文件的位置

(2)拷贝到Eclipse中项目的位置

二、代码编写

1、编写Mapper

2、编写Reduce

3、编写主类

4、运行测试,首先我们先打一个JAR包

5、我导出到本地项目中了

6、将包上传到我们的虚拟机中

7、上传我们的测试文件,测试文件的文本结构如下,可以自己编写,中间使用空格隔开的。

hello everyone

hello hadoop

hello hadoop

hello hive

go home

come on

8、我们运行一下

9、我们查看一下浏览器,运行后的结果

10、在虚拟机查看一下文本内容

三、单词统计理解

(一)概念

1、单词统计的是统计一个文件中单词出现的次数,比如下面的数据源

2、其中,最终出现的次数结果应该是下面的显示

(二)那么在MapReduce中该如何编写代码并出现最终结果?

首先我们把文件上传到HDFS中(hdfs dfs –put …)

数据名称:data.txt,大小是size是2G

(三)进一步理解

1、红黄绿三个块表示的是数据存放的块

2、然后数据data.txt进入map阶段,会以<K,V>(KV对)的形式进入,K表示的是:每行首字母相对于文件头的字节偏移量,V表示的是每一行的文本。

3、那么我可以用图表示:蓝色的椭圆球表示一个map,红黄绿数据块在进入map阶段的时候,数据的形式为左边红色的<K,V>(KV对)的形式

4、经过map处理,比如String.split(“”),做一次处理,数据会在不同的红黄绿数据块中变为下面的KV形式

5、我们在配置Hadoop的时候或设置reduce的数量,假如有两个reduce

Map执行完的数据会放到对应的reduce中,如下图

6、这个地方有一个简单的原理就是

Job.setNumReduce会设置reduce的数量

而HashPartioner类可以利用 key.hashcode % reduce的结果,将不同的map结果输入到不同的reduce中,比如a-e开头的放到一个地方,e-z开头的放到一个地方,那么

7、这样的数据结果就会变成

最终出现我们想要的结果,统计完成

四、练习

1、准备的数据:data.txt。文本内容:

hello everyone

hello hadoop

hello hadoop

hello hive

go home

come on

2、项目配置的pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>



  <groupId>com.xlglvc.xx.mapredece</groupId>

  <artifactId>wordcount-client</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>



  <name>wordcount-client</name>

  <url>http://maven.apache.org</url>



 <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <maven.compiler.source>1.7</maven.compiler.source>

    <maven.compiler.target>1.7</maven.compiler.target>

  </properties>



  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>4.11</version>

      <scope>test</scope>

    </dependency>

   

    <dependency>

      <groupId>org.apache.hadoop</groupId>

      <artifactId>hadoop-client</artifactId>

      <version>2.7.3</version>

    </dependency>

  </dependencies>



  <build>

    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->

      <plugins>

        <plugin>

          <artifactId>maven-clean-plugin</artifactId>

          <version>3.0.0</version>

        </plugin>

        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->

        <plugin>

          <artifactId>maven-resources-plugin</artifactId>

          <version>3.0.2</version>

        </plugin>

        <plugin>

          <artifactId>maven-compiler-plugin</artifactId>

          <version>3.7.0</version>

        </plugin>

        <plugin>

          <artifactId>maven-surefire-plugin</artifactId>

          <version>2.20.1</version>

        </plugin>

        <plugin>

          <artifactId>maven-jar-plugin</artifactId>

          <version>3.0.2</version>

        </plugin>

        <plugin>

          <artifactId>maven-install-plugin</artifactId>

          <version>2.5.2</version>

        </plugin>

        <plugin>

          <artifactId>maven-deploy-plugin</artifactId>

          <version>2.8.2</version>

        </plugin>

      </plugins>

    </pluginManagement>

  </build>

</project>

3、Mapper文件

package com.xlglvc.xx.mapredece.wordcount_client;



import java.io.IOException;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.Mapper.Context;



public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>

{

  protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)

    throws IOException, InterruptedException

  {

    Text keyout = new Text();

    IntWritable valueout = new IntWritable(1);



    String line = value.toString();



    String[] words = line.split(" ");



    for (String word : words) {

      keyout.set(word);

      context.write(keyout, valueout);

    }

  }

}

4、Reducer文件:

package com.xlglvc.xx.mapredece.wordcount_client;



import java.io.IOException;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.Reducer.Context;



public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>

{

  protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context)

    throws IOException, InterruptedException

  {

    IntWritable valueout = new IntWritable();



    int count = 0;



    for (IntWritable value : values)

    {

      count += value.get();

    }



    valueout.set(count);



    context.write(key, valueout);

  }

}

5、主方法类WordCountDriver

package com.xlglvc.xx.mapredece.wordcount_client;



import java.io.IOException;

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.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;



public class WordCountDriver

{

  public static void main(String[] args)

    throws IOException, ClassNotFoundException, InterruptedException

  {

    Configuration conf = new Configuration();



    Job job = Job.getInstance(conf);



    job.setJarByClass(WordCountDriver.class);



    FileInputFormat.addInputPath(job, new Path(args[0]));

    FileOutputFormat.setOutputPath(job, new Path(args[1]));



    job.setMapperClass(WordCountMapper.class);

    job.setReducerClass(WordCountReducer.class);



    job.setMapOutputKeyClass(Text.class);

    job.setMapOutputValueClass(IntWritable.class);



    job.setOutputKeyClass(Text.class);

    job.setOutputValueClass(IntWritable.class);



    boolean result = job.waitForCompletion(true);

    System.exit(result ? 0 : 1);

  }

}

6、打包到虚拟机运行,方法和之前相同。


http://www.niftyadmin.cn/n/643631.html

相关文章

MyEclipse10安装包

MyEclipse10安装包.zip&#xff08;访问密码&#xff1a;7567&#xff09;: https://url56.ctfile.com/f/34653256-576352282-3896d2?p7567 https://url56.ctfile.com/f/34653256-576352282-3896d2?p7567

一个博友的SQL问题解决过程

一个博友的SQL问题解决过程环境&#xff1a;DB2 9.1问题&#xff1a;有两张表&#xff0c;一个产品表&#xff0c;一个价格表&#xff0c;一对多关系&#xff08;一个产品可以对应多个价格&#xff09;&#xff0c;要求输出结果为 产品 当前有效价格&#xff0c;优惠价格&#…

arp协议的工作过程

地址解析协议&#xff0c;即ARP&#xff08;Address Resolution Protocol&#xff09;&#xff0c;是根据IP地址获取物理地址的一个TCP/IP协议。主机发送信息时将包含目标IP地址的ARP请求广播到网络上的所有主机&#xff0c;并接收返回消息&#xff0c;以此确定目标的物理地址&…

Spring从菜鸟到高手(四)(下)使用JdbcTemplate类实现批量查询

我们知道了如何验证登陆和批量更新&#xff0c;那么我再介绍一个功能那就是批量查询那就要用到org.springframework.jdbc.core Class JdbcTemplate 类的query&#xff08;&#xff09;这个方法他返回一个List对象&#xff0c;里面存了我们所有的对象数据他接受一个RowMapper类型…

jdk8安装包

jdk-8u151-windows-x64.exe&#xff08;访问密码&#xff1a;7567&#xff09;: https://url56.ctfile.com/f/34653256-556162739-d2fbfb?p7567 jdk-8u31-windows-x64.exe&#xff08;访问密码&#xff1a;7567&#xff09;: https://url56.ctfile.com/f/34653256-556162736-a…

9月第2周回顾:四核之战热闹上演 系统之争暗流涌动

一场秋雨&#xff0c;将2007年正式告别了酷暑&#xff1b;但金秋的季节却是IT界的又一个“酷暑”。上周&#xff0c;已经让大家期待了许久的AMD四核处理器“巴塞罗那”正式登场&#xff0c;宣告了四核之战正式启幕&#xff1b;微软上周定期发布了安全补丁&#xff0c;但很少见地…

Apache Tomcat介绍与下载地址

Apache Tomcat介绍&#xff1a; Tomcat 服务器是一个免费的开放源代码的Web 应用服务器&#xff0c;属于轻量级应用服务器&#xff0c;在中小型系统和并发访问用户不是很多的场合下被普遍使用&#xff0c;是开发和调试JSP 程序的首选。对于一个初学者来说&#xff0c;可以这样…

面试100问答

面试100问答转载于:https://blog.51cto.com/241998/43824