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