Hadoop实验:关于MapReduce词频统计的实验步骤

需要搭建帮助的可以去taobao搜索Easy Company技术服务,谢谢!!!

需要搭建帮助的可以去taobao搜索Easy Company技术服务,谢谢!!!

一、在本地创建两个文本文件

创建 wordfile1.txt 文件,内容为:

bash 复制代码
I love Spark

I love Hadoop

I love WangJianXiong

创建 wordfile2.txt 文件,内容为:

bash 复制代码
Hadoop is good 
Spark is fast
WangJianXiong is good

将本地文件复制到docker容器内

bash 复制代码
docker cp /usr/local/hadoop-docker/wordfile1.txt 88ec9e3975c7:/root/wordfile1.txt
docker cp /usr/local/hadoop-docker/wordfile2.txt 88ec9e3975c7:/root/wordfile2.txt

二、启动 HDFS 并创建 input 目录

bash 复制代码
hadoop fs -mkdir /input
hadoop fs -ls /

三、上传文件到 HDFS 的 input 目录

bash 复制代码
hadoop fs -put /root/wordfile1.txt /input
hadoop fs -put /root/wordfile2.txt /input
hadoop fs -ls /input

四、编写 MapReduce 程序

WordCount.java

java 复制代码
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 {

    // Mapper 类
    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);
            }
        }
    }

    // Reducer 类
    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);
        }
    }

    // Main 方法
    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);
    }
}

五、编译和运行程序

1.将代码保存到 WordCount.java 文件中

bash 复制代码
vi WordCount.java

按下 i 进入 插入模式,将上述的WordCount代码插入到此WordCount.java 文件中,:wq保存退出

2.保存完代码后,运行以下命令查看文件内容

bash 复制代码
cat WordCount.java

3.编译代码

bash 复制代码
javac -classpath $(hadoop classpath) -d . WordCount.java

$(hadoop classpath):自动获取 Hadoop 所需的类路径。

-d .:编译输出目录设置为当前目录。

bash 复制代码
jar -cvf wordcount.jar *.class

-cvf:创建一个新的 JAR 文件,显示详细信息。

wordcount.jar:输出的 JAR 文件名。

检查 wordcount.jar 是否已生成

bash 复制代码
ls -l wordcount.jar

4.运行 MapReduce 作业

将打包的 wordcount.jar 运行在 Hadoop 集群上

bash 复制代码
hadoop jar wordcount.jar WordCount /input /output

六、查看输出结果

bash 复制代码
hadoop fs -cat /output/part-r-00000
相关推荐
B612 little star king13 分钟前
力扣29. 两数相除题解
java·算法·leetcode
野犬寒鸦14 分钟前
力扣hot100:环形链表(快慢指针法)(141)
java·数据结构·算法·leetcode·面试·职场和发展
上官浩仁19 分钟前
springboot synchronized 本地锁入门与实战
java·spring boot·spring
Gogo81621 分钟前
java与node.js对比
java·node.js
SmartJavaAI27 分钟前
Java调用Whisper和Vosk语音识别(ASR)模型,实现高效实时语音识别(附源码)
java·人工智能·whisper·语音识别
用户37215742613531 分钟前
Python 高效实现 Word 转 PDF:告别 Office 依赖
java
iGarment32 分钟前
服装采购跟单系统的高效管理实践
大数据·经验分享·云计算
渣哥36 分钟前
Java ThreadPoolExecutor 动态调整核心线程数:方法与注意事项
java
Miraitowa_cheems1 小时前
LeetCode算法日记 - Day 38: 二叉树的锯齿形层序遍历、二叉树最大宽度
java·linux·运维·算法·leetcode·链表·职场和发展
闯闯桑1 小时前
Spark 中spark.implicits._ 中的 toDF和DataFrame 类本身的 toDF 方法
大数据·ajax·spark·scala