老王大数据教程(二) Hadoop eclipse 开发

来源:互联网 发布:拍摄淘宝图片技巧 编辑:程序博客网 时间:2024/06/08 10:45

Hadoop eclipse 开发

 

 

(一)需要相关文件

 

  1. Eclipse插件hadoop-eclipse-plugin-2.7.3.jar
  2. l Hadoop 安装包 hadoop-2.7.3.tar.gz
  3. l hadoop源码文件hadoop-2.7.3-src
  4. hadoop.llwinutils.exe

 

(1) 添加系统变量HADOOP_HOME

(2) 安装eclipsehadoop-eclipse-plugin-2.7.3.jar放到 eclipse\jee-neon\eclipse\dropins中并重启eclipsewindows->preferences下可看见hadoop Map/Reduce界面,路径选择你WINDOWS下的hadoop(hadoop-2.7.3.tar.gz)解压后的路径

 

 

(3)  选择Windows->show view->others下的MapReduce Locations

(3)新建配置

Map/Reduce Locations 新建


 

host为你的远程hadoop待连接的主机IP地址;接着上一节这里为master ,windows下修改hosts文件192.168.202.5 master ,端口为

vim /usr/local/hadoop/etc/hadoop/core-site.xml中的端口号

       <name>fs.defaultFS</name>

        <value>hdfs://master:9000</value>

user name 填你windows的用户名; 

 

保存配置参数并重启myeclipse,可以看见如下的文件结构说明配置连接成功。

 

 

 

下载hadoop.llwinutils.exewindowshadoop/bin目录下

并将hadoop.dll添加到windows->system32目录下

 

 新建项目:File-->New-->Other-->Map/Reduce Project ,项目名可以随便取

  它会自动添加依赖包,如下:

 

,编写WordCount测试

 

代码如下 

package testjar; 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;import org.apache.hadoop.util.GenericOptionsParser; 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<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();    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();    if (otherArgs.length != 2) {      System.err.println("Usage: wordcount <in> <out>");      System.exit(2);    }//    @SuppressWarnings("deprecation")Job job = new Job(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(otherArgs[0]));    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));    System.exit(job.waitForCompletion(true) ? 0 : 1);  }}


HDFS上创建目录input

hadoop fs -mkdir /wlt/input

拷贝本地README.txtHDFSinput

input里面put txt文件

/usr/local/hadoop/bin/hdfs  dfs -put test.txt  /wlt/input/

 

配置运行环境 Run as ->run configuration

 

 

生成结果 

 

 

 

至此环境搭建成功!!!!!!!!!!

 

参考文献

http://www.cnblogs.com/duking1991/p/6056923.html

http://blog.csdn.net/young_kim1/article/details/50208837

http://www.cnblogs.com/gaopeng527/p/4314215.html

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

参考文献

http://www.cnblogs.com/duking1991/p/6056923.html

0 0
原创粉丝点击