hadoop mapreducez自定義分割槽
package hello_hadoop; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.DoubleWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Partitioner; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class AutoParitionner { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { if(args.length!=2) { System.err.println("Usage: hadoop jar xxx.jar <input path> <output path>"); System.exit(1); } Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "avg of grades"); job.setJarByClass(AutoParitionner.class); job.setMapperClass(PartitionInputClass.class); job.setReducerClass(PartitionOutputClass.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(DoubleWritable.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(DoubleWritable.class); //宣告自定義分割槽的類,下面有類的宣告 job.setPartitionerClass(MyPartitioner.class); job.setNumReduceTasks(2); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true)?0:1); } } class PartitionInputClass extends Mapper<LongWritable, Text, Text, DoubleWritable>{ @Override protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, DoubleWritable>.Context context) throws IOException, InterruptedException { String line = value.toString(); if(line.length()>0){ String[] array = line.split("\t"); if(array.length==2){ String name=array[0]; int grade = Integer.parseInt(array[1]); context.write(new Text(name), new DoubleWritable(grade)); } } } } class PartitionOutputClass extends Reducer<Text, DoubleWritable, Text, DoubleWritable>{ @Override protected void reduce(Text text, Iterable<DoubleWritable> iterable, Reducer<Text, DoubleWritable, Text, DoubleWritable>.Context context) throws IOException, InterruptedException { int sum = 0; int cnt= 0 ; for(DoubleWritable iw : iterable) { sum+=iw.get(); cnt++; } context.write(text, new DoubleWritable(sum/cnt)); } } //自定義分割槽的類 //Partitioner<Text , DoubleWritable > Text,DoubleWirtable分別為map結果的key,value class MyPartitioner extends Partitioner<Text , DoubleWritable >{ @Override public int getPartition(Text text, DoubleWritable value, int numofreuceTask) { String name = text.toString(); if(name.equals("wd")||name.equals("wzf")||name.equals("xzh")||name.equals("zz")) { return 0; }else return 1; } }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31506529/viewspace-2213415/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Kafka - 自定義分割槽器Kafka
- MapReduce之自定義分割槽器Partitioner
- spark:自定義分割槽,自定義排序,spark與jdbc,廣播變數等Spark排序JDBC變數
- hadoop自定義許可權Hadoop
- Flink SQL FileSystem Connector 分割槽提交與自定義小檔案合併策略 SQL
- Hadoop自定義輸出排序方式Hadoop排序
- oracle 線上重新定義,普通表改變分割槽表,分割槽表可以更改型別、分割槽欄位等Oracle型別
- Linux分割槽方案、分割槽建議Linux
- hadoop 自定義格式化輸出Hadoop
- oracle分割槽表和分割槽表exchangeOracle
- PostgreSQL/LightDB 分割槽表之分割槽裁剪SQL
- Linux 分割槽擴容(根分割槽擴容,SWAP 分割槽擴容,掛載新分割槽為目錄)Linux
- Oracle分割槽表基礎運維-07增加分割槽(2 HASH分割槽)Oracle運維
- oracle分割槽表和非分割槽表exchangeOracle
- 非分割槽錶轉換成分割槽表
- [oracle] expdp 匯出分割槽表的分割槽Oracle
- Oracle分割槽表基礎運維-07增加分割槽(1範圍分割槽)Oracle運維
- openGauss 分割槽
- mysql 分割槽MySql
- 分割槽Partition
- lvs 分割槽
- Kafka 分割槽Kafka
- Hive的靜態分割槽與動態分割槽Hive
- Linux 新增LVM分割槽及LVM分割槽擴容LinuxLVM
- MySql資料分割槽操作之新增分割槽操作MySql
- Oracle分割槽表基礎運維-07增加分割槽(3列表分割槽)Oracle運維
- Oracle12c:建立主分割槽、子分割槽,實現自動分割槽插入效果Oracle
- hadoop 自定義GroupComparator實現求最大值Hadoop
- 【Linux】MBR磁碟分割槽表只能有四個分割槽?Linux
- PG的非分割槽表線上轉分割槽表
- 理解MySQL分割槽MySql
- linux分割槽方案Linux
- 建立sawp分割槽
- 範圍分割槽
- 配置FLASH分割槽
- 搞懂MySQL分割槽MySql
- 【MYSQL】 分割槽表MySql
- MySql建立分割槽MySql