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