MapReduce实现wordcount案例
MapReduce实现wordcount案例
1、创建maven工程
导入hadoop所需要的依赖包
<!-- 你的hadoop版本信息 -->
<properties>
<hadoop.version>3.1.4</hadoop.version>
</properties>
<!-- hadoop运行所需要的依赖包 -->
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>${hadoop.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
</dependency>
<!-- 显示日志所需要的依赖包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<!-- <verbal>true</verbal>-->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
2、创建自定义的Mapper逻辑
package wordcount;
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 java.io.IOException;
public class WordMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//这个value指的是一行文本数据
String s = value.toString();
//把一行文本数据,按照“,”的方式进行切割
String[] split = s.split(",");
for (String word:split) {
//每个数据,一个单词,对应的value值是1
context.write(new Text(word),new IntWritable(1));
}
}
}
3、自定义Mapper类
package wordcount;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class WordReduce extends Reducer<Text, IntWritable,Text,IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value:values) {
sum = sum + value.get();
}
context.write(key,new IntWritable(sum));
}
}
4、自定义测试类
package wordcount;
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.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.log4j.BasicConfigurator;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//这一句话是为了能打印日志的,进行的配置
BasicConfigurator.configure();
//1、获取到我们的job对象,让它运行
Job job = Job.getInstance(new Configuration(), "WordCount");
//2、如果我们打包成jar文件,指定我们程序的入口类是哪一个
job.setJarByClass(Test.class);
//3、从存储系统中获取到什么样的文件,这里指的是Text这样的输入流文件
job.setInputFormatClass(TextInputFormat.class);
//4、指定输入流文件的位置
TextInputFormat.addInputPath(job,new Path("E:\\hadoop\\mapreduce\\input"));
//5、设置自定义的Mapper
job.setMapperClass(WordMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//6、设置自定义的Reduce
job.setReducerClass(WordReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//7、设置Reduce个数
job.setNumReduceTasks(1);
//8、设置输出的文件以什么类型存储,这里是Text形式的输出流
job.setOutputFormatClass(TextOutputFormat.class);
//9、输出的文件夹的位置(文件中不能存在这个文件夹)
TextOutputFormat.setOutputPath(job,new Path("E:\\hadoop\\mapreduce\\output\\text19"));
//10、等待结果输出
boolean b = job.waitForCompletion(true);
//11、退出
System.exit(b?0:1);
}
}