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.oozie.action.ActionExecutorException;
024import org.apache.oozie.client.XOozieClient;
025import org.apache.oozie.client.WorkflowAction;
026import org.jdom.Element;
027import org.jdom.Namespace;
028import org.jdom.JDOMException;
029import org.json.simple.parser.JSONParser;
030
031import java.util.ArrayList;
032import java.util.List;
033
034public class PigActionExecutor extends ScriptLanguageActionExecutor {
035
036    private static final String PIG_MAIN_CLASS_NAME = "org.apache.oozie.action.hadoop.PigMain";
037    static final String PIG_SCRIPT = "oozie.pig.script";
038    static final String PIG_PARAMS = "oozie.pig.params";
039    static final String PIG_ARGS = "oozie.pig.args";
040
041    public PigActionExecutor() {
042        super("pig");
043    }
044
045    @SuppressWarnings("rawtypes")
046    @Override
047    public List<Class> getLauncherClasses() {
048        List<Class> classes = new ArrayList<Class>();
049        try {
050            classes.add(Class.forName(PIG_MAIN_CLASS_NAME));
051            classes.add(JSONParser.class);
052        }
053        catch (ClassNotFoundException e) {
054            throw new RuntimeException("Class not found", e);
055        }
056        return classes;
057    }
058
059
060    @Override
061    protected String getLauncherMain(Configuration launcherConf, Element actionXml) {
062        return launcherConf.get(LauncherMapper.CONF_OOZIE_ACTION_MAIN_CLASS, PIG_MAIN_CLASS_NAME);
063    }
064
065    @Override
066    void injectActionCallback(Context context, Configuration launcherConf) {
067    }
068
069    @Override
070    @SuppressWarnings("unchecked")
071    Configuration setupActionConf(Configuration actionConf, Context context, Element actionXml, Path appPath)
072            throws ActionExecutorException {
073        super.setupActionConf(actionConf, context, actionXml, appPath);
074        Namespace ns = actionXml.getNamespace();
075
076        String script = actionXml.getChild("script", ns).getTextTrim();
077        String pigName = new Path(script).getName();
078
079        List<Element> params = (List<Element>) actionXml.getChildren("param", ns);
080        String[] strParams = new String[params.size()];
081        for (int i = 0; i < params.size(); i++) {
082            strParams[i] = params.get(i).getTextTrim();
083        }
084        String[] strArgs = null;
085        List<Element> eArgs = actionXml.getChildren("argument", ns);
086        if (eArgs != null && eArgs.size() > 0) {
087            strArgs = new String[eArgs.size()];
088            for (int i = 0; i < eArgs.size(); i++) {
089                strArgs[i] = eArgs.get(i).getTextTrim();
090            }
091        }
092        setPigScript(actionConf, pigName, strParams, strArgs);
093        return actionConf;
094    }
095
096    public static void setPigScript(Configuration conf, String script, String[] params, String[] args) {
097        conf.set(PIG_SCRIPT, script);
098        MapReduceMain.setStrings(conf, PIG_PARAMS, params);
099        MapReduceMain.setStrings(conf, PIG_ARGS, args);
100    }
101
102
103    @Override
104    protected boolean getCaptureOutput(WorkflowAction action) throws JDOMException {
105        return false;
106    }
107
108    /**
109     * Return the sharelib postfix for the action.
110     *
111     * @return returns <code>pig</code>.
112     * @param actionXml
113     */
114    @Override
115    protected String getDefaultShareLibName(Element actionXml) {
116        return "pig";
117    }
118
119    protected String getScriptName() {
120        return XOozieClient.PIG_SCRIPT;
121    }
122
123}