本篇內(nèi)容介紹了“hadoop的wordcount java舉例分析”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)公司主要從事做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)井陘礦,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
1.導(dǎo)入hadoop需要用到的包
hadoop-2.4.2/share/hadoop/mapreduce/*.jar
hadoop-2.4.2/share/hadoop/mapreduce/lib/*.jar
hadoop-2.4.2/share/hadoop/common/*.jar
hadoop-2.4.2/share/hadoop/common/lib/*.jar
2.編寫java程序
package demo;
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
public class WordCount {
public static class Map extends MapReduceBase implements Mapper{
private final static IntWritable one=new IntWritable(1);
private Text word=new Text();
@Override
public void map(LongWritable key, Text value,
OutputCollector output, Reporter reporter)
throws IOException {
// TODO Auto-generated method stub
String line=value.toString();
StringTokenizer tokenizer=new StringTokenizer(line);
while (tokenizer.hasMoreTokens()){
word.set(tokenizer.nextToken());
output.collect(word,one);
}
}
}
//舊版本
public static class Reduce extends MapReduceBase implements Reducer{
@Override
public void reduce(Text key, Iterator values,
OutputCollector output, Reporter reporter)
throws IOException {
// TODO Auto-generated method stub
int sum=0;
while(values.hasNext()){
sum+=values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
//System.setProperty("HADOOP_USER_NAME","root");
JobConf conf=new JobConf(WordCount.class);
//conf.set("fs.defaultFS","hdfs://192.168.1.120:9000");
conf.setJobName("wordcount");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(Map.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf,new Path(args[0]));
FileOutputFormat.setOutputPath(conf,new Path(args[1]));
JobClient.runJob(conf);
}
}
3.導(dǎo)出為jar文件
4.上傳到linux系統(tǒng)中。
5.新建input目錄,如果有output目錄,先刪除
6.上傳jar包后,到j(luò)ar包的目錄下,執(zhí)行
hadoop jar WordCount.jar demo.WordCount /input/* /output/
7.如果執(zhí)行時(shí)不帶“/”,會(huì)在hadoop目錄中新建/user/root下新建兩個(gè)文件夾,會(huì)提示
Exception in thread "main" org.apache.hadoop.mapred.InvalidInputException: Input path does not exist: file: /input Exception in thread "main" org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input path does not exist: hdfs://master:9000/user/root/input
只需要在執(zhí)行的時(shí)候帶上“/”就行。
8.獲取分離后的文件
hadoop fs -get /output/* output/
“hadoop的wordcount java舉例分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)頁題目:hadoop的wordcountjava舉例分析
標(biāo)題網(wǎng)址:http://www.chinadenli.net/article6/igpjog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)站制作、電子商務(wù)、網(wǎng)站設(shè)計(jì)、企業(yè)建站、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)