gpt4 book ai didi

hadoop - 如何在映射器输出中收集三个参数。有什么办法

转载 作者:可可西里 更新时间:2023-11-01 16:34:17 30 4
gpt4 key购买 nike

我是 map Reduce 和 hadoop 概念的新手。所以请帮忙

我有将近 100 个文件包含这种格式的数据

conf/iceis/GochenouerT01a:::John E. Gochenouer::Michael L. Tyler:::Voyeurism, Exhibitionism, and Privacy on the Internet.

我应该通过 map reduce 算法来完成。现在在我要显示的输出中

John E. Gochenoue Voyeurism .
John E. Gochenoue Exhibitionism
John E. Gochenoue and
John E. Gochenoue privacy
John E. Gochenoue on
John E. Gochenoue the
John E. Gochenoue internet
Michael L. Tyler Voyeurism .
Michael L. Tyler Exhibitionism
Michael L. Tyler and
Michael L. Tyler privacy
Michael L. Tyler on
Michael L. Tyler the
Michael L. Tyler internet

所以现在它是单行的。所以有 'n' 行这样包含大量名称和大量书籍的行。

因此,如果我考虑一个包含 110 行的文档。我可以像这样输出我的映射器吗

John E. Gochenoue Voyeurism    1  
John E. Gochenoue Exhibitionism 3
Michael L. Tyler on 7

即说它显示名称和作品,然后是文档中单词的出现,最后在 reduce 之后,它应该显示名称,后面是名称反对它的词,以及它在 'n 中出现的词的组合频率' 文档。

我知道 output.collecter() 但它需要两个参数

output.collect(arg0, arg1)

有什么方法可以收集name、word、word occourence这三个值

下面是我的代码

public static class Map extends MapReduceBase implements
Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

public void map(LongWritable key, Text value,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
String line = value.toString();
/*
* StringTokenizer tokenizer = new StringTokenizer(line); while
* (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken());
* output.collect(word, one);
*/

String strToSplit[] = line.split(":::");
String end = strToSplit[strToSplit.length - 1];
String[] names = strToSplit[1].split("::");
for (String name : names) {
StringTokenizer tokens = new StringTokenizer(end, " ");
while (tokens.hasMoreElements()) {
output.collect(arg0, arg1)
System.out.println(tokens.nextElement());
}
}

}
}

public static class Reduce extends MapReduceBase implements
Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}

public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(example.class);
conf.setJobName("wordcount");

conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);

conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);

conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);

FileInputFormat.setInputPaths(conf, "/home/vishal/workspace/hw3data");
FileOutputFormat.setOutputPath(conf,
new Path("/home/vishal/nmnmnmnmnm"));

JobClient.runJob(conf);
}

最佳答案

诀窍是编写一个文本(hadoop 可写实现之一),其字符串内容是许多制表符分隔值。这就是您可以轻松地在映射器和缩减器之间传递复杂值的方式。

当然,更具工业实力的做法是自己写Writable。可写对象基本上是具有特殊序列化/反序列化行为的 pojo。在这种情况下,您的可写对象将具有三个属性。

关于hadoop - 如何在映射器输出中收集三个参数。有什么办法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13288877/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com