1. 程式人生 > >輸出是一個文本文件,每一行第一個數字式行標,第二個數字是輸入文件中每一行除行標外數字的平均值

輸出是一個文本文件,每一行第一個數字式行標,第二個數字是輸入文件中每一行除行標外數字的平均值

程序 values err size 結構 text rgs int manager

有時候你會遇到這樣的問題:你有一個表格,給出了每個人在十二月,一月和二月的收入。

表格如下:

姓名 一月 二月 三月

楚喬 200 314 3500

宇文玥 2000 332 2300

煙熏柿子 6000 333 680

淳兒 5000 333 789

洛河 30 12 2900

現在需要知道每個人這三個月的收入平均值,那麽你就需要將表格中一行代表收入的數字相加除以月數.下面請編寫MR程序解決這個簡單的問題。

輸入只包含一個文件,它的結構如下:(數據自己做格式化)

input:

1    200    314    3500
2    2000    332    2300
3 6000 333 680 4 5000 333 789 5 30 12 2900

其中每行最前面的數字是行標

輸出是一個文本文件,每一行第一個數字式行標,第二個數字是輸入文件中每一行除行標外數字的平均值

如下:

Output:

1    1338
2    1544
3    2337.67
4    2040.67
5    980.67

代碼如下(由於水平有限,不保證完全正確,如果發現錯誤歡迎指正):

package com;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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;

public class MonthTest2 { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Configuration config = new Configuration(); config.set("fs.defaultFS", "hdfs://192.168.0.100:9000"); config.set("yarn.resourcemanager.hostname", "192.168.0.100
"); FileSystem fs = FileSystem.get(config); Job job = Job.getInstance(config); job.setJarByClass(MonthTest2.class); //設置所用到的map類 job.setMapperClass(myMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(Text.class); //設置所用到的reduce類 job.setReducerClass(myReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); //設置輸入輸出地址 FileInputFormat.addInputPath(job, new Path("/yuekao/month.txt")); Path path = new Path("/output1/"); //判斷目錄文件是否存在,存在的話,刪除 if(fs.exists(path)){ fs.delete(path, true); } //指定結果文件的輸出地址 FileOutputFormat.setOutputPath(job,path); //啟動處理任務job boolean completion = job.waitForCompletion(true); if(completion){ System.out.println("Job Success!"); } } public static class myMapper extends Mapper<LongWritable,Text, Text, Text>{ @Override protected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException { String values=value.toString(); String words[]=values.split("\t"); //1 200 314 3500 float avg=(Float.parseFloat(words[1])+Float.parseFloat(words[2])+Float.parseFloat(words[3]))/3; String aaa=String.format("%.2f", avg);//保留兩位小數 context.write(new Text(words[0]), new Text(aaa)); } } public static class myReducer extends Reducer< Text, Text, Text, Text>{ @Override protected void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException { for (Text value : values) { if(value.toString().endsWith("00")){ String[] aa = value.toString().split(".00");//1338.00--->aa[0]=1338 context.write(key,new Text(aa[0])); }else{ context.write(key, value); } } } } }

註意:主要難點在於整數不要求保留兩位小數且不要小數點,float類型的話要求保留兩位小數

希望大家會喜歡~~

輸出是一個文本文件,每一行第一個數字式行標,第二個數字是輸入文件中每一行除行標外數字的平均值