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