| 1 | |
| 2 | = 以下在run time時有錯 = |
| 3 | |
| 4 | 原因:Type mismatch in key from map: expected org.apache.hadoop.io.LongWritable, recieved org.apache.hadoop.io.Text |
| 5 | |
| 6 | Map的key從LongWritable 強制轉型到 String,似乎會遇到一些錯 |
| 7 | |
| 8 | * keyvalue.java |
| 9 | |
| 10 | {{{ |
| 11 | #!java |
| 12 | package nchc.keyvalue; |
| 13 | |
| 14 | import org.apache.hadoop.fs.Path; |
| 15 | import org.apache.hadoop.mapred.FileInputFormat; |
| 16 | import org.apache.hadoop.mapred.FileOutputFormat; |
| 17 | import org.apache.hadoop.mapred.JobClient; |
| 18 | import org.apache.hadoop.mapred.JobConf; |
| 19 | |
| 20 | public class keyvalue{ |
| 21 | public static void main(String[] args) { |
| 22 | String[] argv = {"input","oh9","1","1"}; |
| 23 | args = argv; |
| 24 | |
| 25 | if (args.length < 4) { |
| 26 | System.out.println("keyvalue <inDir> <outDir> <m> <r>"); |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | JobConf conf = new JobConf(keyvalue.class); |
| 31 | conf.setJobName("keyValue"); |
| 32 | FileInputFormat.setInputPaths(conf, args[0]); |
| 33 | FileOutputFormat.setOutputPath(conf, new Path(args[1])); |
| 34 | conf.setNumMapTasks(Integer.parseInt(args[2])); |
| 35 | conf.setNumReduceTasks(Integer.parseInt(args[3])); |
| 36 | conf.setMapperClass(kvM.class); |
| 37 | conf.setReducerClass(kvR.class); |
| 38 | |
| 39 | long start = System.nanoTime(); |
| 40 | try { |
| 41 | JobClient.runJob(conf); |
| 42 | } catch (Exception e) { |
| 43 | e.printStackTrace(); |
| 44 | } |
| 45 | long period = System.nanoTime() - start; |
| 46 | System.err.println(period*(1e-9) + " secs."); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | }}} |
| 51 | |
| 52 | * kvm.java |
| 53 | |
| 54 | {{{ |
| 55 | package nchc.keyvalue; |
| 56 | |
| 57 | import java.io.IOException; |
| 58 | |
| 59 | import org.apache.hadoop.io.LongWritable; |
| 60 | import org.apache.hadoop.io.Text; |
| 61 | import org.apache.hadoop.mapred.MapReduceBase; |
| 62 | import org.apache.hadoop.mapred.Mapper; |
| 63 | import org.apache.hadoop.mapred.OutputCollector; |
| 64 | import org.apache.hadoop.mapred.Reporter; |
| 65 | |
| 66 | public class kvM extends MapReduceBase implements |
| 67 | Mapper<LongWritable, Text, Text, Text> { |
| 68 | |
| 69 | public void map(LongWritable key, Text value, |
| 70 | OutputCollector<Text, Text> output, Reporter report) |
| 71 | throws IOException { |
| 72 | Text keyv = new Text(key.toString()); |
| 73 | output.collect(keyv, value); |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | }}} |
| 78 | |
| 79 | * kvr.java |
| 80 | |
| 81 | {{{ |
| 82 | package nchc.keyvalue; |
| 83 | |
| 84 | import java.io.IOException; |
| 85 | import java.util.Iterator; |
| 86 | |
| 87 | import org.apache.hadoop.io.Text; |
| 88 | import org.apache.hadoop.mapred.MapReduceBase; |
| 89 | import org.apache.hadoop.mapred.OutputCollector; |
| 90 | import org.apache.hadoop.mapred.Reducer; |
| 91 | import org.apache.hadoop.mapred.Reporter; |
| 92 | |
| 93 | public class kvR extends MapReduceBase implements |
| 94 | Reducer< Text, Text, Text, Text> { |
| 95 | public void reduce(Text key, Iterator<Text> values, |
| 96 | OutputCollector<Text, Text> output, Reporter report) |
| 97 | throws IOException { |
| 98 | while (values.hasNext()) { |
| 99 | Text keyv = new Text("< "+key+" , "); |
| 100 | Text val = new Text(values.next()+">"); |
| 101 | output.collect(keyv, val); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | }}} |