• Hadoop集群搭建记录 | 云计算[CentOS7] | 伪分布式集群 Master运行WordCount


    写在前面

    本系列文章索引以及一些默认好的条件在 传送门

    step1 eclipse访问hadoop

    首先需要明确eclipse安装目录,然后将hadoop-eclipse-plugin_版本号.jar插件放在安装目录的dropins下

    在这里插入图片描述
    关于插件,可以通过博主上传到csdn的免费资源获取,链接
    具体版本可以自己选择:
    在这里插入图片描述

    step2 重启并配置eclipse

    在eclipse界面中依次选择:Window→show view→other→MapReduce Tools/Map/Reduce Locations
    在这里插入图片描述
    然后在界面上会显示一个大象符号,如下图:
    在这里插入图片描述
    点击大象符号,然后就会配置一个DFS Location
    配置参数为:
    在这里插入图片描述
    配置好后点击finish
    然后会出现:
    在这里插入图片描述

    step3 新建Map/Reduce项目

    在Eclipse中依次选择File→New→Other…→Map/Reduce Project→Next,
    输入项目名TestWordCount,浏览并选择Hadoop路径/usr/local/hadoop,单击“Finish”按钮,则新建项目TestWordCount成功
    新建包并在包下创建相应的类,具体项目结构如下:
    在这里插入图片描述

    step4 复制粘贴代码

    package wordCount;
    
    import java.io.IOException;
    import java.util.StringTokenizer;
    import org.apache.hadoop.conf.Configuration;
    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.Mapper;
    import org.apache.hadoop.mapreduce.Reducer;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    public class WordCount {
    	public static class WordCountMapper extends Mapper<Object, Text, Text, IntWritable> {
    		/**
    		 * 定义final 静态变量one,用来标示键值对<String,One>
    		 * 然后定义Text类型的变量来存储字符串
    		 */
    		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 WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    		private IntWritable result = new IntWritable();
    		public void reduce(Text key, Iterable<IntWritable> 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();
    		if (args.length != 2) {
    			System.err.println("Usage: <in> <out>");
    			System.exit(2); 
    		}
    		Job job = Job.getInstance(conf, "word count");
    		job.setJarByClass(WordCount.class);
    		job.setMapperClass(WordCountMapper.class);
    		job.setCombinerClass(WordCountReducer.class);
    		job.setReducerClass(WordCountReducer.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);
    	}
    }
     
    

    step5 配置运行参数

    启动hadoop集群后在终端写入:

    hadoop fs -mkdir /tmp
    hadoop fs -chmod -R 777 /tmp
    

    Run→Run Configurations选项,以配置输入输出参数
    大致为:
    在这里插入图片描述
    点击run直接跑
    然后在左侧的目录下会显示文件夹:
    在这里插入图片描述
    点进去看下part-r-00000文件就会发现,该文件存储结果:
    在这里插入图片描述
    其中输入为:
    在这里插入图片描述
    结果与预期一致,项目成功~

    整个hadoop就到此结束啦

  • 相关阅读:
    centos ftp搭建
    python_模块
    python_如何建立包
    python_类
    python_递归
    python_函数
    python_字典
    python_条件、循环语句
    python_中文乱码问题
    Viola–Jones object detection framework--Rapid Object Detection using a Boosted Cascade of Simple Features中文翻译 及 matlab实现(见文末链接)
  • 原文地址:https://www.cnblogs.com/PushyTao/p/16196706.html
Copyright © 2020-2023  润新知