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