MaxCompute用户指南:MapReduce:示例程序:使用资源示例
测试准备
准备好测试程序的 Jar 包,假设名字为 mapreduce-examples.jar,本地存放路径为 data\resources。
准备好测试表和资源。
创建测试表。create table mr_upload_src(key bigint, value string);
添加测试资源。
-
add jar data\resources\mapreduce-examples.jar -f;
- add file data\resources\import.txt -f;
import.txt 的数据内容,如下所示:
-
1000,odps
测试步骤
在 odpscmd 中执行 Upload,如下所示:
- jar -resources mapreduce-examples.jar,import.txt -classpath data\resources\mapreduce-examples.jar
- com.aliyun.odps.mapred.open.example.Upload import.txt mr_upload_src;
预期结果
作业成功结束后,输出表 mr_upload_src 中的内容,如下所示:
- +------------+------------+
- | key | value |
- +------------+------------+
- | 1000 | odps |
- +------------+------------+
代码示例
- package com.aliyun.odps.mapred.open.example;
- import java.io.BufferedInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import com.aliyun.odps.data.Record;
- import com.aliyun.odps.data.TableInfo;
- import com.aliyun.odps.mapred.JobClient;
- import com.aliyun.odps.mapred.MapperBase;
- import com.aliyun.odps.mapred.TaskContext;
- import com.aliyun.odps.mapred.conf.JobConf;
- import com.aliyun.odps.mapred.utils.InputUtils;
- import com.aliyun.odps.mapred.utils.OutputUtils;
- import com.aliyun.odps.mapred.utils.SchemaUtils;
- /**
- * Upload
- *
- * Import data from text file into table
- *
- **/
- public class Upload {
- public static class UploadMapper extends MapperBase {
- @Override
- public void setup(TaskContext context) throws IOException {
- Record record = context.createOutputRecord();
- StringBuilder importdata = new StringBuilder();
- BufferedInputStream bufferedInput = null;
- try {
- byte[] buffer = new byte[1024];
- int bytesRead = 0;
- String filename = context.getJobConf().get("import.filename");
- bufferedInput = context.readResourceFileAsStream(filename);
- while ((bytesRead = bufferedInput.read(buffer)) != -1) {
- String chunk = new String(buffer, 0, bytesRead);
- importdata.append(chunk);
- }
- String lines[] = importdata.toString().split("\n");
- for (int i = 0; i < lines.length; i++) {
- String[] ss = lines.split(",");
- record.set(0, Long.parseLong(ss[0].trim()));
- record.set(1, ss[1].trim());
- context.write(record);
- }
- } catch (FileNotFoundException ex) {
- throw new IOException(ex);
- } catch (IOException ex) {
- throw new IOException(ex);
- } finally {
- }
- }
- @Override
- public void map(long recordNum, Record record, TaskContext context)
- throws IOException {
- }
- }
- public static void main(String[] args) throws Exception {
- if (args.length != 2) {
- System.err.println("Usage: Upload <import_txt> <out_table>");
- System.exit(2);
- }
- JobConf job = new JobConf();
- job.setMapperClass(UploadMapper.class);
- job.set("import.filename", args[0]);
- job.setNumReduceTasks(0);
- job.setMapOutputKeySchema(SchemaUtils.fromString("key:bigint"));
- job.setMapOutputValueSchema(SchemaUtils.fromString("value:string"));
- InputUtils.addTable(TableInfo.builder().tableName("mr_empty").build(), job);
- OutputUtils.addTable(TableInfo.builder().tableName(args[1]).build(), job);
- JobClient.runJob(job);
- }
- }
实际上,您有多种手段设置 JobConf。如下所示:
通过 SDK 中 JobConf 的接口设置,本示例即是通过此方法实现。此方法的优先级最低
在 Jar 命令行中,通过 –conf 参数指定新的 JobConf 文件。此方法的优先级最低。
收起
行者武松
2017-10-23 17:45:45
2336
0
0
条回答
写回答
取消
提交回答