問題:apache
輸入文件A的樣例以下(注意文件以tab爲分隔符,粘貼時請檢查):app
20170101 xoop 20170102 yspa 20170103 xdebug 20170104 y日誌 20170105 zcode 20170106 xorm |
輸入文件B的樣例以下:blog
20170101 yhadoop 20170102 y 20170103 x 20170104 z 20170105 y |
根據輸入文件A和B合併獲得的輸出文件C的樣例以下:
20170101 x 20170101 y 20170102 y 20170103 x 20170104 y 20170104 z 20170105 y 20170105 z 20170106 x |
代碼實現:
1 import org.
apache.hadoop.fs.Path; 2 import org.apache.hadoop.io.DoubleWritable; 3 import org.apache.hadoop.io.IntWritable; 4 import org.apache.hadoop.io.LongWritable; 5 import org.apache.hadoop.io.Text; 6 import org.apache.hadoop.mapreduce.Job; 7 import org.apache.hadoop.mapreduce.Mapper; 8 import org.apache.hadoop.mapreduce.Reducer; 9 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 10 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 11 import org.apache.hadoop.util.GenericOptionsParser; 12 13 public class Task1 { 14 public static class MapClass extends Mapper<LongWritable, Text, Text, Text>{ 15 public void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException { 16 context.write(value, new Text("")); 17 } 18 } 19 public static class ReduceClass extends Reducer<Text,Text,Text,Text>{ 20 public void reduce( Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException { 21 context.write(key, new Text("")); 22 } 23 } 24 public static void main(String args[]) throws IOException, ClassNotFoundException, InterruptedException { 25 Configuration conf = new Configuration(); 26 Job job = new Job(conf); 27 job.setJarByClass(Task1.class); 28 job.setMapperClass(MapClass.class); 29 job.setReducerClass(ReduceClass.class); 30 job.setOutputKeyClass(Text.class); 31 job.setOutputValueClass(Text.class); 32 33 FileInputFormat.addInputPath(job, new Path("C:\\Users\\Administrator\\Desktop\\新建文件夾\\input2.txt") ); 34 FileInputFormat.addInputPath(job, new Path("C:\\Users\\Administrator\\Desktop\\\\新建文件夾\\input1.txt") ); 35 FileOutputFormat.setOutputPath(job, new Path("C:\\Users\\Administrator\\Desktop\\新建文件夾\\output")); 36 37 System.exit(job.waitForCompletion(true)?0:1); 38 } 39 }
結果:
踩過的坑:
reduce不執行的緣由:
一、程序出現過異常,能夠經過日誌來debug;
二、參數類型不匹配;
等