前言:本文是在hadoop已经配置好的情况下
WordCount是hadoop下的HelloWorld程序,是初学者必须要会的。下面是用eclipse进行开发
一、工程与MapReduce代码
新建工程,创建WordCount class
下面的代码是旧版mapreduce
package mapreduce
import java.io.IOException
import java.util.Iterator
import java.util.StringTokenizer
import org.apache.hadoop.fs.Path
import org.apache.hadoop.io.IntWritable
import org.apache.hadoop.io.LongWritable
import org.apache.hadoop.io.Text
import org.apache.hadoop.mapred.FileInputFormat
import org.apache.hadoop.mapred.FileOutputFormat
import org.apache.hadoop.mapred.JobClient
import org.apache.hadoop.mapred.JobConf
import org.apache.hadoop.mapred.MapReduceBase
import org.apache.hadoop.mapred.Mapper
import org.apache.hadoop.mapred.OutputCollector
import org.apache.hadoop.mapred.Reducer
import org.apache.hadoop.mapred.Reporter
import org.apache.hadoop.mapred.TextInputFormat
import org.apache.hadoop.mapred.TextOutputFormat
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.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)
}
}
上述代码的运行成功离不开很多jar包,这些jar包主要在安装的hadoop文件夹里面。具体位置参考:
HADOOP_HOME/share/hadoop/
这里的HADOOP_HOME是你安装hadoop的路径。
这个目录下包含下面文件夹:
common
httpfs
hdfs
mapreduce
tool
yarn
其中我们的程序需要从common, mapreduce, yarn文件夹里添加存在的所有jar包以及lib下的所有jar包(这可能有重复,只要相同的覆盖即可)
添加好jar包后,就可以运行程序了。这里我们要为程序配置两个输入。
本地测试时,直接在eclipse点击run configuration ,在arguments下的program argument下添加要统计的文件地址以及输出文件路径。
1.本地模式:测试文件路径 输出文件路径(数据都在本地)
2.HDFS文件:hdfs://localhost/测试文件 hdfs://localhost/输出目录(数据在HDFS中)
配置好了,就可以点击run运行了。
另外:
也可以用命令行
打包:用eclipse将工程打包.
执行:hadoop jar wordcount.jar input output
这里的input最好写成HDFS下的文件路径.(本地的不知道怎么表示,试了下老报错),好像hadoop它会默认加载hdfs的路径.
集群模式:
先将文件复制到HDFS上:hadoop dfs -copyFromLocal 本地文件 hdfs路径
然后命令行运行:hadoop jar wordcout.jar WordCout input output
运行wordcount.jar中的WordCount类,input作为输入,output作为输出.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)