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 static org.apache.oozie.action.hadoop.LauncherMapper.CONF_OOZIE_ACTION_MAIN_CLASS;
022
023import java.io.IOException;
024import java.net.URISyntaxException;
025import java.util.ArrayList;
026import java.util.List;
027
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.fs.FileSystem;
030import org.apache.hadoop.fs.Path;
031import org.apache.hadoop.mapred.RunningJob;
032import org.apache.oozie.action.ActionExecutorException;
033import org.apache.oozie.client.WorkflowAction;
034import org.apache.oozie.client.XOozieClient;
035import org.apache.oozie.service.HadoopAccessorException;
036import org.jdom.Element;
037import org.jdom.JDOMException;
038import org.jdom.Namespace;
039
040public class HiveActionExecutor extends ScriptLanguageActionExecutor {
041
042    private static final String HIVE_MAIN_CLASS_NAME = "org.apache.oozie.action.hadoop.HiveMain";
043    static final String HIVE_QUERY = "oozie.hive.query";
044    static final String HIVE_SCRIPT = "oozie.hive.script";
045    static final String HIVE_PARAMS = "oozie.hive.params";
046    static final String HIVE_ARGS = "oozie.hive.args";
047
048    private boolean addScriptToCache;
049
050    public HiveActionExecutor() {
051        super("hive");
052        this.addScriptToCache = false;
053    }
054
055    @Override
056    public List<Class> getLauncherClasses() {
057        List<Class> classes = new ArrayList<Class>();
058        try {
059            classes.add(Class.forName(HIVE_MAIN_CLASS_NAME));
060        }
061        catch (ClassNotFoundException e) {
062            throw new RuntimeException("Class not found", e);
063        }
064        return classes;
065    }
066
067    @Override
068    protected boolean shouldAddScriptToCache() {
069        return this.addScriptToCache;
070    }
071
072    @Override
073    protected String getLauncherMain(Configuration launcherConf, Element actionXml) {
074        // CDH: Allow backwards compatibility with deprecated Hive2Main, which had to be renamed for a name-collision
075        //return launcherConf.get(CONF_OOZIE_ACTION_MAIN_CLASS, HIVE_MAIN_CLASS_NAME);
076        String mainClassName = launcherConf.get(CONF_OOZIE_ACTION_MAIN_CLASS, HIVE_MAIN_CLASS_NAME);
077        if (mainClassName.equals("org.apache.oozie.action.hadoop.Hive2Main")) {
078            mainClassName = "org.apache.oozie.action.hadoop.Hive2MainOld";
079            LOG.warn("Using the Hive action with Hive Server 2 is DEPRECATED.  "
080                    + "Users are encouraged to switch to the Hive 2 action instead.");
081        }
082        return mainClassName;
083    }
084
085    @Override
086    @SuppressWarnings("unchecked")
087    Configuration setupActionConf(Configuration actionConf, Context context, Element actionXml,
088                                  Path appPath) throws ActionExecutorException {
089        Configuration conf = super.setupActionConf(actionConf, context, actionXml, appPath);
090
091        Namespace ns = actionXml.getNamespace();
092        Element scriptElement = actionXml.getChild("script", ns);
093        Element queryElement = actionXml.getChild("query", ns);
094        if (scriptElement != null){
095            String script = scriptElement.getTextTrim();
096            String scriptName = new Path(script).getName();
097            this.addScriptToCache = true;
098            conf.set(HIVE_SCRIPT, scriptName);
099        } else if (queryElement != null) {
100            // Unable to use getTextTrim due to https://issues.apache.org/jira/browse/HIVE-8182
101            String query = queryElement.getText();
102            conf.set(HIVE_QUERY, query);
103        } else {
104            throw new ActionExecutorException(ActionExecutorException.ErrorType.ERROR, "INVALID_ARGUMENTS",
105                "Hive action requires one of <script> or <query> to be set. Neither were found.");
106        }
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        MapReduceMain.setStrings(conf, HIVE_PARAMS, strParams);
114
115        String[] strArgs = null;
116        List<Element> eArgs = actionXml.getChildren("argument", ns);
117        if (eArgs != null && eArgs.size() > 0) {
118            strArgs = new String[eArgs.size()];
119            for (int i = 0; i < eArgs.size(); i++) {
120                strArgs[i] = eArgs.get(i).getTextTrim();
121            }
122        }
123        MapReduceMain.setStrings(conf, HIVE_ARGS, strArgs);
124        return conf;
125    }
126
127    @Override
128    protected boolean getCaptureOutput(WorkflowAction action) throws JDOMException {
129        return true;
130    }
131
132    @Override
133    protected void getActionData(FileSystem actionFs, RunningJob runningJob, WorkflowAction action, Context context)
134            throws HadoopAccessorException, JDOMException, IOException, URISyntaxException {
135        super.getActionData(actionFs, runningJob, action, context);
136        readExternalChildIDs(action, context);
137    }
138
139    /**
140     * Return the sharelib name for the action.
141     *
142     * @return returns <code>hive</code>.
143     * @param actionXml
144     */
145    @Override
146    protected String getDefaultShareLibName(Element actionXml) {
147        return "hive";
148    }
149
150    protected String getScriptName() {
151        return XOozieClient.HIVE_SCRIPT;
152    }
153
154}