全部产品
存储与CDN 数据库 安全 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 开发者工具 解决方案 物联网
MaxCompute

WordCount示例

更新时间:2017-10-17 19:44:40

测试准备

  1. 准备好测试程序的 Jar 包,假设名字为 mapreduce-examples.jar,本地存放路径为 data\resources。

  2. 准备好 WordCount 测试表和资源。

    • 创建测试表。

      1. create table wc_in (key string, value string);
      2. create table wc_out(key string, cnt bigint);
    • 添加测试资源

      1. add jar data\resources\mapreduce-examples.jar -f;
  3. 使用 tunnel 导入数据。

    1. tunnel upload data wc_in;

    导入 wc_in 表的数据文件 data 的内容,如下所示:

    1. hello,odps

测试步骤

在 odpscmd 中执行 WordCount,如下所示:

  1. jar -resources mapreduce-examples.jar -classpath data\resources\mapreduce-examples.jar
  2. com.aliyun.odps.mapred.open.example.WordCount wc_in wc_out

预期结果

作业成功结束后,输出表 wc_out 中的内容,如下所示:

  1. +------------+------------+
  2. | key | cnt |
  3. +------------+------------+
  4. | hello | 1 |
  5. | odps | 1 |
  6. +------------+------------+

代码示例

  1. package com.aliyun.odps.mapred.open.example;
  2. import java.io.IOException;
  3. import java.util.Iterator;
  4. import com.aliyun.odps.data.Record;
  5. import com.aliyun.odps.data.TableInfo;
  6. import com.aliyun.odps.mapred.JobClient;
  7. import com.aliyun.odps.mapred.MapperBase;
  8. import com.aliyun.odps.mapred.ReducerBase;
  9. import com.aliyun.odps.mapred.TaskContext;
  10. import com.aliyun.odps.mapred.conf.JobConf;
  11. import com.aliyun.odps.mapred.utils.InputUtils;
  12. import com.aliyun.odps.mapred.utils.OutputUtils;
  13. import com.aliyun.odps.mapred.utils.SchemaUtils;
  14. public class WordCount {
  15. public static class TokenizerMapper extends MapperBase {
  16. private Record word;
  17. private Record one;
  18. @Override
  19. public void setup(TaskContext context) throws IOException {
  20. word = context.createMapOutputKeyRecord();
  21. one = context.createMapOutputValueRecord();
  22. one.set(new Object[] { 1L });
  23. System.out.println("TaskID:" + context.getTaskID().toString());
  24. }
  25. @Override
  26. public void map(long recordNum, Record record, TaskContext context)
  27. throws IOException {
  28. for (int i = 0; i < record.getColumnCount(); i++) {
  29. word.set(new Object[] { record.get(i).toString() });
  30. context.write(word, one);
  31. }
  32. }
  33. }
  34. /**
  35. * A combiner class that combines map output by sum them.
  36. **/
  37. public static class SumCombiner extends ReducerBase {
  38. private Record count;
  39. @Override
  40. public void setup(TaskContext context) throws IOException {
  41. count = context.createMapOutputValueRecord();
  42. }
  43. @Override
  44. public void reduce(Record key, Iterator<Record> values, TaskContext context)
  45. throws IOException {
  46. long c = 0;
  47. while (values.hasNext()) {
  48. Record val = values.next();
  49. c += (Long) val.get(0);
  50. }
  51. count.set(0, c);
  52. context.write(key, count);
  53. }
  54. }
  55. /**
  56. * A reducer class that just emits the sum of the input values.
  57. **/
  58. public static class SumReducer extends ReducerBase {
  59. private Record result = null;
  60. @Override
  61. public void setup(TaskContext context) throws IOException {
  62. result = context.createOutputRecord();
  63. }
  64. @Override
  65. public void reduce(Record key, Iterator<Record> values, TaskContext context)
  66. throws IOException {
  67. long count = 0;
  68. while (values.hasNext()) {
  69. Record val = values.next();
  70. count += (Long) val.get(0);
  71. }
  72. result.set(0, key.get(0));
  73. result.set(1, count);
  74. context.write(result);
  75. }
  76. }
  77. public static void main(String[] args) throws Exception {
  78. if (args.length != 2) {
  79. System.err.println("Usage: WordCount <in_table> <out_table>");
  80. System.exit(2);
  81. }
  82. JobConf job = new JobConf();
  83. job.setMapperClass(TokenizerMapper.class);
  84. job.setCombinerClass(SumCombiner.class);
  85. job.setReducerClass(SumReducer.class);
  86. job.setMapOutputKeySchema(SchemaUtils.fromString("word:string"));
  87. job.setMapOutputValueSchema(SchemaUtils.fromString("count:bigint"));
  88. InputUtils.addTable(TableInfo.builder().tableName(args[0]).build(), job);
  89. OutputUtils.addTable(TableInfo.builder().tableName(args[1]).build(), job);
  90. JobClient.runJob(job);
  91. }
  92. }
本文导读目录