Resource

Last Updated: Jun 10, 2016

Test Preparation

(1) Prepare the jar package of test program. Suppose that the package is named “mapreduce-examples.jar”.

(2) Prepare the test table and resource.

  • Create the tables:

    1. create table mr_upload_src(key bigint, value string);
  • Add the resource:
    1. add jar mapreduce-examples.jar -f;
    2. add file import.txt -f;
  • The contents of import.txt:
    1. 1000,odps

Test Steps

Execute Upload on the odpscmd:

  1. jar -resources mapreduce-examples.jar,import.txt -classpath mapreduce-examples.jar
  2. com.aliyun.odps.mapred.open.example.Upload import.txt mr_upload_src;

Test Result

The job ran successfully.The contents in the output table “mr_upload_src”, as follows:

  1. +------------+------------+
  2. | key | value |
  3. +------------+------------+
  4. | 1000 | odps |
  5. +------------+------------+

Sample Code

Sample code (for reference only):

  1. package com.aliyun.odps.mapred.open.example;
  2. import java.io.BufferedInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import com.aliyun.odps.data.Record;
  6. import com.aliyun.odps.data.TableInfo;
  7. import com.aliyun.odps.mapred.JobClient;
  8. import com.aliyun.odps.mapred.MapperBase;
  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. /**
  15. * Upload
  16. *
  17. * Import data from text file into table
  18. *
  19. **/
  20. public class Upload {
  21. public static class UploadMapper extends MapperBase {
  22. @Override
  23. public void setup(TaskContext context) throws IOException {
  24. Record record = context.createOutputRecord();
  25. StringBuilder importdata = new StringBuilder();
  26. BufferedInputStream bufferedInput = null;
  27. try {
  28. byte[] buffer = new byte[1024];
  29. int bytesRead = 0;
  30. String filename = context.getJobConf().get("import.filename");
  31. bufferedInput = context.readResourceFileAsStream(filename);
  32. while ((bytesRead = bufferedInput.read(buffer)) != -1) {
  33. String chunk = new String(buffer, 0, bytesRead);
  34. importdata.append(chunk);
  35. }
  36. String lines[] = importdata.toString().split("\n");
  37. for (int i = 0; i < lines.length; i++) {
  38. String[] ss = lines[i].split(",");
  39. record.set(0, Long.parseLong(ss[0].trim()));
  40. record.set(1, ss[1].trim());
  41. context.write(record);
  42. }
  43. } catch (FileNotFoundException ex) {
  44. throw new IOException(ex);
  45. } catch (IOException ex) {
  46. throw new IOException(ex);
  47. } finally {
  48. }
  49. }
  50. @Override
  51. public void map(long recordNum, Record record, TaskContext context)
  52. throws IOException {
  53. }
  54. }
  55. public static void main(String[] args) throws Exception {
  56. if (args.length != 2) {
  57. System.err.println("Usage: Upload <import_txt> <out_table>");
  58. System.exit(2);
  59. }
  60. JobConf job = new JobConf();
  61. job.setMapperClass(UploadMapper.class);
  62. job.set("import.filename", args[0]);
  63. job.setNumReduceTasks(0);
  64. job.setMapOutputKeySchema(SchemaUtils.fromString("key:bigint"));
  65. job.setMapOutputValueSchema(SchemaUtils.fromString("value:string"));
  66. InputUtils.addTable(TableInfo.builder().tableName("mr_empty").build(), job);
  67. OutputUtils.addTable(TableInfo.builder().tableName(args[1]).build(), job);
  68. JobClient.runJob(job);
  69. }
  70. }

In fact, user has several methods to set up JobConf:

  • Set it through JobConf interface in SDK. This example above is through this method and this method is of the highest priority.
  • In jar command lines, specify new JobConf file through the parameter ‘-conf’. This method is of the lowest priority.
Thank you! We've received your feedback.