gpt4 book ai didi

java - MapReduce Apache Hadoop 技术

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

我正在尝试使用 MapReduce Hadoop 技术对 wordcount 程序进行统计。我需要做的是开发一个索引字数统计应用程序,该应用程序将计算给定输入文件集中每个文件中每个字词的出现次数。此文件集存在于 Amazon S3 存储桶中。它还将计算每个单词的总出现次数。我附上了计算给定文件集中单词出现次数的代码。在此之后,我需要打印出哪个文件中出现了哪个单词,以及该特定文件中该单词出现的次数。

我知道它有点复杂,但我们将不胜感激。

map .java

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

public class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
private String pattern= "^[a-z][a-z0-9]*$";

public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
InputSplit inputSplit = context.getInputSplit();
String fileName = ((FileSplit) inputSplit).getPath().getName();
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
String stringWord = word.toString().toLowerCase();
if (stringWord.matches(pattern)){
context.write(new Text(stringWord), one);
}

}
}
}

Reduce.java

import java.io.IOException;

import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;

public class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {

public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
}

WordCount.java

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class WordCount {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();

Job job = new Job(conf, "WordCount");
job.setJarByClass(WordCount.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);

job.setNumReduceTasks(3);

job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);

job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);

FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));

job.waitForCompletion(true);
}
}

最佳答案

在映射器中,创建自定义可写 textpair这将是将文件名和单词从您的文件和值保存为 1 的输出键。

映射器输出:

<K,V> ==> <MytextpairWritable,new IntWritable(1)

您可以使用以下代码片段在映射器中获取文件名。

FileSplit fileSplit = (FileSplit)context.getInputSplit();
String filename = fileSplit.getPath().getName();

并将它们作为构造函数传递给 context.write 中的自定义可写类。像这样。

context.write(new MytextpairWritable(filename,word),new IntWritable(1));

在 reducer 端,只需对值求和,这样您就可以得到每个文件中特定单词出现的次数。 Reducer 代码应该是这样的。

public class Reduce extends Reducer<mytextpairWritable, IntWritable,mytextpairWritable, IntWritable> {


public void reduce(mytextpairWritable key, Iterable<IntWritable> values , Context context)
throws IOException, InterruptedException {
int sum = 0;
for(IntWritable val: values){
sum+=val.get();
}
context.write(key, new IntWritable(sum));
}

你的输出将是这样的。

File1,hello,2
File2,hello,3
File3,hello,1

关于java - MapReduce Apache Hadoop 技术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32911549/

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