需求:统计一个文件中每一个单词出现的总次数。
案例数据:
一段文章
使用 idea 创建 maven 工程,添加依赖如下:
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>cn.kgc</groupId>
<artifactId>hadoop_day1208</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>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0-cdh5.14.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0-cdh5.14.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.0-cdh5.14.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.6.0-cdh5.14.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-jobclient</artifactId>
<version>2.6.0-cdh5.14.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-auth</artifactId>
<version>2.6.0-cdh5.14.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
WCMApper
public class WCMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
private Text word=new Text();
private IntWritable count =new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] words = value.toString().trim()
.replaceAll(",|\\.|!|\\?|:|\"", "").split(" ");
for (String word : words) {
this.word.set(word);
context.write(this.word,count);
}
}
}
WCReader
public class WCReducer extends Reducer <Text, IntWritable,Text,IntWritable>{
private IntWritable count=new IntWritable();
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum=0;
for (IntWritable value : values) {
sum+=value.get();
}
count.set(sum);
context.write(key,count);
}
}
WCPartitioner
public class WCPartitioner extends Partitioner<Text, IntWritable> {
@Override
public int getPartition(Text text, IntWritable intWritable, int numberReduceTasks) {
return text.toString().length()%numberReduceTasks;
}
}
WCDriver
public class WCDriver {
public static void main(String[] args) throws Exception {
//创建hdfs访问路径配置信息
final String INPUT=args[0],OUTPUT=args[1];
Configuration config=new Configuration();
config.set("fs.defaultFS","hdfs://192.168.184.200:9000");
//如果输出路径已存在则删除
FileSystem fs=FileSystem.get(config);
fs.deleteOnExit(new Path(OUTPUT));
fs.close();
//创建mapreduce计算任务
Job job=Job.getInstance(config,"wordCount");
//设置主类,定位外部jar资源
job.setJarByClass(WCDriver.class);
//设置任务数:和文件分片数和所存储的DN数有关
job.setNumReduceTasks(2);
//设置分区器Partitioner
job.setPartitionerClass(WCPartitioner.class);
//设置Combiner
job.setCombinerClass(WCReducer.class);
//设置Maper
job.setMapperClass(WCMapper.class);
//设置Reducer
job.setReducerClass(WCReducer.class);
//设置Mapper的输出键值类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//设置Reducer的输出键值类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.setInputPaths(job,new Path(INPUT));
FileOutputFormat.setOutputPath(job,new Path(OUTPUT));
//等待所有job步骤完成
System.out.println(job.waitForCompletion(true));
}
}
打成jar包在虚拟机执行
找到jar包,传输到虚拟机中
在虚拟机中执行jar包
cn.kgc.WordCount.WCDriver为要运行的类名