1, 源代码如下,源代码已经在eclipse里调试通过,源代码的目录~/workspace/MyHelloWorld/src/WordCount.java
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 {
System.out.println("key=" +key.toString());
System.out.println("Value=" + value.toString());
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();
System.out.println("url:" + conf.get("fs.default.name"));
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
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);
}
}
2, 我在~/workspace/MyHelloWorld/bin目录下面执行
[jxwu@master bin]$ pwd
/home/jxwu/workspace/MyHelloWorld/bin
[jxwu@master bin]$ ll
total 12
-rw-rw-r--. 1 jxwu jxwu 2311 Dec 25 14:26 WordCount.class
-rw-rw-r--. 1 jxwu jxwu 2337 Dec 25 14:26 WordCount$IntSumReducer.class
-rw-rw-r--. 1 jxwu jxwu 2543 Dec 25 14:26 WordCount$TokenizerMapper.class
[jxwu@master bin]$ jar cvf test.jar *.class
added manifest
adding: WordCount.class(in = 2311) (out= 1221)(deflated 47%)
adding: WordCount$IntSumReducer.class(in = 2337) (out= 920)(deflated 60%)
adding: WordCount$TokenizerMapper.class(in = 2543) (out= 1061)(deflated 58%)
3, cp test.jar ../../../code/hadoop/hadoop-0.20.2/build
[jxwu@master bin]$ cd ../../../code/hadoop/hadoop-0.20.2/build
[jxwu@master build]$ ll
total 2816
drwxr-xr-x. 3 jxwu jxwu 4096 Nov 28 15:20 ant
drwxr-xr-x. 2 jxwu jxwu 4096 Nov 28 15:20 c++
drwxr-xr-x. 3 jxwu jxwu 4096 Dec 1 16:41 classes
drwxr-xr-x. 12 jxwu jxwu 4096 Nov 28 15:20 contrib
-rw-rw-r--. 1 jxwu jxwu 3525 Dec 22 16:32 Deldup.jar
drwxr-xr-x. 3 jxwu jxwu 4096 Nov 28 15:23 examples
-rw-r--r--. 1 jxwu jxwu 2690076 Nov 28 15:23 hadoop-0.20.2-core.jar
-rw-r--r--. 1 jxwu jxwu 142515 Nov 28 15:23 hadoop-0.20.2-examples.jar
drwxr-xr-x. 5 jxwu jxwu 4096 Nov 28 15:17 ivy
drwxr-xr-x. 3 jxwu jxwu 4096 Nov 28 15:20 src
drwxr-xr-x. 6 jxwu jxwu 4096 Nov 28 15:20 test
-rw-rw-r--. 1 jxwu jxwu 3969 Dec 25 15:06 test.jar
drwxr-xr-x. 3 jxwu jxwu 4096 Nov 28 15:20 tools
drwxr-xr-x. 8 jxwu jxwu 4096 Nov 28 15:20 webapps
[jxwu@master build]$ cd ..
[jxwu@master hadoop-0.20.2]$ bin/hadoop build/test.jar WordCount in out
Error: Could not find or load main class build.test.jar
[jxwu@master hadoop-0.20.2]$ jps
4706 TaskTracker
4234 DataNode
5662 Jps
2457 org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar
4418 SecondaryNameNode
4143 NameNode
4561 JobTracker
这个问题困扰了我好几天了,麻烦大神帮忙看看,程序jar包的分布还有什么地方需要注意么,比如说配置环境变量??谢谢了
3 个解决方案
#1
我自己解决了,我把class文件和jar文件放在了一个路径下面,然后执行就可以了。
我刚入门,可能是范了低级错误。
这个是不是要在什么地方配置,让jar文件可以到某个路径下面去寻找以下class文件呢?
我刚入门,可能是范了低级错误。
这个是不是要在什么地方配置,让jar文件可以到某个路径下面去寻找以下class文件呢?
#2
把jar包引入到eclipse工程下啊
#3
huifu xia
#1
我自己解决了,我把class文件和jar文件放在了一个路径下面,然后执行就可以了。
我刚入门,可能是范了低级错误。
这个是不是要在什么地方配置,让jar文件可以到某个路径下面去寻找以下class文件呢?
我刚入门,可能是范了低级错误。
这个是不是要在什么地方配置,让jar文件可以到某个路径下面去寻找以下class文件呢?
#2
把jar包引入到eclipse工程下啊
#3
huifu xia