阅读403 返回首页    go windows


MapOnly示例__示例程序_MapReduce_大数据计算服务-阿里云

对于MapOnly的作业,Map直接将 < Key, Value > 信息输出到ODPS的表中。用户只需要指定输出表即可,不再需要指定Map输出的Key/Value元信息。

测试准备

(1)准备好测试程序jar包,假设名字为mapreduce-examples.jar;

(2)准备好MapOnly测试表和资源;

  • 创建表

    create table wc_in (key string, value string);
    create table wc_out(key string, cnt bigint);
    
  • 添加资源

    add jar mapreduce-examples.jar -f;
    

(3)使用tunnel导入数据;

   tunnel upload data wc_in;
  • 导入wc_in表的数据文件data内容为:
    hello,odps
    hello,odps

测试步骤

在odpscmd中执行MapOnly

jar -resources mapreduce-examples.jar -classpath mapreduce-examples.jar
    com.aliyun.odps.mapred.open.example.MapOnly wc_in wc_out map

预期结果

作业成功结束。 输出表wc_out中内容为:

+------------+------------+
| key        | cnt        |
+------------+------------+
| hello      | 1          |
| hello      | 1          |
+------------+------------+

代码示例

    package com.aliyun.odps.mapred.open.example;

    import java.io.IOException;

    import com.aliyun.odps.data.Record;
    import com.aliyun.odps.mapred.JobClient;
    import com.aliyun.odps.mapred.MapperBase;
    import com.aliyun.odps.mapred.conf.JobConf;
    import com.aliyun.odps.mapred.utils.SchemaUtils;
    import com.aliyun.odps.mapred.utils.InputUtils;
    import com.aliyun.odps.mapred.utils.OutputUtils;
    import com.aliyun.odps.data.TableInfo;

    public class MapOnly {

      public static class MapperClass extends MapperBase {
        @Override
        public void setup(TaskContext context) throws IOException {
          boolean is = context.getJobConf().getBoolean("option.mapper.setup", false);

          if (is) {
            Record result = context.createOutputRecord();
            result.set(0, "setup");
            result.set(1, 1L);
            context.write(result);
          }
        }

        @Override
        public void map(long key, Record record, TaskContext context) throws IOException {
          boolean is = context.getJobConf().getBoolean("option.mapper.map", false);

          if (is) {
            Record result = context.createOutputRecord();
            result.set(0, record.get(0));
            result.set(1, 1L);
            context.write(result);
          }
        }

        @Override
        public void cleanup(TaskContext context) throws IOException {
          boolean is = context.getJobConf().getBoolean("option.mapper.cleanup", false);

          if (is) {
            Record result = context.createOutputRecord();
            result.set(0, "cleanup");
            result.set(1, 1L);
            context.write(result);
          }
        }
      }

      public static void main(String[] args) throws Exception {
        if (args.length != 2 && args.length != 3) {
          System.err.println("Usage: OnlyMapper <in_table> <out_table> [setup|map|cleanup]");
          System.exit(2);
        }

        JobConf job = new JobConf();
        job.setMapperClass(MapperClass.class);
        job.setNumReduceTasks(0);

        InputUtils.addTable(TableInfo.builder().tableName(args[0]).build(), job);
        OutputUtils.addTable(TableInfo.builder().tableName(args[1]).build(), job);

        if (args.length == 3) {
          String options = new String(args[2]);

          if (options.contains("setup")) {
            job.setBoolean("option.mapper.setup", true);
          }

          if (options.contains("map")) {
            job.setBoolean("option.mapper.map", true);
          }

          if (options.contains("cleanup")) {
            job.setBoolean("option.mapper.cleanup", true);
          }
        }

        JobClient.runJob(job);
      }
    }

最后更新:2016-05-06 10:43:08

  上一篇:go WordCount示例__示例程序_MapReduce_大数据计算服务-阿里云
  下一篇:go 多路输入输出示例__示例程序_MapReduce_大数据计算服务-阿里云