如果文章中没有相应的单词的话,就不用出现。
首先,我们需要一个工具类,把每行的String,分解成一个一个的单词:
import java.util.ArrayList;
import java.util.List;
/**
* @author Hadoop
*
*/
public class StringUtil {
/**
* 获得字符串中单词
* @param value
* @return
*/
public static List<String> getWords(String value)
{
List<String> wordList = new ArrayList<String>();
int len = value.length();
char[] charArray = value.toCharArray();
char[] word = new char[40];
int wordIndex = 0;
for(int i = 0; i < len; i++)
{
char c = charArray[i];
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
word[wordIndex] = c;
wordIndex++;
}
else
{
if(wordIndex > 0)
{
wordList.add(String.valueOf(word, 0, wordIndex));
wordIndex = 0;
}
}
}
return wordList;
}
}
然后就是我们的mapreduce啦:
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.MapWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import com.test.util.HdfsFileUtil;
import com.test.util.Prop;
import com.test.util.StringUtil;
/**
* @author hadoop
*
*/
public class Seach {
private static Log log = LogFactory.getLog(Seach.class);
public static class Map extends Mapper<Object, Text, Text, MapWritable> {
private FileSplit split;
private static IntWritable data = new IntWritable(1);
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
split = (FileSplit)context.getInputSplit();
MapWritable map = new MapWritable();
List<String> wordList = StringUtil.getWords(value.toString());
int len = wordList.size();
for(int i = 0; i < len; i++)
{
map.put(new Text(split.getPath().getName()), data);
context.write(new Text(wordList.get(i)), map);
}
}
}
public static class Reduce extends Reducer<Text, MapWritable, Text, Text> {
public void reduce(Text key, Iterable<MapWritable> values,
Context context) throws IOException, InterruptedException {
int count = 0;
java.util.Map<String, Integer> countMap = new HashMap<String, Integer>();
Iterator<MapWritable> iterator = values.iterator();
while(iterator.hasNext())
{
MapWritable curMap = iterator.next();
String fileName = curMap.keySet().iterator().next().toString();
if(countMap.containsKey(fileName))
{
countMap.put(fileName, countMap.get(fileName) + 1);
}
else
{
countMap.put(fileName, 1);
}
count++;
}
int fCount = 0;
String value = "";
Iterator<String> it = countMap.keySet().iterator();
while(it.hasNext())
{
if(fCount > 0)
{
value += "-";
}
String fileName = it.next();
value += fileName + ":" + countMap.get(fileName);
fCount++;
}
context.write(key, new Text(count + "-" + value));
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
String inputPath = "search_in";
String outPath = "search_out";
Configuration conf = new Configuration();
conf.set("mapred.job.tracker", Prop.HADOOP_MAPRED_JOB_TRACKER);
conf.set("fs.default.name", Prop.HDFS_HOST);
HdfsFileUtil.checkAndDelete(conf, "/" + Prop.HDFS_DIRECTORY + "/" + inputPath);
HdfsFileUtil.checkAndDelete(conf, "/" + Prop.HDFS_DIRECTORY + "/" + outPath);
HdfsFileUtil.upload(conf, Prop.LOCAL_HDFS_DIRECTORY + "/" + inputPath, Prop.HDFS_DIRECTORY + "/" + inputPath);
Job job = new Job(conf, "search");
job.setJarByClass(STjoin.class);
// 设置Map和Reduce处理类
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
// 设置输出类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(MapWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
// 设置输入和输出目录
FileInputFormat.addInputPath(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outPath));
boolean exitStatus = job.waitForCompletion(true);
HdfsFileUtil.download(conf, "/" + Prop.HDFS_DIRECTORY + "/" + outPath, Prop.LOCAL_HDFS_DIRECTORY + "/" + outPath);
System.exit(exitStatus ? 0 : 1);
}
}
