標籤:string org src dex 分享圖片 需要 lin path getc
配置系統內容變數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 protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException { String line = value.toString(); String[] words = line.split(" "); for(String word : words) { context.write(new Text(word), new IntWritable(1)); } } }
代碼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 protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException { long count = 0; for(IntWritable v : values) { count += v.get(); } context.write(key, new LongWritable(count)); } }
代碼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 = Job.getInstance(conf); job.setMapperClass(WordMapper.class); job.setReducerClass(WordReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(LongWritable.class); FileInputFormat.setInputPaths(job, "c:/bigdata/hadoop/test/test.txt"); FileOutputFormat.setOutputPath(job, new Path("c:/bigdata/hadoop/test/out/")); job.waitForCompletion(true); }}
把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)方式添加,不推薦使用絕對路徑的方式
hadoop第五課:java開發Map/Reduce