這篇文章主要介紹了hadoop中如何實現(xiàn)GenericWritable,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)公司長期為上千客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為龍川企業(yè)提供專業(yè)的做網(wǎng)站、網(wǎng)站建設(shè),龍川網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
package com.test;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.GenericWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
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.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
/**
* 業(yè)務(wù)場景:
* 含有兩個文件,兩個文件中單詞之間的分隔方式不一樣,但是統(tǒng)計出單詞在兩個文件中公共出現(xiàn)的次數(shù)
*
* 文件來源1,逗號分隔text1.txt
* hello,what
* you,haha
* 文件來源2,制表符分隔text2.txt
* girl boy
* father mother
*/
public class WordCountGenericWritable extends Configured implements Tool {
public static class Map1 extends Mapper<LongWritable, Text, Text, MyGenericWritable> {
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer st = new StringTokenizer(line, ",");
while(st.hasMoreElements()) {
context.write(new Text(st.nextElement().toString()), new MyGenericWritable(new LongWritable(1)));
}
}
}
public static class Map2 extends Mapper<Text, Text, Text, MyGenericWritable> {
public void map(Text key, Text value, Context context) throws IOException, InterruptedException {
context.write(key, new MyGenericWritable(new Text("1")));
context.write(value, new MyGenericWritable(new Text("1")));
}
}
public static class Reduce extends Reducer<Text, MyGenericWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<MyGenericWritable> values, Context context) throws IOException, InterruptedException {
int count = 0;
Iterator<MyGenericWritable> it = values.iterator();
while(it.hasNext()) {
MyGenericWritable myGw = it.next();
Writable value = myGw.get();
if(value instanceof LongWritable) {
count = count + Long.valueOf(((LongWritable)value).get()).intValue();
}
if(value instanceof Text) {
count = count + Long.valueOf(((Text)value).toString()).intValue();
}
}
context.write(key, new IntWritable(count));
}
}
public int run(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Configuration conf = this.getConf();
Job job = new Job(conf);
job.setJobName(WordCountGenericWritable.class.getSimpleName());
job.setJarByClass(WordCountGenericWritable.class);
MultipleInputs.addInputPath(job, new Path("hdfs://grid131:9000/text1.txt"), TextInputFormat.class, Map1.class);
MultipleInputs.addInputPath(job, new Path("hdfs://grid131:9000/text2.txt"), KeyValueTextInputFormat.class, Map2.class);
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setReducerClass(Reduce.class);
job.setOutputFormatClass(TextOutputFormat.class);
//當(dāng)map的輸出類型和reduce的輸出類型不一致的時候,需要單獨設(shè)置map輸出類型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(MyGenericWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.waitForCompletion(true);
return job.isSuccessful()?0:1;
}
public static void main(String[] args) throws Exception {
int exit = ToolRunner.run(new WordCount(), args);
System.exit(exit);
}
}
class MyGenericWritable extends GenericWritable {
public MyGenericWritable() {
}
public MyGenericWritable(LongWritable longWritable) {
super.set(longWritable);
}
public MyGenericWritable(Text text) {
super.set(text);
}
@Override
protected Class<? extends Writable>[] getTypes() {
return new Class[]{LongWritable.class, Text.class};
}
}感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“hadoop中如何實現(xiàn)GenericWritable”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
本文題目:hadoop中如何實現(xiàn)GenericWritable
標(biāo)題來源:http://www.chinadenli.net/article48/isjdep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、、品牌網(wǎng)站制作、網(wǎng)站設(shè)計、響應(yīng)式網(wǎng)站、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)