001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.action.hadoop;
020
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.fs.Path;
023import org.apache.hadoop.mapred.JobConf;
024import org.apache.oozie.action.ActionExecutorException;
025import org.apache.oozie.action.ActionExecutor.Context;
026import org.apache.oozie.client.XOozieClient;
027import org.apache.oozie.client.WorkflowAction;
028import org.apache.oozie.service.ConfigurationService;
029import org.apache.oozie.service.HadoopAccessorService;
030import org.apache.oozie.service.Services;
031import org.apache.oozie.service.WorkflowAppService;
032import org.jdom.Element;
033import org.jdom.Namespace;
034import org.jdom.JDOMException;
035import org.json.simple.parser.JSONParser;
036
037import java.util.ArrayList;
038import java.util.List;
039
040public class PigActionExecutor extends ScriptLanguageActionExecutor {
041
042    private static final String PIG_MAIN_CLASS_NAME = "org.apache.oozie.action.hadoop.PigMain";
043    static final String PIG_SCRIPT = "oozie.pig.script";
044    static final String PIG_PARAMS = "oozie.pig.params";
045    static final String PIG_ARGS = "oozie.pig.args";
046
047    public PigActionExecutor() {
048        super("pig");
049    }
050
051    @SuppressWarnings("rawtypes")
052    @Override
053    public List<Class> getLauncherClasses() {
054        List<Class> classes = new ArrayList<Class>();
055        try {
056            classes.add(Class.forName(PIG_MAIN_CLASS_NAME));
057            classes.add(JSONParser.class);
058        }
059        catch (ClassNotFoundException e) {
060            throw new RuntimeException("Class not found", e);
061        }
062        return classes;
063    }
064
065
066    @Override
067    protected String getLauncherMain(Configuration launcherConf, Element actionXml) {
068        return launcherConf.get(LauncherMapper.CONF_OOZIE_ACTION_MAIN_CLASS, PIG_MAIN_CLASS_NAME);
069    }
070
071    @Override
072    void injectActionCallback(Context context, Configuration launcherConf) {
073    }
074
075    @Override
076    @SuppressWarnings("unchecked")
077    Configuration setupActionConf(Configuration actionConf, Context context, Element actionXml, Path appPath)
078            throws ActionExecutorException {
079        super.setupActionConf(actionConf, context, actionXml, appPath);
080        Namespace ns = actionXml.getNamespace();
081
082        String script = actionXml.getChild("script", ns).getTextTrim();
083        String pigName = new Path(script).getName();
084
085        List<Element> params = (List<Element>) actionXml.getChildren("param", ns);
086        String[] strParams = new String[params.size()];
087        for (int i = 0; i < params.size(); i++) {
088            strParams[i] = params.get(i).getTextTrim();
089        }
090        String[] strArgs = null;
091        List<Element> eArgs = actionXml.getChildren("argument", ns);
092        if (eArgs != null && eArgs.size() > 0) {
093            strArgs = new String[eArgs.size()];
094            for (int i = 0; i < eArgs.size(); i++) {
095                strArgs[i] = eArgs.get(i).getTextTrim();
096            }
097        }
098        setPigScript(actionConf, pigName, strParams, strArgs);
099        return actionConf;
100    }
101
102    public static void setPigScript(Configuration conf, String script, String[] params, String[] args) {
103        conf.set(PIG_SCRIPT, script);
104        MapReduceMain.setStrings(conf, PIG_PARAMS, params);
105        MapReduceMain.setStrings(conf, PIG_ARGS, args);
106    }
107
108
109    @Override
110    protected boolean getCaptureOutput(WorkflowAction action) throws JDOMException {
111        return false;
112    }
113
114    /**
115     * Return the sharelib postfix for the action.
116     *
117     * @return returns <code>pig</code>.
118     * @param actionXml
119     */
120    @Override
121    protected String getDefaultShareLibName(Element actionXml) {
122        return "pig";
123    }
124
125    protected String getScriptName() {
126        return XOozieClient.PIG_SCRIPT;
127    }
128
129    @Override
130    protected JobConf loadHadoopDefaultResources(Context context, Element actionXml) {
131        boolean loadDefaultResources = ConfigurationService
132                .getBoolean(HadoopAccessorService.ACTION_CONFS_LOAD_DEFAULT_RESOURCES);
133        JobConf conf = super.createBaseHadoopConf(context, actionXml, loadDefaultResources);
134        return conf;
135    }
136}