Я новичок в Hadoop. Я пытался изменить пример WordCount, чтобы выполнить следующую задачу (установив ключ для второго элемента, а четвертый и пятый элементы соответствуют соответствующему значению для этого ключа, а затем сгруппируйте их вместе на основе значений ключа и напишите окончательный результаты в текстовый файл):Как написать окончательный вывод Hadoop Reducer в текстовый файл?
Input.txt :
a:b:c:d:e:f
g:h:i:j:k:l
m:b:n:o:p:q
Output.txt :
b:d:o:e:p
h:j:k
Вот мой код:
public class Test {
public static class Map extends MapReduceBase implements
Mapper<LongWritable, Text, Text, Text> {
private Text word = new Text();
public void map(LongWritable key, Text value,
OutputCollector<Text, Text> output, Reporter reporter)
throws IOException {
String [] temp = value.toString().split(":");
String remainder = temp[3] + ":" +temp[4];
output.collect(new Text(temp[1]), new Text(remainder));
}
}
public static class Reduce extends MapReduceBase implements
Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterator<Text> values,
OutputCollector<Text, Text> output, Reporter reporter)
throws IOException {
String temp ="";
while (values.hasNext()) {
temp = temp + values.next().toString();
}
//String remainder = ":" +temp;
output.collect(key,new Text(temp));// point
// :
// distance
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(Test.class);
conf.setJobName("pivotpoints");
System.out.println(conf.getNumMapTasks() + "map runs");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(Text.class);
conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
}
}
Это выход я получаю из кода выше:
part-00000 :
b d:eo:p
h j:k
Итак, мой Вопрос: Как заставить Hadoop написать окончательный вывод в текстовый файл в заданном формате с использованием специального символа разделения?
Установите свойство mapreduce.output.textoutputformat.separator в конфигурациях вашего основного класса. conf.set ("mapreduce.output.textoutputformat.separator", ":"); –
@ ARunAK, Большое спасибо Аруну, он решил первую часть моей проблемы. Теперь вывод выглядит так: «b: d: eo: p», но я хочу «b: d: e: o: p»; в которой он сгруппировал две пары (ключ: «b», значение: «d: e») и (клавиша: «b», значение: «o: p»), которые имеют один и тот же ключ. –
Также, как написать окончательный вывод в текстовый файл? –