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