[英]MapReduce job: weird output?
我正在寫我的第一份MapReduce工作。 簡單的事情:只計算文件中的字母數字字符。 我已經完成了生成我的jar文件並運行它的工作,但是除了調試輸出之外,我找不到MR作業的輸出。 請你幫助我好嗎?
我的應用程序類:
import CharacterCountMapper;
import CharacterCountReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
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;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class CharacterCountDriver extends Configured implements Tool {
@Override
public int run(String[] args) throws Exception {
// Create a JobConf using the processed configuration processed by ToolRunner
Job job = Job.getInstance(getConf());
// Process custom command-line options
Path in = new Path("/tmp/filein");
Path out = new Path("/tmp/fileout");
// Specify various job-specific parameters
job.setJobName("Character-Count");
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(CharacterCountMapper.class);
job.setReducerClass(CharacterCountReducer.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.setInputPaths(job, in);
FileOutputFormat.setOutputPath(job, out);
job.setJarByClass(CharacterCountDriver.class);
job.submit();
return 0;
}
public static void main(String[] args) throws Exception {
// Let ToolRunner handle generic command-line options
int res = ToolRunner.run(new Configuration(), new CharacterCountDriver(), args);
System.exit(res);
}
}
然后我的映射器類:
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class CharacterCountMapper extends
Mapper<Object, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
@Override
protected void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String strValue = value.toString();
StringTokenizer chars = new StringTokenizer(strValue.replaceAll("[^a-zA-Z0-9]", ""));
while (chars.hasMoreTokens()) {
context.write(new Text(chars.nextToken()), one);
}
}
}
減速器:
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class CharacterCountReducer extends
Reducer<Text, IntWritable, Text, IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int charCount = 0;
for (IntWritable val: values) {
charCount += val.get();
}
context.write(key, new IntWritable(charCount));
}
}
看起來不錯,我從IDE生成了可運行的jar文件,並按如下所示執行它:
$ ./hadoop jar ~/Desktop/example_MapReduce.jar no.hib.mod250.hadoop.CharacterCountDriver
14/11/27 19:36:42 INFO Configuration.deprecation: session.id is deprecated. Instead, use dfs.metrics.session-id
14/11/27 19:36:42 INFO jvm.JvmMetrics: Initializing JVM Metrics with processName=JobTracker, sessionId=
14/11/27 19:36:42 INFO input.FileInputFormat: Total input paths to process : 1
14/11/27 19:36:42 INFO mapreduce.JobSubmitter: number of splits:1
14/11/27 19:36:43 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_local316715466_0001
14/11/27 19:36:43 WARN conf.Configuration: file:/tmp/hadoop-roberto/mapred/staging/roberto316715466/.staging/job_local316715466_0001/job.xml:an attempt to override final parameter: mapreduce.job.end-notification.max.retry.interval; Ignoring.
14/11/27 19:36:43 WARN conf.Configuration: file:/tmp/hadoop-roberto/mapred/staging/roberto316715466/.staging/job_local316715466_0001/job.xml:an attempt to override final parameter: mapreduce.job.end-notification.max.attempts; Ignoring.
14/11/27 19:36:43 WARN conf.Configuration: file:/tmp/hadoop-roberto/mapred/local/localRunner/roberto/job_local316715466_0001/job_local316715466_0001.xml:an attempt to override final parameter: mapreduce.job.end-notification.max.retry.interval; Ignoring.
14/11/27 19:36:43 WARN conf.Configuration: file:/tmp/hadoop-roberto/mapred/local/localRunner/roberto/job_local316715466_0001/job_local316715466_0001.xml:an attempt to override final parameter: mapreduce.job.end-notification.max.attempts; Ignoring.
14/11/27 19:36:43 INFO mapreduce.Job: The url to track the job: http://localhost:8080/
14/11/27 19:36:43 INFO mapred.LocalJobRunner: OutputCommitter set in config null
14/11/27 19:36:43 INFO mapred.LocalJobRunner: OutputCommitter is org.apache.hadoop.mapreduce.lib.output.FileOutputCommitter
14/11/27 19:36:43 INFO mapred.LocalJobRunner: Waiting for map tasks
14/11/27 19:36:43 INFO mapred.LocalJobRunner: Starting task: attempt_local316715466_0001_m_000000_0
14/11/27 19:36:43 INFO mapred.Task: Using ResourceCalculatorProcessTree : [ ]
14/11/27 19:36:43 INFO mapred.MapTask: Processing split: file:/tmp/filein:0+434
14/11/27 19:36:43 INFO mapred.MapTask: Map output collector class = org.apache.hadoop.mapred.MapTask$MapOutputBuffer
然后我猜我的輸出文件將在/ tmp / fileout中。 但是,它似乎是空的:
$ tree /tmp/fileout/
/tmp/fileout/
└── _temporary
└── 0
2 directories, 0 files
有什么我想念的嗎? 誰能幫我嗎?
問候 :-)
編輯:
在CharacterCountDriver中,我用job.waitForCompletion(true)替換了job.submit()。 我得到了更詳細的輸出:
/tmp/fileout/
├── part-r-00000
└── _SUCCESS
0 directories, 2 files
但是我仍然不知道該怎么讀,_SUCCESS是空的,part-r-0000不是我所期望的:
Absorbantandyellowandporousishe 1
AreyoureadykidsAyeAyeCaptain 1
ICanthearyouAYEAYECAPTAIN 1
Ifnauticalnonsensebesomethingyouwish 1
Ohh 1
READY 1
SPONGEBOBSQUAREPANTS 1
SpongebobSquarepants 3
Spongebobsquarepants 4
Thendroponthedeckandfloplikeafish 1
Wholivesinapineappleunderthesea 1
有什么建議嗎? 我的代碼中可能有任何錯誤嗎? 謝謝。
part-r-00000是減速器輸出文件的名稱。 如果您有更多的減速器,它們將被編號為r-00001,以此類推。
如果我理解正確,則您希望程序對輸入文件中的字母數字字符計數。 但是,這不是您的代碼在做什么。 您可以更改映射器以計算每行中的字母數字字符:
String strValue = value.toString();
strValue.replaceAll("[^a-zA-Z0-9]", "");
context.write(new Text("alphanumeric", strValue.length());
這應該可以修復您的程序。 基本上,您的映射器將每行中的字母數字字符作為鍵輸出。 減速器累積每個鍵的計數。 進行我的更改后,您只需使用一個鍵:“字母數字”。 密鑰可能是其他東西,並且仍然可以使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.