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