Hadoop大資料之第一個程式--WordCount

來源:互聯網
上載者:User

標籤:

Map類:

 1 package lyc.yushao.hadoop.mr.wc; 2  3 import java.io.IOException; 4  5 import org.apache.hadoop.io.LongWritable; 6 import org.apache.hadoop.io.Text; 7 import org.apache.hadoop.mapreduce.Mapper; 8  9 public class WCMapper extends Mapper<LongWritable, Text, Text, LongWritable> {10 11     @Override12     protected void map(LongWritable key, Text value, Context context)13             throws IOException, InterruptedException {14         // 首先,接收資料15         // accept data16         String line = value.toString();17         // 進行拆分18         // split19         String[] words = line.split(" ");20         // 進行迴圈21         // loop22         for (String w : words) {23             // 發送24             // send25             context.write(new Text(w), new LongWritable(1));26         }27 28     }29 30 }

Reduce類:

 1 package lyc.yushao.hadoop.mr.wc; 2  3 import java.io.IOException; 4  5 import org.apache.hadoop.io.LongWritable; 6 import org.apache.hadoop.io.Text; 7 import org.apache.hadoop.mapreduce.Reducer; 8  9 public class WCReducer extends Reducer<Text, LongWritable, Text, LongWritable> {10 11     @Override12     protected void reduce(Text key, Iterable<LongWritable> values,13             Context context) throws IOException, InterruptedException {14         // 定義一個計數器15         // define a counter16         long counter = 0;17 18         // 接收資料 迴圈19         // accept data and loop20         for (LongWritable i : values) {21             // sum22             counter += i.get();23 24         }25         // send26         context.write(key, new LongWritable(counter));27 28     }29 30 }

 

WordCount類:

 1 package lyc.yushao.hadoop.mr.wc; 2  3 import org.apache.hadoop.conf.Configuration; 4 import org.apache.hadoop.fs.Path; 5 import org.apache.hadoop.io.LongWritable; 6 import org.apache.hadoop.io.Text; 7 import org.apache.hadoop.mapreduce.Job; 8 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 9 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;10 11 public class WordCount {12 13     public static void main(String[] args) throws Exception {14         Configuration conf = new Configuration();15         // 將mapreduce抽象成一個作業16         Job job = Job.getInstance(conf);17 18         // notice!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!19         job.setJarByClass(WordCount.class);20 21         // 將自訂的類組裝起來22 23         // set mapper‘s properties24         job.setMapperClass(WCMapper.class);25 26         job.setMapOutputKeyClass(Text.class);27 28         job.setMapOutputValueClass(LongWritable.class);29 30         // 讀取HDFS資料31         FileInputFormat.setInputPaths(job, new Path("/words.txt"));32 33         // set reducer‘s properties34         job.setReducerClass(WCReducer.class);35         // 輸出到HDFS裡面36         job.setOutputKeyClass(Text.class);37         job.setOutputValueClass(LongWritable.class);38         FileOutputFormat.setOutputPath(job, new Path("/wcout111"));39 40         // 調用job的一些方法來提交41         // submit,but this is not good42         // job.submit();43         job.waitForCompletion(true);44     }45 46 }

 

右鍵:工程名

Export

JAR File

 

 

Finish

命令列中:

hadoop jar /root/mrs.jar 

 

SUCCESS!!

Hadoop大資料之第一個程式--WordCount

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.