這篇文章主要介紹hadoop 2.2.0如何編譯運(yùn)行wordcount,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

我們提供的服務(wù)有:成都網(wǎng)站制作、網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、沁水ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的沁水網(wǎng)站制作公司
1、首先介紹下hadoop的版本問題,當(dāng)前Hadoop版本比較混亂,讓很多用戶不知所措。實(shí)際上,當(dāng)前Hadoop只有兩個(gè)版本:Hadoop 1.0和Hadoop 2.0,其中,Hadoop 1.0由一個(gè)分布式文件系統(tǒng)HDFS和一個(gè)離線計(jì)算框架MapReduce組成,而Hadoop 2.0則包含一個(gè)支持NameNode橫向擴(kuò)展的HDFS,一個(gè)資源管理系統(tǒng)YARN和一個(gè)運(yùn)行在YARN上的離線計(jì)算框架MapReduce。相比于Hadoop 1.0,Hadoop 2.0功能更加強(qiáng)大,且具有更好的擴(kuò)展性、性能,并支持多種計(jì)算框架。由于hadoop 2.0不用于hadoop 1.0的API,所以,從hadoop 1.0升級(jí)到hadoop 2.0需要重寫mapreduce程序,關(guān)于從Hadoop 1.0升級(jí)到2.0(1)參考鏈接: http://dongxicheng.org/mapreduce-nextgen/hadoop-upgrade-to-version-2/ hadoop 2.2.0新功能介紹 參考鏈接http://docs.aws.amazon.com/zh_cn/ElasticMapReduce/latest/DeveloperGuide/emr-hadoop-2.2.0-features.html
2、然后就是準(zhǔn)備程序WordCount.java在/root/test/下:
import java.io.IOException;  
import java.util.StringTokenizer;  
  
import org.apache.hadoop.conf.Configuration;  
import org.apache.hadoop.fs.Path;  
import org.apache.hadoop.io.IntWritable;  
import org.apache.hadoop.io.Text;  
import org.apache.hadoop.mapreduce.Job;  
import org.apache.hadoop.mapreduce.Mapper;  
import org.apache.hadoop.mapreduce.Reducer;  
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
import org.apache.hadoop.util.GenericOptionsParser;  
  
public class WordCount {  
  
  public static class TokenizerMapper   
       extends Mapper<Object, Text, Text, IntWritable>{  
      
    private final static IntWritable one = new IntWritable(1);  
    private Text word = new Text();  
    // value已經(jīng)是文件內(nèi)容的一行  
    public void map(Object key, Text value, Context context  
                    ) throws IOException, InterruptedException {  
      StringTokenizer itr = new StringTokenizer(value.toString());  
      while (itr.hasMoreTokens()) {  
        word.set(itr.nextToken());  
        context.write(word, one);  
      }  
    }  
  }  
    
  public static class IntSumReducer   
       extends Reducer<Text,IntWritable,Text,IntWritable> {  
    private IntWritable result = new IntWritable();  
  
    public void reduce(Text key, Iterable<IntWritable> values,   
                       Context context  
                       ) throws IOException, InterruptedException {  
      int sum = 0;  
      for (IntWritable val : values) {  
        sum += val.get();  
      }  
      result.set(sum);  
      context.write(key, result);  
    }  
  }  
  
  public static void main(String[] args) throws Exception {  
    Configuration conf = new Configuration();  
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();  
    if (otherArgs.length != 2) {  
      System.err.println("Usage: wordcount <in> <out>");  
      System.exit(2);  
    }  
    Job job = new Job(conf, "word count");  
    job.setJarByClass(WordCount.class);  
    job.setMapperClass(TokenizerMapper.class);  
    job.setCombinerClass(IntSumReducer.class);  
    job.setReducerClass(IntSumReducer.class);  
    job.setOutputKeyClass(Text.class);  
    job.setOutputValueClass(IntWritable.class);  
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
    System.exit(job.waitForCompletion(true) ? 0 : 1);  
  }  
}3、新建bin文件夾在/root/test/下,將WordCount編譯成class文件,命令如下:
root@ubuntupc:/home/ubuntu/software/cdh6-hadoop/share/hadoop# javac -classpath common/hadoop-common-2.2.0-cdh6.0.0-beta-2.jar:common/lib/commons-cli-1.2.jar:common/lib/hadoop-annotations-2.2.0-cdh6.0.0-beta-2.jar:mapreduce/hadoop-mapreduce-client-core-2.2.0-cdh6.0.0-beta-2.jar -d /root/test/bin/ /root/test/WordCount.java
4、將class文件打包成jar包,命令如下:
root@ubuntupc:~/test# jar -cvf WordCount.jar com/du/simple/*.class
5、運(yùn)行jar文件
root@ubuntupc:~/test# hadoop jar WordCount.jar com/du/simple/WordCount /user/root/input /user/root/output
6、查看運(yùn)行結(jié)果
root@ubuntupc:~/hadoop/WordCount# hadoop fs -cat output/part-r-00000
好的,到此打完收功!
以上是“hadoop 2.2.0如何編譯運(yùn)行wordcount”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
                新聞名稱:hadoop2.2.0如何編譯運(yùn)行wordcount
                
                URL分享:http://www.chinadenli.net/article26/iigdcg.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、微信小程序、品牌網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站排名、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
