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 static org.apache.oozie.action.hadoop.LauncherMapper.CONF_OOZIE_ACTION_MAIN_CLASS;
021    
022    import java.io.StringReader;
023    import java.util.List;
024    
025    import org.apache.hadoop.conf.Configuration;
026    import org.apache.hadoop.fs.Path;
027    import org.apache.oozie.action.ActionExecutorException;
028    import org.apache.oozie.client.WorkflowAction;
029    import org.apache.oozie.util.XConfiguration;
030    import org.apache.oozie.util.XmlUtils;
031    import org.jdom.Element;
032    import org.jdom.JDOMException;
033    import org.jdom.Namespace;
034    
035    public class HiveActionExecutor extends JavaActionExecutor {
036        private static final String OOZIE_HIVE_DEFAULTS = "oozie.hive.defaults";
037    
038        public HiveActionExecutor() {
039            super("hive");
040        }
041    
042        @Override
043        protected List<Class> getLauncherClasses() {
044            List<Class> classes = super.getLauncherClasses();
045            classes.add(LauncherMain.class);
046            classes.add(MapReduceMain.class);
047            classes.add(HiveMain.class);
048            return classes;
049        }
050    
051        @Override
052        protected String getLauncherMain(Configuration launcherConf, Element actionXml) {
053            return launcherConf.get(CONF_OOZIE_ACTION_MAIN_CLASS, HiveMain.class.getName());
054        }
055    
056        public Configuration setupHiveDefault(Configuration conf, Path appPath, Element actionXml)
057                throws ActionExecutorException {
058            try {
059                //Setting up hive-default.xml file if specified by the Hive action
060                Element actionConf = actionXml.getChild("configuration", actionXml.getNamespace());
061                if (actionConf != null) {
062                    String strConf = XmlUtils.prettyPrint(actionConf).toString();
063                    XConfiguration inlineConf = new XConfiguration(new StringReader(strConf));
064                    if (inlineConf.get(OOZIE_HIVE_DEFAULTS) != null) {
065                        Path hiveDefaults = new Path(inlineConf.get(OOZIE_HIVE_DEFAULTS));
066                        // hive-default.xml will be softlinked to the working dir which is in the launcher CP.
067                        // the softlink is done as 'oozie-user-hive-default.xml' and Oozie HiveMain class will
068                        // check if the Hive being used has a hive-default.xml or not, if not it will rename
069                        // it as hive-default.xml before invoking hive
070                        addToCache(conf, appPath, hiveDefaults + "#" + HiveMain.USER_HIVE_DEFAULT_FILE, false);
071                    }
072                }
073                return conf;
074            }
075            catch (Exception ex) {
076                throw convertException(ex);
077            }
078        }
079    
080        @Override
081        protected Configuration setupLauncherConf(Configuration conf, Element actionXml, Path appPath, Context context)
082                throws ActionExecutorException {
083            try {
084                super.setupLauncherConf(conf, actionXml, appPath, context);
085                Namespace ns = actionXml.getNamespace();
086    
087                setupHiveDefault(conf, appPath, actionXml);
088    
089                String script = actionXml.getChild("script", ns).getTextTrim();
090                String scriptName = new Path(script).getName();
091                addToCache(conf, appPath, script + "#" + scriptName, false);
092                return conf;
093            }
094            catch (Exception ex) {
095                throw convertException(ex);
096            }
097        }
098    
099        @Override
100        @SuppressWarnings("unchecked")
101        Configuration setupActionConf(Configuration actionConf, Context context, Element actionXml,
102                                      Path appPath) throws ActionExecutorException {
103            Configuration conf = super.setupActionConf(actionConf, context, actionXml, appPath);
104    
105            Namespace ns = actionXml.getNamespace();
106            String script = actionXml.getChild("script", ns).getTextTrim();
107            String scriptName = new Path(script).getName();
108            addToCache(conf, appPath, script + "#" + scriptName, false);
109    
110            List<Element> params = (List<Element>) actionXml.getChildren("param", ns);
111            String[] strParams = new String[params.size()];
112            for (int i = 0; i < params.size(); i++) {
113                strParams[i] = params.get(i).getTextTrim();
114            }
115    
116            HiveMain.setHiveScript(conf, scriptName, strParams);
117            return conf;
118        }
119    
120        @Override
121        protected boolean getCaptureOutput(WorkflowAction action) throws JDOMException {
122            return true;
123        }
124    
125        /**
126         * Return the sharelib postfix for the action.
127         *
128         * @param context executor context.
129         * @param actionXml the action XML.
130         * @return the action sharelib post fix, this implementation returns <code>hive</code>.
131         */
132        protected String getShareLibPostFix(Context context, Element actionXml) {
133            return "hive";
134        }
135    
136    }