hadoop第五課:java開發Map/Reduce

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.