hadoop运行Eclipse项目:WordCount项目

进行到这一步,以成功安装hadoop及eclipse及配置完成

开启hadoop集群

hadoop运行Eclipse项目:WordCount项目_第1张图片
连接成功
hadoop运行Eclipse项目:WordCount项目_第2张图片

创建项目

hadoop运行Eclipse项目:WordCount项目_第3张图片

package com.hadoop.test;

import java.io.IOException;

public class WordCount {

    public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);
            }
        }
    }

    public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();

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

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

在src下,创建文件test,并输入文字,用于测试
hadoop运行Eclipse项目:WordCount项目_第4张图片
hadoop运行Eclipse项目:WordCount项目_第5张图片

然后将该test文件上传到路径中
hadoop运行Eclipse项目:WordCount项目_第6张图片

刷新,出现test文件
hadoop运行Eclipse项目:WordCount项目_第7张图片

注意,一定要创建如下文件夹:程序会自动创建输出结果文件,但不会穿件上层文件夹
hadoop运行Eclipse项目:WordCount项目_第8张图片

配置args参数
hadoop运行Eclipse项目:WordCount项目_第9张图片

hadoop运行Eclipse项目:WordCount项目_第10张图片

右键,run on hadoop
hadoop运行Eclipse项目:WordCount项目_第11张图片

运行完成后,刷新/usr/output/wc,出现以下文件
hadoop运行Eclipse项目:WordCount项目_第12张图片

打开可看到结果
hadoop运行Eclipse项目:WordCount项目_第13张图片

报错解决

如果报一下错误:

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

在src下创建文件:log4j.properties
hadoop运行Eclipse项目:WordCount项目_第14张图片
并输入以下内容

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

保存后运行,

你可能感兴趣的:(大数据)