Hadoop-2.2.0 wordcount 编译打包执行 (java.lang.ClassNotFoundException)

来源:互联网 发布:人工智能股票代码 编辑:程序博客网 时间:2024/05/01 19:30


安装好 HADOOP+HBASE 于是开始在403准备试试好不好用然后做实验~万能的 wordcound 我来了~


1、mywordcound.java 书写

主要是从网上抄了来,据说 eclipse 不支持 hadoop-2.2.0的插件,所以不能用来编译和封包只能手写?不详。抄来的代码如下所示:

//must I just coding without the help of eclipse ?import java.io.IOException;import java.util.Iterator;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 MyWordCount{// mapper part *public static class WordCountMapper extends Mapper<Object, Text, Text, IntWritable>{private static final IntWritable one =  new IntWritable(1);private Text word = new Text();//MAP FUNCTION protected void map(Object key, Text value, Context context) throws IOException, InterruptedException{String line = value.toString();StringTokenizer words = new StringTokenizer(line);  //split line into wordswhile(words.hasMoreElements()){word.set(words.nextToken());context.write(word, one);}}}// reducer part *public static class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{private IntWritable totalNum = new IntWritable();protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{int sum = 0;Iterator<IntWritable> it = values.iterator();while(it.hasNext()){sum += it.next().get();}totalNum.set(sum);context.write(key, totalNum);}}// configuration part *@SuppressWarnings("deprecation")public static void main(String[] args) throws Exception{Configuration conf = new Configuration();    //~~~commons-logging-1.1.1.jarJob job = new Job(conf, "MyWordCount");//create a job ~~~ /hadoop/yarn/lib/log4j-1.2.17.jarjob.setJarByClass(MyWordCount.class);         //set mapper.class reducer.class and Job.jarjob.setMapperClass(WordCountMapper.class);job.setReducerClass(WordCountReducer.class);job.setCombinerClass(WordCountReducer.class);job.setOutputKeyClass(Text.class);            //set output key & valuejob.setOutputValueClass(IntWritable.class);FileInputFormat.addInputPath(job, new Path(args[0]));//set input & output pathsFileOutputFormat.setOutputPath(job, new Path(args[1]));System.exit(job.waitForCompletion(true)?0:1);//whether wait job to done or not}}




2、编译

    编译命令如下

javac -classpath ./lib/xxx.jar:./lib/xxx.jar -d ../wordcount_classes *.java -encoding gdb
  -classpath为该java文件需要引用的外部jar包,用“:”符号分隔开;

 -d 是指将编译好的 .class 文件存放到 ./wordcount_classes 文件夹内;

 *.java 是编译的java文件对象

 -encoding gdb 这个可有可无,实际上是没有写的。




3、打jar包

    打包命令如下,也就是这个命令的错误直接导致了后面的 ClassNotFoundException 博大精深的java啊..

jar -cvf ../MyWordCount.jar -C wordcount_classes/ .
     

    这里注意后面的-C和最后的" ."(有一个空格)

    整体编译和打包的情况就如下图所示了:





4、执行

 ClassNotFound的图今天看了一天了,所以截下来留着如下:



实际的运行结果应该是这个样子的:





5、题外话

  尝试过修改 HADOOP_CLASSPATH 虽然没找到估计是被HADOOP-2.2.0给删了吧,但是楞加上也是没有用的;

  比对了使用 jar -cvf xxx.jar ./classes/*  和 jar -cvf xxx.jar -C ./classes . 生成的 jar 包,看里面包含的内容和大小都是一样一样的,很是奇怪;

  本来打算去把 share/hadoop/mapreduce/sources 里面的 hadoop-mapreduce-examples-2.2.0-sources.jar 包里面的文件拿出来的,发现里面竟然都是 .java 文件唉,如下图所示~ 还好小师弟及时跑来了,一个空格一个.解决问题~还有好吃的油桃和李子(xx黑加仑?)谢谢><




0 0
原创粉丝点击