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