搭建Hadoop的MapReduce代码环境- Maven-eclipse

程序链接

链接:https://pan.baidu.com/s/1DPwPQLAO5cXdMoNcEgEttQ
提取码:crz1

搭建步骤

  1. 安装jdk
  2. 配置maven
  3. 安装eclipse
  4. 给eclipse配置maven
  5. 创建maven项目,导入pom.xml文件
  6. 编写WordCount,然后运行代码

详细搭建步骤

  1. 安装jdk
    1. 安装java
    2. 配置java环境
  2. 配置maven
    1. 解压maven文件
    2. 配置conf/setting.xml 中的本地仓库 和镜像
  3. 安装eclipse
  4. 给eclipse配置maven
    1. Windows -> Preferences
    2. 选择Maven -> Installations 然后点击Add。 然后点击Directory选择maven所在的目录
    3. 选择Maven -> User settings, 然后点击Browser,选择setting.xml所在的目录
  5. 创建maven项目,导入pom.xml文件
    1. File ->New -> Maven Project
    2. 点击Next,选择quick-start ,点击Next,输入Group Id和 Artifact Id ,然后点击Finish
    3. 修改pom.xml文件,添加
  6. 编写WordCount,然后运行代码

详细搭建步骤

  1. 安装jdk

    1. 安装Java1.8
    2. 详细安装步骤在这里,这里就不多做赘述了:https://www.cnblogs.com/maoning/p/10701349.html
  2. 配置maven

    • 下载maven,并解压
    • image
    • 修改conf/setting.xml文件,用记事本打开即可
    • image

    image

    <mirror>
            <id>alimaven</id>
            <mirrorOf>central</mirrorOf>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
        </mirror>
    
    • 保存后即可
    • 新建本地仓库,新建一个文件夹,名字随便起,但是要记住。 比如新建一个文件夹:D:\Hadoop\resp
    • 像上述步骤一样, 编辑setting.xml
      image
  3. 安装eclipse
    image

    • 免安装版本,解压之后,直接运行eclipse.exe就行
  4. 给eclipse配置maven

    1. Windows -> Preferences
      image
    2. 选择Maven -> Installations 然后点击Add。 然后点击Directory选择maven所在的目录
      image
      完成之后点击Finishimage
      点击Apply and Closeimage
    3. 选择Maven -> User settings, 然后点击Browser,选择setting.xml所在的目录
      image
  5. 创建maven项目,导入pom.xml文件

    1. File ->New -> Maven Project
      image
      image
    2. 点击Next
      image
      选择quick-start ,点击Next,image
      输入Group Id和 Artifact Id ,然后点击Finishimage
    3. 修改pom.xml文件,将内容复制进去 ,pom.xml的具体内容见附录
      image
    • 保存之后,然后耐心等待jar包加载完毕
    • 在eclipse的右下角有加载的进度条
  6. 编写程序,然后运行代码
    image

    • 具体代码见附录
    • 运行代码
    • image
    • 查看运行结果,是否创建成功
      image

搭建Window版的Hadoop,以及Eclipse插件

安装Windows环境的Hadoop

  1. 解压Hadoop的压缩文件
  2. 将Hadoop添加到环境变量中,分别添加HADOOP_HOME和 HADOOP_USER_NAME
    image
    image
  3. 打开hadoop\etc\hadoop目录下的hadoop-env.cmd文件,将其中的%JAVA_HOME%改为Java JDK的安装路径,然后保存。
    image

安装eclipse插件

  1. 解压插件hadoop-eclipse-plugin.zip
  2. 将插件中的bin目录下的所有文件放入Hadoop2.7.3的bin目录下
    image
  3. 在bin里面.dll复制到C:\Windows\system32中
    image
  4. 把jar包复制到eclipse安装目录的plugs文件夹里面
    image
  5. 把platform.xml删了,可到Eclipse根目录下按CTRL+F进行查找该文件,
    找到后删除,Eclipse就会重新读取Plugins目录下的插件并后进行安装。
    image
  6. Window -> show view -> other -> MapReduce Tools
    image
    image
  7. 鼠标右键, 新建一个Hadoop的location
    image
    image
  8. 显示DFS图标
    image
    image
  9. 运行MapReduce程序,右键 -> Run As -> Run on Hadoop
    image
附录

pom.xml配置

<dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>3.8.1</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.apache.hadoop</groupId>
           <artifactId>hadoop-common</artifactId>
           <version>2.6.0</version>
       </dependency>
       <dependency>
           <groupId>org.apache.hadoop</groupId>
           <artifactId>hadoop-client</artifactId>
           <version>2.6.0</version>
       </dependency>
       <dependency>
           <groupId>org.apache.hadoop</groupId>
           <artifactId>hadoop-hdfs</artifactId>
           <version>2.6.0</version>
       </dependency>
       <dependency>
           <groupId>jdk.tools</groupId>
           <artifactId>jdk.tools</artifactId>
           <version>1.8</version>
           <scope>system</scope>
           <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
       </dependency>
        <dependency>
   <groupId>commons-lang</groupId>
   <artifactId>commons-lang</artifactId>
   <version>2.6</version>
   </dependency>

java测试代码

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class Test {
	public static void main(String args[] ) throws URISyntaxException, IOException{
		// 设置uri的地址 这是你虚拟机的ip地址
	    URI uri = new URI("hdfs://192.168.43.100:9000");
	    // 创建配置信息
	    Configuration conf = new Configuration();
	    //获取hdfs的对象
	    FileSystem fs = FileSystem.get(uri,conf);
	    //创建一个目录
	    fs.mkdirs(new Path("/hf002"));
	    fs.mkdirs(new Path("/hf002/hello"));
	    //查看目录信息
	    FileStatus[] listStatus = fs.listStatus(new Path("/hf002"));
	    
	    for(FileStatus fileStatus: listStatus){
	        System.out.println(fileStatus.getPath());
	    }
	    System.out.println("创建成功");
	    //查看里面的数据
	    
//	    FSDataInputStream in = fs.open(new Path("/hf002/hello.txt"));
//	    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in,"utf-8"));
//	    String line = null;
//	    while((line= bufferedReader.readLine())!=null){
//	        System.out.println(line);
//	    }
	}
	
    
    
}

mapreduce代码

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**   
 * @Title: WordCount.java  
 * @Description:  
 *   wordCount:单词统计,是最常用的mapreduce代码,通常用于词频分析、词云制作。
 * 
 **/ 
public class WordCount2 {

		// mapper一共有四个参数,是两对key-value,前两个是从文件传输到map端时候的数据类型,后两个是map输出时候的数据类型
		/**
		 * 
		 * @Title: wordcount1.java  
		 * @Description:  
		 * 
		 * 111  222  333
		 * 123  234  456
		 * aa    bb   c
		 * 
		 * <111,1>  <222,1>  <333,1> <111,1>
		 * 
		 * @version 1.0
		 */
		public static class wordcount1Mapper extends Mapper<LongWritable, Text, Text, IntWritable> {

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

			@Override
			protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
					throws IOException, InterruptedException {

				// \\s+:一个或者多个空格的统称
				//String[] words = value.toString().split("\\s+");
				System.out.println("hello");
				String[] words = value.toString().split("\\s+");
				System.out.println(words);
				for (String word : words) {
					outputKey.set(word);
					context.write(outputKey, one);
				}
			}
		}

		public static class wordcount1Reducer extends Reducer<Text, IntWritable, Text, IntWritable> {

			private IntWritable result = new IntWritable();

			// <aa,1>  <bb,1>  <cc,1> <aa,1>  <bb,1> <aa,1>
			// <aa,1 1 1> <bb,1 1> <cc,1>
			@Override
			protected void reduce(Text key, Iterable<IntWritable> values,
					Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {

				int sum = 0;
				for (IntWritable value : values) {
					sum+=value.get();
				}

				result.set(sum);
				
				context.write(key, result);
			}
		}

		public static void main(String[] args) {

			try {
				Configuration conf = new Configuration();
				conf.set("fs.defaultFS", "hdfs://192.168.43.100:9000");
				
				Job job;
				job=Job.getInstance(conf,"wordcount1");
				job.setJarByClass(WordCount2.class);
				
				// 配置此job的专属mapper和reducer
				job.setMapperClass(wordcount1Mapper.class);
				job.setReducerClass(wordcount1Reducer.class);
				// 配置输出的key和value的数据类型
				job.setOutputKeyClass(Text.class);
				job.setMapOutputValueClass(IntWritable.class);
				// 设置map端读取数据文件的方式
				job.setInputFormatClass(TextInputFormat.class);
				
				FileInputFormat.addInputPath(job, new Path("/hf002/cat.txt"));
				Path outputPath = new Path("/wordcount");
				
				FileSystem.get(conf).delete(outputPath,true);
				FileOutputFormat.setOutputPath(job, outputPath);
				// 三目运算符
				System.exit(job.waitForCompletion(true)?0:1);
				
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
}

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值