首先检查hadoop是否安装并配置正确
然后建立WordCount.java文件
里面保存
package org.myorg;

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;

public class WordCount {

public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();

public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(word, one);
}
}
}

public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {

public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}

public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("wordcount");

conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);

conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);

conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);

FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));

JobClient.runJob(conf);
}

}

然后编译WordCount.java文件,把它制作成可执行jar包
javac -d . -classpath /root/hadoop-0.20.1/hadoop-0.20.1-core.jar WordCount.java
然后在org的同级目录上建立manifest.mf
在里面写上Main-Class: org.myorg.WordCount
然后保存并执行jar -cvfm count.jar manifest.mf org/
然后在hdfs上建立一个文件夹,hadoop fs -mkdir /test
hadoop fs -put /root/wordtestnum.txt /test
然后执行hadoop jar /root/Desktop/count.jar /test/in /test/out
查看运行结果hadoop fs -cat /test/out/part-00000

 

运城互联网论坛地址:http://www.dmyc8.com/forum-104-1.html

相关文章:

  • 2022-01-05
  • 2021-07-05
  • 2022-01-17
  • 2021-10-08
  • 2021-05-21
  • 2022-12-23
  • 2022-01-22
  • 2021-12-03
猜你喜欢
  • 2022-12-23
  • 2022-02-22
  • 2021-11-14
  • 2021-10-15
  • 2021-12-29
  • 2022-12-23
  • 2022-01-15
相关资源
相似解决方案