すべてのプロダクト
Search
ドキュメントセンター

E-MapReduce:Pig ジョブの構成

最終更新日:Jan 11, 2025

このトピックでは、Pig ジョブを構成する方法について説明します。

前提条件

  • プロジェクトが作成されていること。詳細については、「プロジェクトの管理」をご参照ください。
  • Pig スクリプトが準備されていること。サンプルコード:
     /*
     * Licensed to the Apache Software Foundation (ASF) under one
     * or more contributor license agreements.  See the NOTICE file
     * distributed with this work for additional information
     * regarding copyright ownership.  The ASF licenses this file
     * to you under the Apache License, Version 2.0 (the
     * "License"); you may not use this file except in compliance
     * with the License.  You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
     -- クエリフレーズの人気度(Hadoop クラスタ)
     -- このスクリプトは、Excite 検索エンジンの検索クエリログファイルを処理し、特定の時間帯に特に高い頻度で発生する検索フレーズを見つけます。
     -- スクリプトで含まれている UDF を呼び出すことができるように、チュートリアル JAR ファイルを登録します。
     REGISTER oss://emr/checklist/jars/chengtao/pig/tutorial.jar;
     -- PigStorage 関数を使用して、excite ログファイルをレコードの配列として "raw" バッグにロードします。
     -- 入力: (user,time,query) 
     raw = LOAD 'oss://emr/checklist/data/chengtao/pig/excite.log.bz2' USING PigStorage('\t') AS (user, time, query);
     -- query フィールドが空または URL の場合、NonURLDetector UDF を呼び出してレコードを削除します。
     clean1 = FILTER raw BY org.apache.pig.tutorial.NonURLDetector(query);
     -- ToLower UDF を呼び出して、query フィールドを小文字に変更します。
     clean2 = FOREACH clean1 GENERATE user, time, org.apache.pig.tutorial.ToLower(query) as query;
     -- ログファイルには 1 日分のクエリのみが含まれているため、時間だけに注目します。
     -- excite クエリログのタイムスタンプ形式は YYMMDDHHMMSS です。
     -- ExtractHour UDF を呼び出して、time フィールドから時間(HH)を抽出します。
     houred = FOREACH clean2 GENERATE user, org.apache.pig.tutorial.ExtractHour(time) as hour, query;
     -- NGramGenerator UDF を呼び出して、クエリの n-gram を構成します。
     ngramed1 = FOREACH houred GENERATE user, hour, flatten(org.apache.pig.tutorial.NGramGenerator(query)) as ngram;
     -- DISTINCT コマンドを使用して、すべてのレコードの一意の n-gram を取得します。
     ngramed2 = DISTINCT ngramed1;
     -- GROUP コマンドを使用して、n-gram と時間でレコードをグループ化します。
     hour_frequency1 = GROUP ngramed2 BY (ngram, hour);
     -- COUNT 関数を使用して、各 n-gram のカウント(出現回数)を取得します。
     hour_frequency2 = FOREACH hour_frequency1 GENERATE flatten($0), COUNT($1) as count;
     -- GROUP コマンドを使用して、n-gram のみでレコードをグループ化します。
     -- 各グループは、個別の n-gram に対応し、各時間のカウントを持ちます。
     uniq_frequency1 = GROUP hour_frequency2 BY group::ngram;
     -- 各グループについて、この n-gram が特に高い頻度で使用される時間を特定します。
     -- ScoreGenerator UDF を呼び出して、n-gram の「人気度」スコアを計算します。
     uniq_frequency2 = FOREACH uniq_frequency1 GENERATE flatten($0), flatten(org.apache.pig.tutorial.ScoreGenerator($1));
     -- FOREACH-GENERATE コマンドを使用して、フィールドに名前を割り当てます。
     uniq_frequency3 = FOREACH uniq_frequency2 GENERATE $1 as hour, $0 as ngram, $2 as score, $3 as count, $4 as mean;
     -- FILTER コマンドを使用して、スコアが 2.0 以下のすべてのレコードを移動します。
     filtered_uniq_frequency = FILTER uniq_frequency3 BY score > 2.0;
     -- ORDER コマンドを使用して、残りのレコードを時間とスコアでソートします。
     ordered_uniq_frequency = ORDER filtered_uniq_frequency BY hour, score;
     -- PigStorage 関数を使用して、結果を保存します。
     -- 出力: (hour, n-gram, score, count, average_counts_among_all_hours)
     STORE ordered_uniq_frequency INTO 'oss://emr/checklist/data/chengtao/pig/script1-hadoop-results' USING PigStorage();
  • script1-hadoop-oss.pig ファイルを保存し、oss://path/to/script1-hadoop-oss.pig などの OSS のディレクトリにアップロードします。

手順

  1. [データプラットフォーム] タブに移動します。
    1. Alibaba Cloud アカウントを使用して、Alibaba Cloud EMR コンソール にログインします。
    2. 上部のナビゲーションバーで、クラスタが存在するリージョンを選択し、ビジネス要件に基づいてリソースグループを選択します
    3. [データプラットフォーム] タブをクリックします。
  2. [プロジェクト] セクションで、プロジェクトを見つけ、[アクション] 列の [ジョブの編集] をクリックします。
  3. Pig ジョブを作成します。
    1. 左側の [ジョブの編集] ペインで、操作を実行するフォルダを右クリックし、[ジョブの作成] を選択します。
    2. [ジョブの作成] ダイアログボックスで、[名前][説明] を指定し、Pig[ジョブタイプ] ドロップダウンリストから を選択します。
      このオプションは、Pig ジョブが作成されることを示します。Pig ジョブを送信するには、次のコマンド構文を使用できます。
      pig [user provided parameters]
    3. [OK] をクリックします。
  4. ジョブの内容を編集します。
    1. [コンテンツ] フィールドに、ジョブの送信に必要なコマンドラインパラメータを指定します。
      たとえば、OSS にアップロードされた Pig スクリプトを使用するには、次のコマンドを入力します。
      -x mapreduce ossref://emr/checklist/jars/chengtao/pig/script1-hadoop-oss.pig
      説明 ページの下部にある [+ OSS パスを入力] をクリックします。[OSS ファイル] ダイアログボックスで、[ファイルプレフィックス][OSSREF] に設定し、[ファイルパス] を指定します。システムは、OSS 内の Pig スクリプトのパスを自動的に補完します。
    2. [保存] をクリックします。