020_自己编写的wordcount程序在hadoop上面运行,不使用插件hadoop-eclipse-plugin-1.2.1.jar

1、Eclipse中无插件运行MP程序 

  1)在Eclipse中编写MapReduce程序
  2)打包成jar包
  3)使用FTP工具,上传jar到hadoop 集群环境
  4)运行

2、具体步骤

说明:该程序运行完被我删除了,具体添加哪些包不太清楚,但是最保险的是把有可能用到的都添加进去,添加情况如下:

1)创建工程、类

2)添加文件夹conf、lib,然后将指定的jar包添加到相对应的文件夹下面。

3)lib下的所有jar选定,右击build Path——>add to build path。效果如图。

4)编写wordcount程序

  1 package org.dragon.hadoop.mr;
  2 
  3 import java.io.IOException;
  4 import java.util.StringTokenizer;
  5 
  6 import org.apache.hadoop.conf.Configuration;
  7 import org.apache.hadoop.fs.Path;
  8 import org.apache.hadoop.io.IntWritable;
  9 import org.apache.hadoop.io.LongWritable;
 10 import org.apache.hadoop.io.Text;
 11 import org.apache.hadoop.mapreduce.Job;
 12 import org.apache.hadoop.mapreduce.Mapper;
 13 import org.apache.hadoop.mapreduce.Reducer;
 14 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
 15 
 16 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
 17 import org.apache.hadoop.util.GenericOptionsParser;
 18 
 19 import com.sun.accessibility.internal.resources.accessibility;
 20 import com.sun.org.apache.xpath.internal.Arg;
 21 
 22 /**
 23  * 
 24  * @author ZhuXY  
 25  * @time   2016-3-7 下午3:37:54
 26  * 
 27  * MapReduce 初级案例 wordcount程序
 28  */
 29 public class MyWorldCount {
 30     
 31     //Mapper 区域
 32     /**
 33      * <KEYIN,         VALUEIN,         KEYOUT,         VALUEOUT>
 34      * 输入key类型    输入value类型    输出key类型        输出value类型
 35      *
 36      */
 37     
 38     /**
 39      * WordCount程序map类
 40      *
 41      */
 42     static  class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
 43         
 44         private Text word=new Text();
 45         private final static IntWritable one=new IntWritable(1);
 46         
 47         //快捷键alt+shift+s
 48         //map方法每次只读取一行数据,换句话说就是每行启动一个map函数
 49         @Override
 50         protected void map(LongWritable key, Text value, Context context)
 51                 throws IOException, InterruptedException {
 52             
 53             //获取每行数据的值
 54             String lineValue=value.toString();
 55             
 56             //进行分割
 57             StringTokenizer stringTokenizer=new StringTokenizer(lineValue);
 58             
 59             //遍历
 60             while (stringTokenizer.hasMoreElements()) {
 61                 
 62                 //获取每个值
 63                 String worldValue=stringTokenizer.nextToken();
 64                 
 65                 //设置map, 输入的key值
 66                 word.set(worldValue);
 67                 context.write(word, one); //如果出现就出现一次,存在每行出现几次,这时候键的值一样,多个键值对
 68             }
 69         }
 70     }
 71     
 72     //Reducer 区域
 73     /**
 74      * <KEYIN, VALUEIN, KEYOUT, VALUEOUT>
 75      * KEYIN key, Iterable<VALUEIN> values, Context context
 76      */
 77     
 78     /**
 79      * WordCount程序reduce类
 80      *
 81      */
 82     static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
 83 
 84         private IntWritable resultIntWritable=new IntWritable();
 85         //这里key还是key。但是value变成了values
 86         @Override
 87         protected void reduce(Text key, Iterable<IntWritable> values,
 88                 Context context)
 89                 throws IOException, InterruptedException {
 90             //用于累加的变量
 91             
 92             int sum=0;
 93             //循环遍历Interable
 94             for(IntWritable value:values)
 95             {
 96                 //累加
 97                 sum+=value.get();
 98             }
 99             
100             //设置总次数
101             resultIntWritable.set(sum);
102             context.write(key, resultIntWritable);
103         }
104     }
105     
106     //Client  区域
107     public static void main(String[] args) throws Exception {
108         
109         //方便起见直接在此处写死了,可以在run as的配置中配置参数
110         args=new String[]{
111                 "hdfs://hadoop-master.dragon.org:9000/wc/wcinput/",
112                 "hdfs://hadoop-master.dragon.org:9000/wc/wcoutput/"            
113         };
114         
115         //获取配置文件信息
116         Configuration configuration=new Configuration();
117         
118         //当命令格式不正确的时候,提示,类似于shell中的命令提示
119 //        String[] otherArgs = new GenericOptionsParser(configuration, 
120 //                args).getRemainingArgs();
121 //                if (otherArgs.length != 2) {
122 //                System.err.println("Usage: wordcount <in> <out>");
123 //                System.exit(2);
124 //                }
125         
126         //创建job。设置配置文件信息和Job名称
127         Job job=new Job(configuration,"wc");
128         
129         //1、设置Job运行的类
130         job.setJarByClass(MyWorldCount.class);
131         
132         //2、设置Mapper类和Reducer类
133         job.setMapperClass(MyMapper.class);
134         job.setReducerClass(MyReducer.class);
135         
136         //3、设置输入文件的目录和输出文件目录
137         FileInputFormat.addInputPath(job, new Path(args[0]));
138         FileOutputFormat.setOutputPath(job, new Path(args[1]));
139         
140         //4、设置输出结果的key和value的类型
141         job.setOutputKeyClass(Text.class);
142         job.setOutputValueClass(IntWritable.class);
143         
144         //5、提交Job等待运行结果,并在客户端显示运行信息
145         boolean isSuccess=job.waitForCompletion(true);
146         
147         //6、结束程序
148         System.exit(isSuccess?0:1);
149     }
150 }
View Code

  5)打包,打成jar包。项目右击Export-->jar file,如图:

6)将jar包(此处为wc.jar)通过FileZilla客户端上传到CentOS中。

7)运行命令:$hadoop jar wc.jar <input path> <output path>.

运行前提:jps查看,确保dfs和MapReduce服务都已经开启。

3、运行时出的WARNING提示

 增加代码,就是上述代码中注释掉的代码:

        
        //当命令格式不正确的时候,提示,类似于shell中的命令提示
        String[] otherArgs = new GenericOptionsParser(configuration, 
                args).getRemainingArgs();
                if (otherArgs.length != 2) {
                System.err.println("Usage: wordcount <in> <out>");
                System.exit(2);
                }
        

 

posted @ 2016-03-15 11:02  YouxiBug  阅读(305)  评论(0编辑  收藏  举报