一 代码
Wordcount.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.LongWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class WordCount {
public static class WordCountMap extends
Mapper<LongWritable, Text, Text, IntWritable> {
private final IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer token = new StringTokenizer(line);
while (token.hasMoreTokens()) {
word.set(token.nextToken());
context.write(word, one);
}
}
}
public static class WordCountReduce extends
Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf);
job.setJarByClass(WordCount.class);
job.setJobName("wordcount");
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(WordCountMap.class);
job.setReducerClass(WordCountReduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
}
}
二 构建运行
1、编译
[root@localhost word_count]# ll
total 4
drwxr-xr-x. 2 root root 101 Aug 20 14:27 word_count_class
-rwxr-xr-x. 1 root root 2132 Aug 20 14:22 WordCount.java
[root@localhost word_count]# javac -classpath /opt/hadoop-1.2.1/hadoop-core-1.2.1.jar:/opt/hadoop-1.2.1/lib/commons-cli-1.2.jar -d word_count_class/ WordCount.java
[root@localhost word_count]# cd word_count_class/
[root@localhost word_count_class]# ls
WordCount.class WordCount$WordCountMap.class WordCount$WordCountReduce.class
2、打包
[root@localhost word_count_class]# jar -cvf wordcount.jar *.class
added manifest
adding: WordCount.class(in = 1539) (out= 772)(deflated 49%)
adding: WordCount$WordCountMap.class(in = 1829) (out= 767)(deflated 58%)
adding: WordCount$WordCountReduce.class(in = 1645) (out= 687)(deflated 58%)
[root@localhost word_count_class]# ls
WordCount.class wordcount.jar WordCount$WordCountMap.class WordCount$WordCountReduce.class
3、准备输入文件file1和输入文件file2
[root@localhost input]# ls
file1 file2
file1的内容:
hello world
hello hadoop
hadoop file system
hadoop java api
hello java
file2的内容:
new file
hadoop file
hadoop new world
hadoop free home
hadoop free school
4、将输入文件提交HDFS
[root@localhost word_count]# hadoop fs -mkdir input_wordcount
Warning: $HADOOP_HOME is deprecated.
[root@localhost word_count]# hadoop fs -put input/* input_wordcount/
Warning: $HADOOP_HOME is deprecated.
[root@localhost word_count]# hadoop fs -ls
Warning: $HADOOP_HOME is deprecated.
Found 2 items
drwxr-xr-x - root supergroup 0 2017-08-20 12:44 /user/root/input
drwxr-xr-x - root supergroup 0 2017-08-20 14:41 /user/root/input_wordcount
[root@localhost word_count]# hadoop fs -ls input_wordcount
Warning: $HADOOP_HOME is deprecated.
Found 2 items
-rw-r--r-- 3 root supergroup 71 2017-08-20 14:41 /user/root/input_wordcount/file1
-rw-r--r-- 3 root supergroup 74 2017-08-20 14:41 /user/root/input_wordcount/file2
[root@localhost word_count]# hadoop fs -cat input_wordcount/file1
Warning: $HADOOP_HOME is deprecated.
hello world
hello hadoop
hadoop file system
hadoop java api
hello java
5、任务提交
[root@localhost word_count]# hadoop jar word_count_class/wordcount.jar WordCount input_wordcount output_wordcount
Warning: $HADOOP_HOME is deprecated.
17/08/20 14:50:30 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
17/08/20 14:50:31 INFO input.FileInputFormat: Total input paths to process : 2
17/08/20 14:50:31 INFO util.NativeCodeLoader: Loaded the native-hadoop library
17/08/20 14:50:31 WARN snappy.LoadSnappy: Snappy native library not loaded
17/08/20 14:50:33 INFO mapred.JobClient: Running job: job_201708201140_0001
17/08/20 14:50:34 INFO mapred.JobClient: map 0% reduce 0%
17/08/20 14:51:20 INFO mapred.JobClient: map 100% reduce 0%
17/08/20 14:51:45 INFO mapred.JobClient: map 100% reduce 100%
17/08/20 14:51:51 INFO mapred.JobClient: Job complete: job_201708201140_0001
17/08/20 14:51:52 INFO mapred.JobClient: Counters: 29
17/08/20 14:51:52 INFO mapred.JobClient: Job Counters
17/08/20 14:51:52 INFO mapred.JobClient: Launched reduce tasks=1
17/08/20 14:51:52 INFO mapred.JobClient: SLOTS_MILLIS_MAPS=81389
17/08/20 14:51:52 INFO mapred.JobClient: Total time spent by all reduces waiting after reserving slots (ms)=0
17/08/20 14:51:52 INFO mapred.JobClient: Total time spent by all maps waiting after reserving slots (ms)=0
17/08/20 14:51:52 INFO mapred.JobClient: Launched map tasks=2
17/08/20 14:51:52 INFO mapred.JobClient: Data-local map tasks=2
17/08/20 14:51:52 INFO mapred.JobClient: SLOTS_MILLIS_REDUCES=24253
17/08/20 14:51:52 INFO mapred.JobClient: File Output Format Counters
17/08/20 14:51:52 INFO mapred.JobClient: Bytes Written=83
17/08/20 14:51:52 INFO mapred.JobClient: FileSystemCounters
17/08/20 14:51:52 INFO mapred.JobClient: FILE_BYTES_READ=301
17/08/20 14:51:52 INFO mapred.JobClient: HDFS_BYTES_READ=381
17/08/20 14:51:52 INFO mapred.JobClient: FILE_BYTES_WRITTEN=156847
17/08/20 14:51:52 INFO mapred.JobClient: HDFS_BYTES_WRITTEN=83
17/08/20 14:51:52 INFO mapred.JobClient: File Input Format Counters
17/08/20 14:51:52 INFO mapred.JobClient: Bytes Read=145
17/08/20 14:51:52 INFO mapred.JobClient: Map-Reduce Framework
17/08/20 14:51:52 INFO mapred.JobClient: Map output materialized bytes=307
17/08/20 14:51:52 INFO mapred.JobClient: Map input records=10
17/08/20 14:51:52 INFO mapred.JobClient: Reduce shuffle bytes=307
17/08/20 14:51:52 INFO mapred.JobClient: Spilled Records=50
17/08/20 14:51:52 INFO mapred.JobClient: Map output bytes=245
17/08/20 14:51:52 INFO mapred.JobClient: Total committed heap usage (bytes)=246751232
17/08/20 14:51:52 INFO mapred.JobClient: CPU time spent (ms)=5290
17/08/20 14:51:52 INFO mapred.JobClient: Combine input records=0
17/08/20 14:51:52 INFO mapred.JobClient: SPLIT_RAW_BYTES=236
17/08/20 14:51:52 INFO mapred.JobClient: Reduce input records=25
17/08/20 14:51:52 INFO mapred.JobClient: Reduce input groups=11
17/08/20 14:51:52 INFO mapred.JobClient: Combine output records=0
17/08/20 14:51:52 INFO mapred.JobClient: Physical memory (bytes) snapshot=382996480
17/08/20 14:51:52 INFO mapred.JobClient: Reduce output records=11
17/08/20 14:51:52 INFO mapred.JobClient: Virtual memory (bytes) snapshot=2590666752
17/08/20 14:51:52 INFO mapred.JobClient: Map output records=25
6、查看结果
[root@localhost word_count]# hadoop fs -ls output_wordcount
Warning: $HADOOP_HOME is deprecated.
Found 3 items
-rw-r--r-- 3 root supergroup 0 2017-08-20 14:51 /user/root/output_wordcount/_SUCCESS
drwxr-xr-x - root supergroup 0 2017-08-20 14:50 /user/root/output_wordcount/_logs
-rw-r--r-- 3 root supergroup 83 2017-08-20 14:51 /user/root/output_wordcount/part-r-00000
[root@localhost word_count]# hadoop fs -cat output_wordcount/part-r-00000
Warning: $HADOOP_HOME is deprecated.
api 1
file 3
free 2
hadoop 7
hello 3
home 1
java 2
new 2
school 1
system 1
world 2
相关推荐
WordCount 是 MapReduce 框架中经典的入门示例,它统计文本文件中每个单词出现的次数。在这个案例中,我们将深入探讨如何在 Hadoop 环境中使用 MapReduce 实现 WordCount。 【描述】在 Hadoop 环境中,WordCount 的...
它将相同键的所有值(即单词计数)相加,得到每个单词的总出现次数。例如,如果Reduce接收到多个("hello", "1")键值对,它会将它们合并为一个("hello", "N"),其中N是所有"hello"的总和。 现在,我们来看如何将这个...
单词计数(WordCount)的任务是对一组输入文档中的单词进行分别计数。假设文件的量比较大,每个文档又包含大量的单词,则无法使用传统的线性程序进行处理,而这类问题正是 MapReduce 可以发挥优势的地方。 在前面...
MapReduce之Wordcount代码实现 接着,我们进入MapReduce WordCount程序的代码实现部分。WordCount程序由两个主要部分组成,即Mapper类和Reducer类。 Mapper类代码解析: ```java publicstaticclassMapextendsMapper...
### Python 实现 MapReduce 的 WordCount 示例详解 #### 一、引言 MapReduce 是 Hadoop 生态系统中的一种编程模型,主要用于大规模数据集的并行处理。它通过两个主要阶段——`Map` 和 `Reduce` 来实现数据处理任务...
分布式网络环境中的MapReduce编程模型,以WordCount程序的实现为例,展示了如何在伪分布式模式下完成文本的单词计数功能。WordCount是一个基础程序,广泛用于演示分布式计算模型的基本原理,其核心操作分为Map(映射...
- **背景**: WordCount是最经典的MapReduce示例之一,用于统计文件中每个单词出现的次数。 - **步骤**: - 使用Java编写Mapper类,处理输入的文本行,提取单词并计数。 - 使用Java编写Reducer类,汇总来自Mapper的...
在MapReduce框架中,"WordCount"是一个经典的例子,用于演示如何处理大数据并进行简单的统计。这个任务的主要目标是计算文本文件中每个单词出现的次数。在这个特定的案例中,我们不仅实现了基本的WordCount功能,还...
本篇文章将探讨如何在Hadoop平台上利用Python实现WordCount,一个经典的MapReduce示例。 Hadoop是Apache基金会的一个项目,其目标是解决大数据处理的效率问题。MapReduce是Hadoop中的核心组件,负责数据的并行处理...
WordCount是最简单的MapReduce应用示例,用于统计文本中各个单词出现的次数。Map阶段,Mapper读取文本行,分割单词并生成<单词, 1>键值对;Reduce阶段,Reducer汇总相同单词的所有计数,得出每个单词的总数。 3. *...
在WordCount例子中,Reducer会接收到所有带有相同单词键的中间键值对,将它们的值(出现次数)求和,生成最终的单词计数结果。 **IntelliJ IDEA与Maven配置MapReduce**: 要在IntelliJ IDEA中配置MapReduce开发环境...
在本项目"mapreduce-wordcount"中,我们将探讨如何在Go语言和Cassandra数据库(利用Java API)环境中实现一个简单的MapReduce任务,该任务用于统计文本中的单词数量。 首先,让我们了解MapReduce的基本原理。它分为...
总的来说,WordCount程序清晰地展示了MapReduce的基本工作原理,它将大文件中的每个单词作为key,出现次数作为value,通过map和reduce两个步骤,完成了对大量文本的高效统计。这个例子对于理解和应用MapReduce框架...
Reducer 收集所有来自 Map 阶段的 `(word, 1)` 键值对,对相同单词的计数进行累加,生成最终的 `(word, count)` 键值对。在 `WcReducer` 类中,我们将相同的单词键合并,并累加其对应的值。 ```java public class ...
`Mapper`负责将原始文本拆分成单词,`Reducer`则对单词进行计数并排序,最后`Driver`类用于启动和配置整个MapReduce作业。这个过程展示了MapReduce处理大数据的基本流程,即通过分布式的计算,实现了高效的数据分析...
在这个示例中,WordCount的主要任务是统计文本文件中每个单词出现的次数。以下是实现的关键步骤: 1. **Mapper**:在映射阶段,Map函数接收键值对(通常是行号和整行文本),并将其分割成单词与计数。这通常通过...
字数 使用Java的Hadoop MapReduce字数统计 运行: hadoop jar wordcount.jar "input_folder" "output_folder" “ input_folder”和“ output_folder”是HDFS上的文件夹。
在"Java实现Hadoop下词配对Wordcount计数代码实现"这个项目中,我们的目标是读取文档,对每一行进行处理,去除标点符号,将所有单词转换为小写,然后统计每个单词出现的次数。以下是一般的步骤: 1. **Mapper阶段**...
通过以上介绍,我们了解了MapReduce的基本原理以及如何利用Java编程语言实现一个简单的单词计数任务。MapReduce的核心思想在于将大数据问题分割成小问题,并在多个节点上并行处理,最后将结果合并。这种模式非常适合...