文档

分区表输入示例

更新时间:

本文为您介绍MapReduce的分区表输入示例。

  • 示例一:

    public static void main(String[] args) throws Exception {
        JobConf job = new JobConf();
        ...
            LinkedHashMap<String, String> input = new LinkedHashMap<String, String>();
        input.put("pt", "123456");
        InputUtils.addTable(TableInfo.builder().tableName("input_table").partSpec(input).build(), job);
        LinkedHashMap<String, String> output = new LinkedHashMap<String, String>();
        output.put("ds", "654321");
        OutputUtils.addTable(TableInfo.builder().tableName("output_table").partSpec(output).build(), job);
        JobClient.runJob(job);
    }
  • 示例二:

    package com.aliyun.odps.mapred.open.example;
    ...
        public static void main(String[] args) throws Exception {
        if (args.length != 2) {
            System.err.println("Usage: WordCount <in_table> <out_table>");
            System.exit(2);
        }
        JobConf job = new JobConf();
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(SumCombiner.class);
        job.setReducerClass(SumReducer.class);
        job.setMapOutputKeySchema(SchemaUtils.fromString("word:string"));
        job.setMapOutputValueSchema(SchemaUtils.fromString("count:bigint"));
        // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户
        // 此处以把AccessKey 和 AccessKeySecret 保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里
        // 强烈建议不要把 AccessKey 和 AccessKeySecret 保存到代码里,会存在密钥泄漏风险
        Account account = new AliyunAccount(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        Odps odps = new Odps(account);
        odps.setEndpoint("odps_endpoint_url");
        odps.setDefaultProject("my_project");
        Table table = odps.tables().get(tblname);
        TableInfoBuilder builder = TableInfo.builder().tableName(tblname);
        for (Partition p : table.getPartitions()) {
            if (applicable(p)) {
                LinkedHashMap<String, String> partSpec = new LinkedHashMap<String, String>();
                for (String key : p.getPartitionSpec().keys()) {
                    partSpec.put(key, p.getPartitionSpec().get(key));
                }
                InputUtils.addTable(builder.partSpec(partSpec).build(), job);
            }
        }
        OutputUtils.addTable(TableInfo.builder().tableName(args[1]).build(), job);
        JobClient.runJob(job);
    }
说明
  • 上述示例是使用MaxCompute SDK和MapReduce SDK组合实现MapReduce任务读取范围分区的示例。

  • 此段代码不能够编译执行,仅给出了main函数的示例。

  • 示例中applicable函数是用户逻辑,用于决定该分区是否可以作为MapReduce作业的输入。

  • 本页导读 (1)
文档反馈