需求描述:
利用mapreduce,统计单词出现的次数
设计思路:
代码设计:
目录结构:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.yanlong</groupId>
<artifactId>hadoop-04</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.7.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>cn.itcast.mapreduce.flowsum1.WordCountRunner</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
WordCountMapper
package cn.itcast.mapreduce.flowsum1;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
import java.io.Serializable;
public class WordCountMapper extends Mapper<LongWritable,Text,Text,IntWritable> implements Serializable{
@Override
protected void map(LongWritable key, Text value,
Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] words = line.split(" ");
for (String word : words) {
context.write(new Text(word),new IntWritable(1));
}
}
}
WordCountMapperReducer
package cn.itcast.mapreduce.flowsum1;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class WordCountMapperReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
/**
*
* todo
*/
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int count=0;
for (IntWritable value : values) {
count +=value.get();
}
context.write(key,new IntWritable(count));
}
}
package cn.itcast.mapreduce.flowsum1;
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 WordCountRunner {
public static void main(String[] args) throws Exception {
//1.配置参数,用于指定mr运行时相关的参数属性
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "wordcount");
//2.指定mr运行的类
job.setJarByClass(WordCountRunner.class);
//3.指定mr程序的m,r类
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountMapperReducer.class);
//4指定map输出kv类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//指定ruduce输出的kv类型,也就是mr的最终输出结果
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//指定mr程序的输出路径
FileInputFormat.addInputPath(job,new Path("/huanhuan/input"));
FileOutputFormat.setOutputPath(job,new Path("/wordcount/out"));
//提交文本mr文本程序
//job.submit();
boolean result = job.waitForCompletion(true);//提交mr程序,并且开始任务执行监控的功能
//如果mr程序执行成功,退出0,否则1
System.exit(result?0:1);
}
}
生成jar包 :
然后把生成的jar上传到服务器上。
hadoop集群测试
查看:
补充:
结果查看:
下载查看以后看到:
本地测试:
修改代码:
准备数据:
hadoop hive allen
itcast hadoop
hadoop
查看计算结果: