001 /** 002 * Copyright (c) 2010 Yahoo! Inc. All rights reserved. 003 * Licensed under the Apache License, Version 2.0 (the "License"); 004 * you may not use this file except in compliance with the License. 005 * You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software 010 * distributed under the License is distributed on an "AS IS" BASIS, 011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 * See the License for the specific language governing permissions and 013 * limitations under the License. See accompanying LICENSE file. 014 */ 015 package org.apache.oozie.command.wf; 016 017 import org.apache.hadoop.conf.Configuration; 018 import org.apache.oozie.util.XmlUtils; 019 import org.jdom.Element; 020 import org.jdom.Namespace; 021 import org.apache.oozie.action.hadoop.MapReduceMain; 022 import org.apache.oozie.client.XOozieClient; 023 024 import java.util.ArrayList; 025 import java.util.List; 026 027 public class SubmitPigCommand extends SubmitHttpCommand { 028 029 public SubmitPigCommand(Configuration conf, String authToken) { 030 super("submitPig", "submitPig", conf, authToken); 031 } 032 033 private Element generatePigSection(Configuration conf, Namespace ns) { 034 Element pig = new Element("pig", ns); 035 Element jt = new Element("job-tracker", ns); 036 jt.addContent(conf.get(XOozieClient.JT)); 037 pig.addContent(jt); 038 Element nn = new Element("name-node", ns); 039 nn.addContent(conf.get(XOozieClient.NN)); 040 pig.addContent(nn); 041 042 List<String> Dargs = new ArrayList<String>(); 043 List<String> otherArgs = new ArrayList<String>(); 044 String[] pigArgs = MapReduceMain.getStrings(conf, XOozieClient.PIG_OPTIONS); 045 for (String arg : pigArgs) { 046 if (arg.startsWith("-D")) { 047 Dargs.add(arg); 048 } 049 else { 050 otherArgs.add(arg); 051 } 052 } 053 054 // configuration section 055 if (Dargs.size() > 0) { 056 Element configuration = generateConfigurationSection(Dargs, ns); 057 pig.addContent(configuration); 058 } 059 060 Element script = new Element("script", ns); 061 script.addContent("dummy.pig"); 062 pig.addContent(script); 063 064 // argument section 065 for (String arg : otherArgs) { 066 Element argument = new Element("argument", ns); 067 argument.addContent(arg); 068 pig.addContent(argument); 069 } 070 071 // file section 072 addFileSection(pig, conf, ns); 073 074 // archive section 075 addArchiveSection(pig, conf, ns); 076 077 return pig; 078 } 079 080 private Element generateConfigurationSection(List<String> Dargs, Namespace ns) { 081 Element configuration = new Element("configuration", ns); 082 for (String arg : Dargs) { 083 String name = null, value = null; 084 int pos = arg.indexOf("="); 085 if (pos == -1) { // "-D<name>" or "-D" only 086 name = arg.substring(2, arg.length()); 087 value = ""; 088 } 089 else { // "-D<name>=<value>" 090 name = arg.substring(2, pos); 091 value = arg.substring(pos + 1, arg.length()); 092 } 093 094 Element property = new Element("property", ns); 095 Element nameElement = new Element("name", ns); 096 nameElement.addContent(name); 097 property.addContent(nameElement); 098 Element valueElement = new Element("value", ns); 099 valueElement.addContent(value); 100 property.addContent(valueElement); 101 configuration.addContent(property); 102 } 103 104 return configuration; 105 } 106 107 /* 108 * (non-Javadoc) 109 * 110 * @see 111 * org.apache.oozie.command.wf.SubmitHttpCommand#getWorkflowXml(org.apache 112 * .hadoop.conf.Configuration) 113 */ 114 @Override 115 protected String getWorkflowXml(Configuration conf) { 116 for (String key : MANDATORY_OOZIE_CONFS) { 117 String value = conf.get(key); 118 if (value == null) { 119 throw new RuntimeException(key + " is not specified"); 120 } 121 } 122 123 Namespace ns = Namespace.getNamespace("uri:oozie:workflow:0.2"); 124 Element root = new Element("workflow-app", ns); 125 root.setAttribute("name", "oozie-pig"); 126 127 Element start = new Element("start", ns); 128 start.setAttribute("to", "pig1"); 129 root.addContent(start); 130 131 Element action = new Element("action", ns); 132 action.setAttribute("name", "pig1"); 133 134 Element pig = generatePigSection(conf, ns); 135 action.addContent(pig); 136 137 Element ok = new Element("ok", ns); 138 ok.setAttribute("to", "end"); 139 action.addContent(ok); 140 141 Element error = new Element("error", ns); 142 error.setAttribute("to", "fail"); 143 action.addContent(error); 144 145 root.addContent(action); 146 147 Element kill = new Element("kill", ns); 148 kill.setAttribute("name", "fail"); 149 Element message = new Element("message", ns); 150 message.addContent("Pig failed, error message[${wf:errorMessage(wf:lastErrorNode())}]"); 151 kill.addContent(message); 152 root.addContent(kill); 153 154 Element end = new Element("end", ns); 155 end.setAttribute("name", "end"); 156 root.addContent(end); 157 158 return XmlUtils.prettyPrint(root).toString(); 159 } 160 }