一 需求
這個案例的需求很簡單
現(xiàn)在這里有一個文本wordcount.txt,內(nèi)容如下
現(xiàn)要求你使用 mapreduce 框架統(tǒng)計每個單詞的出現(xiàn)個數(shù)?
這樣一個案例雖然簡單但可以讓新學(xué)習(xí)大數(shù)據(jù)的同學(xué)熟悉 mapreduce 框架
二 準(zhǔn)備工作
(1)創(chuàng)建一個 maven 工程,maven 工程框架可以選擇quickstart
(2)在properties中添加 hadoop.version,導(dǎo)入依賴,pom.xml內(nèi)容如下文章來源:http://www.zghlxwxcb.cn/news/detail-680829.html
<?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>org.example</groupId>
<artifactId>maven_hadoop</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</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>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<hadoop.version>3.1.3</hadoop.version>
</properties>
</project>
(3)準(zhǔn)備數(shù)據(jù),創(chuàng)建兩個文件夾 in,out(一個是輸入文件,一個是輸出文件),輸入文件放在 in 文件夾中文章來源地址http://www.zghlxwxcb.cn/news/detail-680829.html
三 編寫 WordCountMapper 類
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;
// <0, hello java, hello, 1 >
// <0, hello java, java, 1 >
// alt + ins
public class WordCountMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
Text text = new Text();
IntWritable intWritable = new IntWritable();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
System.out.println("WordCountMap stage Key:"+key+" Value:"+value);
String[] words = value.toString().split(" "); // "hello java"--->[hello,java]
for (String word :
words) {
text.set(word);
intWritable.set(1);
context.write(text,intWritable); //<hello,1>,<java,1>
}
}
}
四 編寫 WordCountReducer 類
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class WordCountReduce extends Reducer<Text, IntWritable, Text, LongWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
System.out.println("Reduce stage Key:" + key + " Values:" + values.toString());
int count = 0;
for (IntWritable intWritable :
values) {
count+=intWritable.get();
}
LongWritable longWritable = new LongWritable(count);
System.out.println("ReduceResult key:"+key+" resultValue:"+longWritable.get());
context.write(key,longWritable);
}
}
五 編寫WordCountDriver 類
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class WordCountDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(WordCountDriver.class);
// 設(shè)置job的map階段 工作任務(wù)
job.setMapperClass(WordCountMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// 設(shè)置job的reduce階段 工作任務(wù)
job.setReducerClass(WordCountReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
// 指定job map階段的輸入文件的路徑
FileInputFormat.setInputPaths(job, new Path("D:\\bigdataworkspace\\kb23\\hadoopstu\\in\\wordcount.txt"));
// 指定job reduce階段的輸出文件路徑
Path path = new Path("D:\\bigdataworkspace\\kb23\\hadoopstu\\out1");
FileSystem fileSystem = FileSystem.get(path.toUri(), conf);
if (fileSystem.exists(path))
fileSystem.delete(path,true);
FileOutputFormat.setOutputPath(job, path);
// 啟動job
job.waitForCompletion(true);
}
}
到了這里,關(guān)于hadoop 學(xué)習(xí):mapreduce 入門案例一:WordCount 統(tǒng)計一個文本中單詞的個數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!