大家好,我是Iggi筹裕。
今天我給大家分享的是MapReduce2-3.1.1版本的Word Count Ver2.0實(shí)驗(yàn)窄驹。
關(guān)于MapReduce的一段文字簡(jiǎn)介請(qǐng)自行查閱我的上一篇實(shí)驗(yàn)示例:MapReduce2-3.1.1 實(shí)驗(yàn)示例 單詞計(jì)數(shù)(一)
好乐埠,下面進(jìn)入正題。介紹Java操作MapReduce2組件完成Word Count Ver2.0的操作瑞眼。
首先棵逊,使用IDE建立Maven工程,建立工程時(shí)沒有特殊說(shuō)明徒像,按照向?qū)崾军c(diǎn)擊完成即可蛙讥。重要的是在pom.xml文件中添加依賴包,內(nèi)容如下圖:
image.png
待系統(tǒng)下載好依賴的jar包后便可以編寫程序了谬墙。
展示實(shí)驗(yàn)代碼:
package linose.mapreduce;
import java.io.IOException;
import java.io.OutputStreamWriter;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.io.IOUtils;
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;
//import org.apache.log4j.BasicConfigurator;
/**
* Hello MapReduce!
* Word Count V2.0
* 本示例演示如何使用MapReduce組件,添加忽略詞文件來(lái)統(tǒng)計(jì)單詞出現(xiàn)的個(gè)數(shù)
* 關(guān)于示例中出現(xiàn)的API方法可以參考如下連接:http://hadoop.apache.org/docs/r3.1.1/api/index.html
*/
public class AppVer2
{
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException
{
/**
* 設(shè)定MapReduce示例擁有HDFS的操作權(quán)限
*/
System.setProperty("HADOOP_USER_NAME", "hdfs");
/**
* 為了清楚的看到輸出結(jié)果,暫將集群調(diào)試信息缺省侵蒙。
* 如果想查閱集群調(diào)試信息,取消注釋即可算凿。
*/
//BasicConfigurator.configure();
/**
* MapReude實(shí)驗(yàn)準(zhǔn)備階段:
* 定義HDFS文件路徑
*/
String defaultFS = "hdfs://master2.linose.cloud.beijing.com:8020";
String inputPath = defaultFS + "/index.dirs/inputV2.txt";
String outputPath = defaultFS + "/index.dirs/outputV2";
String skipPath = defaultFS + "/index.dirs/patterns.txt";
/**
* 生產(chǎn)配置氓轰,并獲取HDFS對(duì)象
*/
Configuration conf = new Configuration();
conf.set("fs.defaultFS", defaultFS);
FileSystem system = FileSystem.get(conf);
/**
* 定義輸入路徑,輸出路徑
*/
Path inputHdfsPath = new Path(inputPath);
Path outputHdfsPath = new Path(outputPath);
Path stopWordPath = new Path(skipPath);
/**
* 如果實(shí)驗(yàn)數(shù)據(jù)文件不存在則創(chuàng)建數(shù)據(jù)文件
*/
if (!system.exists(inputHdfsPath)) {
FSDataOutputStream outputStream = system.create(inputHdfsPath);
OutputStreamWriter file = new OutputStreamWriter(outputStream);
file.write("芒果 菠蘿 西瓜! 橘子, 草莓. \n");
file.write("草莓 橘子 蘋果! 荔枝, 藍(lán)莓. \n");
file.write("天天 菇娘 釋迦! 軟棗子, 癩瓜, 蛇皮果. \n");
file.write("香蕉 菠蘿 鴨梨! 柚子, 蘋果. \n");
file.write("草莓 橘子 桂圓! 荔枝, 香蕉. \n");
file.write("蘋果 菠蘿 草莓! 彌猴桃, 芒果. \n");
file.write("蘋果 香蕉 提子! 橘子, 菠蘿. \n");
file.write("西瓜 蘋果 香蕉! 橙子, 提子. \n");
file.write("香蕉 鴨梨 西瓜! 葡萄, 芒果. \n");
file.write("蘋果 櫻桃 香蕉! 葡萄, 橘子. \n");
file.write("西瓜 葡萄 桃! 車?yán)遄? 香蕉, 榴蓮, 瓜, 火龍果, 荔枝. \n");
file.close();
outputStream.close();
}
/**
* 如果實(shí)驗(yàn)結(jié)果目錄存在案糙,遍歷文件內(nèi)容全部刪除
*/
if (system.exists(outputHdfsPath)) {
RemoteIterator<LocatedFileStatus> fsIterator = system.listFiles(outputHdfsPath, true);
LocatedFileStatus fileStatus;
while (fsIterator.hasNext()) {
fileStatus = fsIterator.next();
system.delete(fileStatus.getPath(), false);
}
system.delete(outputHdfsPath, false);
}
/**
* 創(chuàng)建忽略單詞文件时捌,除了要過濾標(biāo)點(diǎn)符號(hào)外炉抒,我希望過濾掉:天天、菇娘拿诸、釋迦蛤奥、軟棗子僚稿、癩瓜蚀同、蛇皮果這幾個(gè)水果
*/
system.delete(stopWordPath, false);
if (!system.exists(stopWordPath)) {
FSDataOutputStream outputStream = system.create(stopWordPath);
OutputStreamWriter file = new OutputStreamWriter(outputStream);
file.write("\\,\n");
file.write("\\.\n");
file.write("\\!\n");
file.write("天天\n");
file.write("菇娘\n");
file.write("釋迦\n");
file.write("軟棗子\n");
file.write("癩瓜\n");
file.write("蛇皮果\n");
file.close();
outputStream.close();
}
/**
* 創(chuàng)建MapReduce任務(wù)并設(shè)定Job名稱
*/
Job job = Job.getInstance(conf, "Word Count Ver2:");
job.setJarByClass(WordCountVer2.class);
/**
* 設(shè)置輸入文件、輸出文件衰猛、緩存文件
*/
FileInputFormat.addInputPath(job, inputHdfsPath);
FileOutputFormat.setOutputPath(job, outputHdfsPath);
job.addCacheFile(stopWordPath.toUri());
job.getConfiguration().setBoolean("wordcount.skip.patterns", true);
/**
* 指定Reduce類輸出類型Key類型與Value類型
*/
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
/**
* 指定自定義Map類啡省,Reduce類髓霞,開啟Combiner函數(shù)。
*/
job.setMapperClass(WordCountVer2.TokenizerMapper.class);
job.setCombinerClass(WordCountVer2.IntSumReducer.class);
job.setReducerClass(WordCountVer2.IntSumReducer.class);
/**
* 提交作業(yè)
*/
job.waitForCompletion(true);
/**
* 然后輪詢進(jìn)度结序,直到作業(yè)完成纵潦。
*/
float progress = 0.0f;
do {
progress = job.setupProgress();
System.out.println("Word Count Ver2: 的當(dāng)前進(jìn)度:" + progress * 100);
Thread.sleep(1000);
} while (progress != 1.0f && !job.isComplete());
/**
* 如果成功垃环,查看輸出文件內(nèi)容
*/
if (job.isSuccessful()) {
RemoteIterator<LocatedFileStatus> fsIterator = system.listFiles(outputHdfsPath, true);
LocatedFileStatus fileStatus;
while (fsIterator.hasNext()) {
fileStatus = fsIterator.next();
FSDataInputStream outputStream = system.open(fileStatus.getPath());
IOUtils.copyBytes(outputStream, System.out, conf, false);
outputStream.close();
System.out.println("--------------------------------------------");
}
}
}
}
展示MapReduce2-3.1.1組件編寫Word Count Ver2.0測(cè)試類:
package linose.mapreduce;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.util.HashSet;
import java.util.Set;
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.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.util.StringUtils;
public class WordCountVer2 {
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
static enum CountersEnum { INPUT_WORDS }
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
private boolean caseSensitive;
private Set<String> patternsToSkip = new HashSet<String>();
private Configuration conf;
private BufferedReader fis;
@Override
public void setup(Context context) throws IOException, InterruptedException {
conf = context.getConfiguration();
caseSensitive = conf.getBoolean("wordcount.case.sensitive", true);
if (conf.getBoolean("wordcount.skip.patterns", false)) {
URI[] patternsURIs = Job.getInstance(conf).getCacheFiles();
for (URI patternsURI : patternsURIs) {
Path patternsPath = new Path(patternsURI.getPath());
String patternsFileName = patternsPath.getName().toString();
parseSkipFile(patternsFileName);
}
}
}
private void parseSkipFile(String fileName) {
try {
fis = new BufferedReader(new FileReader(fileName));
String pattern = null;
while ((pattern = fis.readLine()) != null) {
patternsToSkip.add(pattern);
}
} catch (IOException ioe) {
System.err.println("Caught exception while parsing the cached file '" + StringUtils.stringifyException(ioe));
}
}
@Override
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String line = (caseSensitive) ? value.toString() : value.toString().toLowerCase();
for (String pattern : patternsToSkip) {
line = line.replaceAll(pattern, "");
}
StringTokenizer itr = new StringTokenizer(line);
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
Counter counter = context.getCounter(CountersEnum.class.getName(), CountersEnum.INPUT_WORDS.toString()); counter.increment(1);
}
}
}
public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
result.set(sum);
context.write(key, result);
}
}
}
下圖為測(cè)試結(jié)果:
image.png
至此涧团,MapReduce2-3.1.1 Word Count Ver2.0 實(shí)驗(yàn)示例演示完畢经磅。