MapReduce代码示例

  1. import com.emc.greenplum.gpdb.hadoop.io.GPDBWritable;
  2. import com.emc.greenplum.gpdb.hadoop.mapreduce.lib.input.GPDBInputFormat;
  3. import com.emc.greenplum.gpdb.hadoop.mapreduce.lib.output.GPDBOutputFormat;
  4. import java.io.*;
  5. import java.util.*;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.conf.*;
  8. import org.apache.hadoop.io.*;
  9. import org.apache.hadoop.mapreduce.*;
  10. import org.apache.hadoop.mapreduce.lib.output.*;
  11. import org.apache.hadoop.mapreduce.lib.input.*;
  12. import org.apache.hadoop.util.*;
  13. public class demoMR {
  14. /*
  15. * Helper routine to create our generic record. This section shows the
  16. * format of the data. Modify as necessary.
  17. */
  18. public static GPDBWritable generateGenericRecord() throws
  19. IOException {
  20. int[] colType = new int[3];
  21. colType[0] = GPDBWritable.BIGINT;
  22. colType[1] = GPDBWritable.BOOLEAN;
  23. colType[2] = GPDBWritable.VARCHAR;
  24. /*
  25. * This section passes the values of the data. Modify as necessary.
  26. */
  27. GPDBWritable gw = new GPDBWritable(colType);
  28. gw.setLong (0, (long)12345);
  29. gw.setBoolean(1, true);
  30. gw.setString (2, "abcdef");
  31. return gw;
  32. }
  33. /*
  34. * DEMO Map/Reduce class test1
  35. * -- Regardless of the input, this section dumps the generic record
  36. * into GPDBFormat/
  37. */
  38. public static class Map_test1
  39. extends Mapper<LongWritable, Text, LongWritable, GPDBWritable> {
  40. private LongWritable word = new LongWritable(1);
  41. public void map(LongWritable key, Text value, Context context) throws
  42. IOException {
  43. try {
  44. GPDBWritable gw = generateGenericRecord();
  45. context.write(word, gw);
  46. }
  47. catch (Exception e) {
  48. throw new IOException (e.getMessage());
  49. }
  50. }
  51. }
  52. Configuration conf = new Configuration(true);
  53. Job job = new Job(conf, "test1");
  54. job.setJarByClass(demoMR.class);
  55. job.setInputFormatClass(TextInputFormat.class);
  56. job.setOutputKeyClass (LongWritable.class);
  57. job.setOutputValueClass (GPDBWritable.class);
  58. job.setOutputFormatClass(GPDBOutputFormat.class);
  59. job.setMapperClass(Map_test1.class);
  60. FileInputFormat.setInputPaths (job, new Path("/demo/data/tmp"));
  61. GPDBOutputFormat.setOutputPath(job, new Path("/demo/data/MRTest1"));
  62. job.waitForCompletion(true);
  63. }

上级主题: 一次性HDFS协议安装