001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.action.hadoop;
016    
017    import org.apache.hadoop.conf.Configuration;
018    import org.apache.hadoop.fs.FileSystem;
019    import org.apache.hadoop.fs.FSDataOutputStream;
020    import org.apache.hadoop.fs.Path;
021    import org.apache.oozie.action.ActionExecutorException;
022    import org.apache.oozie.client.XOozieClient;
023    import org.apache.oozie.client.WorkflowAction;
024    import org.apache.oozie.util.XLog;
025    import org.apache.oozie.util.XmlUtils;
026    import org.jdom.Element;
027    import org.jdom.Namespace;
028    import org.jdom.JDOMException;
029    import org.mortbay.log.Log;
030    
031    import java.io.IOException;
032    import java.util.List;
033    
034    public class PigActionExecutor extends JavaActionExecutor {
035    
036        public PigActionExecutor() {
037            super("pig");
038        }
039    
040        protected List<Class> getLauncherClasses() {
041            List<Class> classes = super.getLauncherClasses();
042            classes.add(LauncherMain.class);
043            classes.add(MapReduceMain.class);
044            classes.add(PigMain.class);
045            return classes;
046        }
047    
048        protected String getLauncherMain(Configuration launcherConf, Element actionXml) {
049            return launcherConf.get(LauncherMapper.CONF_OOZIE_ACTION_MAIN_CLASS, PigMain.class.getName());
050        }
051    
052        void injectActionCallback(Context context, Configuration launcherConf) {
053        }
054    
055        @Override
056        Configuration setupLauncherConf(Configuration conf, Element actionXml, Path appPath, Context context)
057                throws ActionExecutorException {
058            super.setupLauncherConf(conf, actionXml, appPath, context);
059            Namespace ns = actionXml.getNamespace();
060            String script = actionXml.getChild("script", ns).getTextTrim();
061            String pigName = new Path(script).getName();
062            String pigScriptContent = context.getProtoActionConf().get(XOozieClient.PIG_SCRIPT);
063    
064            Path pigScriptFile = null;
065            if (pigScriptContent != null) { // Create pig script on hdfs if this is
066                // an http submission pig job;
067                FSDataOutputStream dos = null;
068                try {
069                    Path actionPath = context.getActionDir();
070                    pigScriptFile = new Path(actionPath, script);
071                    FileSystem fs = context.getAppFileSystem();
072                    dos = fs.create(pigScriptFile);
073                    dos.writeBytes(pigScriptContent);
074    
075                    addToCache(conf, actionPath, script + "#" + pigName, false);
076                }
077                catch (Exception ex) {
078                    throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "FAILED_OPERATION", XLog
079                            .format("Not able to write pig script file {0} on hdfs", pigScriptFile), ex);
080                }
081                finally {
082                    try {
083                        if (dos != null) {
084                            dos.close();
085                        }
086                    }
087                    catch (IOException ex) {
088                        XLog.getLog(getClass()).error("Error: " + ex.getMessage());
089                    }
090                }
091            }
092            else {
093                addToCache(conf, appPath, script + "#" + pigName, false);
094            }
095    
096            return conf;
097        }
098    
099        @SuppressWarnings("unchecked")
100        Configuration setupActionConf(Configuration actionConf, Context context, Element actionXml, Path appPath)
101                throws ActionExecutorException {
102            super.setupActionConf(actionConf, context, actionXml, appPath);
103            Namespace ns = actionXml.getNamespace();
104    
105            String script = actionXml.getChild("script", ns).getTextTrim();
106            String pigName = new Path(script).getName();
107    
108            List<Element> params = (List<Element>) actionXml.getChildren("param", ns);
109            String[] strParams = new String[params.size()];
110            for (int i = 0; i < params.size(); i++) {
111                strParams[i] = params.get(i).getTextTrim();
112            }
113            String[] strArgs = null;
114            List<Element> eArgs = actionXml.getChildren("argument", ns);
115            if (eArgs != null && eArgs.size() > 0) {
116                strArgs = new String[eArgs.size()];
117                for (int i = 0; i < eArgs.size(); i++) {
118                    strArgs[i] = eArgs.get(i).getTextTrim();
119                }
120            }
121            PigMain.setPigScript(actionConf, pigName, strParams, strArgs);
122            return actionConf;
123        }
124    
125        protected boolean getCaptureOutput(WorkflowAction action) throws JDOMException {
126            return true;
127        }
128    
129    }