马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动
马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作
马士兵hadoop第四课:Yarn和Map/Reduce配置启动和原理讲解
配置系统环境变量HADOOP_HOME,指向hadoop安装目录(如果你不想招惹不必要的麻烦,不要在目录中包含空格或者中文字符)
把HADOOP_HOME/bin加到PATH环境变量(非必要,只是为了方便)
如果是在windows下开发,需要添加windows的库文件
把盘*享的bin目录覆盖HADOOP_HOME/bin
如果还是不行,把其中的hadoop.dll复制到c:\windows\system32目录下,可能需要重启机器
建立新项目,引入hadoop需要的jar文件
代码WordMapper:
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper; public class WordMapper extends Mapper<LongWritable,Text, Text, IntWritable> {@Override</br>
</span><span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable><span style="color: #000000">.Context context)</br>
</span><span style="color: #0000ff">throws</span><span style="color: #000000"> IOException, InterruptedException {</br>
String line </span>=<span style="color: #000000"> value.toString();</br>
String[] words </span>= line.split(" "<span style="color: #000000">);</br>
</span><span style="color: #0000ff">for</span><span style="color: #000000">(String word : words) {</br>
context.write(</span><span style="color: #0000ff">new</span> Text(word), <span style="color: #0000ff">new</span> IntWritable(1<span style="color: #000000">));
}</br>
}</br>}
代码WordReducer:
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; public class WordReducer extends Reducer<Text, IntWritable, Text, LongWritable> {@Override</br>
</span><span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> reduce(Text key, Iterable<IntWritable><span style="color: #000000"> values,</br>
Reducer</span><Text, IntWritable, Text, LongWritable>.Context context) <span style="color: #0000ff">throws</span><span style="color: #000000"> IOException, InterruptedException {</br>
</span><span style="color: #0000ff">long</span> count = 0<span style="color: #000000">;</br>
</span><span style="color: #0000ff">for</span><span style="color: #000000">(IntWritable v : values) {</br>
count </span>+=<span style="color: #000000"> v.get();</br>
}</br>
context.write(key, </span><span style="color: #0000ff">new</span><span style="color: #000000"> LongWritable(count));</br>
}</br>}
代码Test:
import org.apache.hadoop.conf.Configuration;
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.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class Test {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();Job job </span>=<span style="color: #000000"> Job.getInstance(conf);</br></br> job.setMapperClass(WordMapper.</span><span style="color: #0000ff">class</span><span style="color: #000000">);</br>
job.setReducerClass(WordReducer.</span><span style="color: #0000ff">class</span><span style="color: #000000">);</br>
job.setMapOutputKeyClass(Text.</span><span style="color: #0000ff">class</span><span style="color: #000000">);</br>
job.setMapOutputValueClass(IntWritable.</span><span style="color: #0000ff">class</span><span style="color: #000000">);</br>
job.setOutputKeyClass(Text.</span><span style="color: #0000ff">class</span><span style="color: #000000">);</br>
job.setOutputValueClass(LongWritable.</span><span style="color: #0000ff">class</span><span style="color: #000000">);</br></br> FileInputFormat.setInputPaths(job, </span>"c:/bigdata/hadoop/test/test.txt"<span style="color: #000000">);</br>
FileOutputFormat.setOutputPath(job, </span><span style="color: #0000ff">new</span> Path("c:/bigdata/hadoop/test/out/"<span style="color: #000000">));</br></br> job.waitForCompletion(</span><span style="color: #0000ff">true</span><span style="color: #000000">);</br>
}</br>}
把hdfs中的文件拉到本地来运行
FileInputFormat.setInputPaths(job, "hdfs://master:9000/wcinput/");
FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/wcoutput2/"));
注意这里是把hdfs文件拉到本地来运行,如果观察输出的话会观察到jobID带有local字样
同时这样的运行方式是不需要yarn的(自己停掉yarn服务做实验)
在远程服务器执行
conf.set("fs.defaultFS", "hdfs://master:9000/"); conf.set("mapreduce.job.jar", "target/wc.jar");
conf.set("mapreduce.framework.name", "yarn");
conf.set("yarn.resourcemanager.hostname", "master");
conf.set("mapreduce.app-submission.cross-platform", "true"); FileInputFormat.setInputPaths(job, "/wcinput/");
FileOutputFormat.setOutputPath(job, new Path("/wcoutput3/"));
如果遇到权限问题,配置执行时的虚拟机参数-DHADOOP_USER_NAME=root
也可以将hadoop的四个配置文件拿下来放到src根目录下,就不需要进行手工配置了,默认到classpath目录寻找
或者将配置文件放到别的地方,使用conf.addResource(.class.getClassLoader.getResourceAsStream)方式添加,不推荐使用绝对路径的方式
原文地址:http://www.cnblogs.com/yucongblog/p/6650872.html