閱讀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_大數據計算服務-阿裏雲