hadoop序列化實現案例代碼
更新時間:2024年10月11日 11:00:53 作者:chen18677338530
序列化想必大家都很熟悉了,對象在進行網絡傳輸過程中,需要序列化之后才能傳輸到客戶端,或者客戶端的數據序列化之后送達到服務端,本文將為大家介紹Hadoop如何實現序列化,需要的可以參考一下
Hadoop序列化特點
- 緊湊:高效實用存儲空間
- 快速:讀寫數據額外開銷小
- 可擴展:隨著通信協(xié)議的升級而可以升級
- 互操作:支持多種語言的交互
自定義Bean對象實現序列化
- 必須實現Writable接口
- 反序列化時,需要反射調用無參構造函數
- 如果需要將自定義的bean放在key中傳輸,則還需要實現Comparable接口
案例
package com.chen.phoneproject; import org.apache.hadoop.io.Writable; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; public class FlowBean implements Writable { private long upFlow; private long downFlow; private long sumFlow; public long getUpFlow() { return upFlow; } public void setUpFlow(long upFlow) { this.upFlow = upFlow; } public long getDownFlow() { return downFlow; } public void setDownFlow(long downFlow) { this.downFlow = downFlow; } public long getSumFlow() { return sumFlow; } public void setSumFlow(long sumFlow) { this.sumFlow = sumFlow; } public FlowBean() { super(); } public FlowBean(long upFlow, long downFlow) { super(); this.upFlow = upFlow; this.downFlow = downFlow; } public FlowBean(long upFlow, long downFlow, long sumFlow) { super(); this.upFlow = upFlow; this.downFlow = downFlow; this.sumFlow = sumFlow; } @Override public void write(DataOutput dataOutput) throws IOException { dataOutput.writeLong(upFlow); dataOutput.writeLong(downFlow); dataOutput.writeLong(sumFlow); } @Override public void readFields(DataInput dataInput) throws IOException { this.upFlow = dataInput.readLong(); this.downFlow = dataInput.readLong(); this.sumFlow = dataInput.readLong(); } @Override public String toString() { return "FlowBean{" + "upFlow=" + upFlow + ", downFlow=" + downFlow + ", sumFlow=" + sumFlow + '}'; } }
package com.chen.phoneproject; import lombok.extern.slf4j.Slf4j; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; @Slf4j public class FlowCountMapper extends Mapper<LongWritable, Text,Text,FlowBean> { Text k = new Text(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { log.info("---mapper---"+"key:"+key+",value:"+value); String line = value.toString(); String[] fields = line.split("\t"); String phoneNum = fields[1]; long upFlow = Long.parseLong(fields[3]); long downFlow = Long.parseLong(fields[4]); k.set(phoneNum); FlowBean bean = new FlowBean(upFlow,downFlow); context.write(k,bean); } }
package com.chen.phoneproject; import lombok.extern.slf4j.Slf4j; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; @Slf4j public class FlowCountReducer extends Reducer<Text,FlowBean,Text,FlowBean> { @Override protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException { log.info("---reduce---"+"key:"+key+",value:"+values); long sum_upFlow = 0; long sum_downFlow = 0; for (FlowBean flowBean:values){ sum_upFlow += flowBean.getUpFlow(); sum_downFlow += flowBean.getDownFlow(); } FlowBean result = new FlowBean(sum_upFlow,sum_downFlow,sum_downFlow + sum_upFlow); context.write(key,result); } }
package com.chen.phoneproject; import com.chen.mapreduce.WordcountDriver; import com.chen.mapreduce.WordcountMapper; import com.chen.mapreduce.WordcountReducer; 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.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class FlowsumDriver { public static void main(String[] args) throws Exception { Configuration configuration = new Configuration(); Job job = Job.getInstance(configuration); job.setJarByClass(FlowsumDriver.class); job.setMapperClass(FlowCountMapper.class); job.setReducerClass(FlowCountReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(FlowBean.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(FlowBean.class); FileInputFormat.setInputPaths(job,new Path(args[0])); FileOutputFormat.setOutputPath(job,new Path(args[1])); boolean result = job.waitForCompletion(true); System.exit(result ? 0 : 1); } }
總結
到此這篇關于hadoop序列化實現的文章就介紹到這了,更多相關Hadoop序列化內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot?使用AOP?+?Redis?防止表單重復提交的方法
Spring?Boot是一個用于構建Web應用程序的框架,通過AOP可以實現防止表單重復提交,本文介紹了在Spring?Boot應用程序中使用AOP和Redis來防止表單重復提交的方法,需要的朋友可以參考下2023-04-04通過FeignClient調用微服務提供的分頁對象IPage報錯的解決
這篇文章主要介紹了通過FeignClient調用微服務提供的分頁對象IPage報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03出現SLF4J:?Failed?to?load?class?“org.slf4j.impl.StaticLog
本文主要介紹了出現SLF4J:?Failed?to?load?class?“org.slf4j.impl.StaticLoggerBinder“.的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07SpringBoot+Security 發(fā)送短信驗證碼的實現
這篇文章主要介紹了SpringBoot+Security 發(fā)送短信驗證碼的實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05