附录E Hadoop的word count例子

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://beliefer.blog.csdn.net/article/details/77450068

注:本文是为了配合《Spark内核设计的艺术——架构设计与实现》一书的内容而编写,目的是为了节省成本、方便读者查阅。书中附录E的内容都在本文呈现。

这里主要演示Hadoop1.0版本中的word count例子,用于和Spark中的实现对比。

package org.apache.hadoop.examples;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;
public class WordCount {
    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);
            }
        }
    }

    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(WordCount.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, new Path(args[0]));
        FileOutputFormat.setOutputPath(conf, new Path(args[1]));
        JobClient.runJob(conf);
    }
}

关于Spark内核设计的艺术 架构设计与实现

经过近一年的准备,《Spark内核设计的艺术 架构设计与实现》一书现已出版发行,图书如图:


电子版售卖链接如下:

展开阅读全文

windows xp运行hadoop, word count例子出错,求解决方法

07-03

hadoop集群是部署在虚拟机上的rnWindows帐号和linux帐号相同rn在eclipse中运行WordCount程序报如下异常rn2014-07-03 17:04:47 ERROR org.apache.hadoop.security.UserGroupInformation line:1494 - PriviledgedActionException as:hadoop (auth:SIMPLE) cause:org.apache.hadoop.util.Shell$ExitCodeException: rnException in thread "main" org.apache.hadoop.util.Shell$ExitCodeException: rn at org.apache.hadoop.util.Shell.runCommand(Shell.java:464)rn at org.apache.hadoop.util.Shell.run(Shell.java:379)rn at org.apache.hadoop.util.Shell$ShellCommandExecutor.execute(Shell.java:589)rn at org.apache.hadoop.util.Shell.execCommand(Shell.java:678)rn at org.apache.hadoop.util.Shell.execCommand(Shell.java:661)rn at org.apache.hadoop.fs.RawLocalFileSystem.setPermission(RawLocalFileSystem.java:639)rn at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:435)rn at org.apache.hadoop.fs.FilterFileSystem.mkdirs(FilterFileSystem.java:277)rn at org.apache.hadoop.mapreduce.JobSubmissionFiles.getStagingDir(JobSubmissionFiles.java:125)rn at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:344)rn at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1268)rn at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1265)rn at java.security.AccessController.doPrivileged(Native Method)rn at javax.security.auth.Subject.doAs(Unknown Source)rn at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1491)rn at org.apache.hadoop.mapreduce.Job.submit(Job.java:1265)rn at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:1286)rn at com.run.hadoop.examples.WordCount.main(WordCount.java:81)rnrnrn在网上查了下,说是手动执行D:\soft\hadoop-2.2.0\hadoop-2.2.0\bin\winutils.exe, chmod, 0755, G:\tmp,可以解决,我觉得应该是 winutils.exe 更改不了文件权限,但cmd执行上面命令时,又报错,求哪位大神给个解决方法 论坛

没有更多推荐了,返回首页