IDEA 使用虚拟机中 hdfs 运行 wordcount

准备

  1. 虚拟机 centos6.5 中分布式搭建,Hadoop 2.6.4
  2. Win10
  3. IDEA 2019
  4. JDK 1.8
  5. IDEA 对虚拟机中增删改查操作正常

配置

  1. HADOOP_HOME 系统环境变量添加 hadoop 文件夹的位置(文件夹的 bin 目录下需包含 hadoop.dll 和 winutils.exe)

    获取地址 https://github.com/4ttty/winutils

  2. 上面两文件在 C:\Windows\System32 目录下也放一份

编写

import org.apache.hadoop.conf.Configuration;
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.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class WordCountDemo {

    public static class WordcountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

        /**
         * map阶段的业务逻辑就写在自定义的map()方法中
         * maptask会对每一行输入数据调用一次我们自定义的map()方法
         */
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            //将maptask传给我们的文本内容先转换成String
            String line = value.toString();
            //根据空格将这一行切分成单词
            String[] words = line.split(" ");

            //将单词输出为<单词,1>
            for(String word:words){
                //将单词作为key,将次数1作为value,以便于后续的数据分发,可以根据单词分发,以便于相同单词会到相同的reduce task
                context.write(new Text(word), new IntWritable(1));
            }
        }
    }

    public static class WordcountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        /**
         * <angelababy,1><angelababy,1><angelababy,1><angelababy,1><angelababy,1>
         * <hello,1><hello,1><hello,1><hello,1><hello,1><hello,1>
         * <banana,1><banana,1><banana,1><banana,1><banana,1><banana,1>
         * 入参key,是一组相同单词kv对的key
         */
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int count=0;

            for(IntWritable value:values){

                count += value.get();
            }

            context.write(key, new IntWritable(count));

        }

    }

    public static void main(String[] args) throws Exception {

        if (args == null || args.length == 0) {
            args = new String[2];
            args[0] = "hdfs://node01:9000/data/input/LICENSE.txt";
            args[1] = "hdfs://node01:9000/data/output";
        }

        Configuration conf = new Configuration();

        Job job = Job.getInstance(conf);

        //指定本程序的jar包所在的本地路径
        job.setJarByClass(WordCountDemo.class);

        //指定本业务job要使用的mapper/Reducer业务类
        job.setMapperClass(WordcountMapper.class);
        job.setReducerClass(WordcountReducer.class);

        //指定mapper输出数据的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //指定最终输出的数据的kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //指定job的输入原始文件所在目录
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        //指定job的输出结果所在目录
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //将job中配置的相关参数,以及job所用的java类所在的jar包,提交给yarn去运行
        /*job.submit();*/

        boolean res = job.waitForCompletion(true);
        System.exit(res?0:1);

    }

}

启动 VM 参数添加 -DHADOOP_USER_NAME=hadoop,hadoop 为虚拟机中对 hdfs 有读写权限的用户名

运行结果

Idea 控制台

HDFS 查看

附

resources 下的 log4j.properties

# Global logging configuration 开发时候建议使用 debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容