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 */
018package org.apache.oozie.action.hadoop;
019
020import static org.apache.oozie.action.hadoop.LauncherMapper.CONF_OOZIE_ACTION_MAIN_CLASS;
021
022import java.io.IOException;
023import java.io.StringReader;
024import java.net.URISyntaxException;
025import java.util.ArrayList;
026import java.util.List;
027import java.util.Properties;
028
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.fs.FileSystem;
031import org.apache.hadoop.fs.Path;
032import org.apache.hadoop.mapred.RunningJob;
033import org.apache.oozie.action.ActionExecutorException;
034import org.apache.oozie.client.WorkflowAction;
035import org.apache.oozie.client.XOozieClient;
036import org.apache.oozie.service.HadoopAccessorException;
037import org.jdom.Element;
038import org.jdom.JDOMException;
039import org.jdom.Namespace;
040
041public class HiveActionExecutor extends ScriptLanguageActionExecutor {
042
043    private static final String HIVE_MAIN_CLASS_NAME = "org.apache.oozie.action.hadoop.HiveMain";
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    public HiveActionExecutor() {
049        super("hive");
050    }
051
052    @Override
053    public List<Class> getLauncherClasses() {
054        List<Class> classes = new ArrayList<Class>();
055        try {
056            classes.add(Class.forName(HIVE_MAIN_CLASS_NAME));
057        }
058        catch (ClassNotFoundException e) {
059            throw new RuntimeException("Class not found", e);
060        }
061        return classes;
062    }
063
064    @Override
065    protected String getLauncherMain(Configuration launcherConf, Element actionXml) {
066        return launcherConf.get(CONF_OOZIE_ACTION_MAIN_CLASS, HIVE_MAIN_CLASS_NAME);
067    }
068
069    @Override
070    @SuppressWarnings("unchecked")
071    Configuration setupActionConf(Configuration actionConf, Context context, Element actionXml,
072                                  Path appPath) throws ActionExecutorException {
073        Configuration conf = super.setupActionConf(actionConf, context, actionXml, appPath);
074
075        Namespace ns = actionXml.getNamespace();
076        String script = actionXml.getChild("script", ns).getTextTrim();
077        String scriptName = new Path(script).getName();
078        String hiveScriptContent = context.getProtoActionConf().get(XOozieClient.HIVE_SCRIPT);
079
080        if (hiveScriptContent == null){
081            addToCache(conf, appPath, script + "#" + scriptName, false);
082        }
083
084        List<Element> params = (List<Element>) actionXml.getChildren("param", ns);
085        String[] strParams = new String[params.size()];
086        for (int i = 0; i < params.size(); i++) {
087            strParams[i] = params.get(i).getTextTrim();
088        }
089        String[] strArgs = null;
090        List<Element> eArgs = actionXml.getChildren("argument", ns);
091        if (eArgs != null && eArgs.size() > 0) {
092            strArgs = new String[eArgs.size()];
093            for (int i = 0; i < eArgs.size(); i++) {
094                strArgs[i] = eArgs.get(i).getTextTrim();
095            }
096        }
097
098        setHiveScript(conf, scriptName, strParams, strArgs);
099        return conf;
100    }
101
102    public static void setHiveScript(Configuration conf, String script, String[] params, String[] args) {
103        conf.set(HIVE_SCRIPT, script);
104        MapReduceMain.setStrings(conf, HIVE_PARAMS, params);
105        MapReduceMain.setStrings(conf, HIVE_ARGS, args);
106    }
107
108    @Override
109    protected boolean getCaptureOutput(WorkflowAction action) throws JDOMException {
110        return true;
111    }
112
113    @Override
114    protected void getActionData(FileSystem actionFs, RunningJob runningJob, WorkflowAction action, Context context)
115            throws HadoopAccessorException, JDOMException, IOException, URISyntaxException {
116        super.getActionData(actionFs, runningJob, action, context);
117
118        if (action.getData() != null) {
119            // Load stored Hadoop jobs ids and promote them as external child
120            // ids on job success
121            Properties props = new Properties();
122            props.load(new StringReader(action.getData()));
123            context.setExternalChildIDs((String) props.get(LauncherMain.HADOOP_JOBS));
124        }
125    }
126
127    /**
128     * Return the sharelib name for the action.
129     *
130     * @return returns <code>hive</code>.
131     * @param actionXml
132     */
133    @Override
134    protected String getDefaultShareLibName(Element actionXml) {
135        return "hive";
136    }
137
138    protected String getScriptName() {
139        return XOozieClient.HIVE_SCRIPT;
140    }
141
142}