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