为什么不能将values.next() (即IntWritable对象)传递给文件哈希集,也就是IntWriteable?(引用还原器类)
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
public class LineIndexer{ MAPPER类
public static class LineIndexMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
private final static Text word = new Text();
private final static Text location = new Text();
public void map(LongWritable key, Text val, OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
// get the filename where this line came from
FileSplit fileSplit = (FileSplit)reporter.getInputSplit();
IntWritable fileNo = new IntWritable(Integer.parseInt(fileSplit.getPath().getName()));
String line = val.toString();
StringTokenizer itr = new StringTokenizer(line.toLowerCase());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
output.collect(word, fileNo);
}
}
}减速器级
public static class LineIndexReducer extends MapReduceBase implements Reducer<Text, IntWritable, Text, Text> {
private final static HashSet<IntWritable> files = new HashSet<IntWritable>();
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, Text> output, Reporter reporter)
throws IOException {
files.clear();
int count=0;
StringBuilder toReturn = new StringBuilder();
StringBuilder keyfreq = new StringBuilder();
System.out.println("values"+values);
while (values.hasNext()){
//String filename = values.next().toString();
System.out.println("value.next"+values.next());
if( !(files.contains(values.next()))){
files.add(values.next());
if (count!=0)
toReturn.append("-> ");
count++;
toReturn.append(values.next());
}
}
IntWritable freq = new IntWritable(count);
keyfreq.append(key.toString());
keyfreq.append("|");
keyfreq.append(freq);
output.collect(new Text(keyfreq.toString()), new Text(toReturn.toString()));
}
}RUN方法
public static void run(String input, String output){
JobClient client = new JobClient();
JobConf conf = new JobConf(LineIndexer.class);
conf.setJobName("InvertedIndex");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(conf, new Path(input));
FileOutputFormat.setOutputPath(conf, new Path(output));
conf.setMapperClass(LineIndexMapper.class);
conf.setReducerClass(LineIndexReducer.class);
client.setConf(conf);
try {
JobClient.runJob(conf);
} catch (Exception e) {
e.printStackTrace();
}
}主要方法
public static void main(String[] args) {
if( args.length != 2 ){
System.err.println("InvertedIndex <input_dir> <output_dir>");
}else{
run(args[0], args[1]);
}
}
}错误:
For lines 49 and 50 of LineIndexReducer class
line 49 :if( !(files.contains(values.next()))){
line 50: files.add(values.next());org.apache.hadoop.mapred.Task$ValuesIterator.next(Task.java:121 at org.apache.hadoop.mapred.ReduceTask$ReduceValuesIterator.moveToNext (ReduceTask.java:250),org.apache.hadoop.mapred.ReduceTask$ReduceValuesIterator.next(ReduceTask.java:246) at LineIndexer$LineIndexReducer.reduce(LineIndexer.java:49),LineIndexer$LineIndexReducer.reduce(LineIndexer.java:1) at org.apache.hadoop.mapred.ReduceTask.runOldReducer(ReduceTask.java:522)在org.apache.hadoop.mapred.ReduceTask.run(ReduceTask.java:421) at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:398)
发布于 2015-02-03 08:40:09
您应该只调用next()一次:
while (values.hasNext()){
IntWritable filename = values.next();
System.out.println("value.next" + filename );
if( !(files.contains(filename))){
files.add(filename);
if (count!=0)
toReturn.append("-> ");
count++;
toReturn.append(filename);
}
}发布于 2016-12-15 00:40:28
我看到了一个问题,您的地图减少代码,这不是不正确的,但可怕的惩罚系统。它是地图功能:
public void map(LongWritable key, Text val, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
FileSplit fileSplit = (FileSplit)reporter.getInputSplit();
IntWritable fileNo = new IntWritable(Integer.parseInt(fileSplit.getPath().getName()));
}您将对每条记录执行此操作,但是对于每个InputSplit,它不会改变。移到setup()函数
https://stackoverflow.com/questions/28294352
复制相似问题