全部产品
Search
文档中心

MaxCompute:Contoh MapOnly

更新时间:Jun 19, 2025

Untuk pekerjaan map-only, mapper secara langsung menghasilkan pasangan kunci-nilai ke tabel MaxCompute. Anda hanya perlu menentukan tabel output tanpa harus mendefinisikan metadata kunci-nilai untuk output dari mapper.

Prasyarat

Selesaikan konfigurasi lingkungan untuk pengujian. Lihat Memulai.

Persiapan

  1. Siapkan paket JAR program uji. Dalam topik ini, paket JAR diberi nama mapreduce-examples.jar dan disimpan di direktori bin\data\resources dalam jalur instalasi lokal MaxCompute.

  2. Siapkan tabel uji dan sumber daya yang digunakan untuk menjalankan pekerjaan map-only.

    1. Buat tabel uji.

      CREATE TABLE wc_in (key STRING, value STRING);
      CREATE TABLE wc_out (key STRING, cnt BIGINT);
    2. Tambahkan sumber daya uji.

      -- Saat menambahkan paket JAR untuk pertama kali, Anda dapat mengabaikan flag -f.
      add jar data\resources\mapreduce-examples.jar -f;
  3. Gunakan Tunnel untuk mengimpor file data.txt dari direktori bin klien MaxCompute ke tabel wc_in.

    tunnel upload data.txt wc_in;

    Data berikut diimpor ke tabel wc_in:

     hello,odps
     hello,odps

Prosedur

Jalankan pekerjaan map-only pada klien MaxCompute.

jar -resources mapreduce-examples.jar -classpath data\resources\mapreduce-examples.jar
com.aliyun.odps.mapred.open.example.MapOnly wc_in wc_out map

Hasil yang Diharapkan

Pekerjaan berjalan normal. Data berikut dikembalikan di tabel wc_out:

+------------+------------+
| key        | cnt        |
+------------+------------+
| hello      | 1          |
| hello      | 1          |
+------------+------------+

Kode Contoh

Untuk informasi tentang dependensi Project Object Model (POM), lihat Peringatan.

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);
            /** Fungsi utama menjalankan logika berikut hanya jika option.mapper.setup diatur ke true dalam file JobConf: */
            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);
            /** Fungsi utama menjalankan logika berikut hanya jika option.mapper.map diatur ke true dalam file JobConf: */
            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);
            /** Fungsi utama menjalankan logika berikut hanya jika option.mapper.cleanup diatur ke true dalam file JobConf: */
            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);
        /** Untuk pekerjaan MapOnly, jumlah reducer harus diatur secara eksplisit menjadi 0. */
        job.setNumReduceTasks(0);
        /** Konfigurasikan informasi tentang tabel input dan output. */
        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]);
            /** Anda dapat menentukan pasangan kunci-nilai dalam file JobConf, dan menggunakan getJobConf dari konteks untuk memeriksa konfigurasi dalam mapper. */
            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);
    }
}