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 package org.apache.oozie.action.hadoop;
019
020 import org.apache.hadoop.conf.Configuration;
021 import org.apache.oozie.DagELFunctions;
022 import org.apache.oozie.service.HadoopAccessorService;
023 import org.apache.oozie.service.Services;
024 import org.apache.oozie.util.ELEvaluationException;
025 import org.apache.oozie.util.XLog;
026 import org.apache.oozie.workflow.WorkflowInstance;
027 import org.json.simple.JSONValue;
028
029 import java.util.Map;
030
031 /**
032 * Hadoop EL functions.
033 */
034 public class HadoopELFunctions {
035
036 private static final String HADOOP_COUNTERS = "oozie.el.action.hadoop.counters";
037
038 public static final String RECORDS = "org.apache.hadoop.mapred.Task$Counter";
039 public static final String MAP_IN = "MAP_INPUT_RECORDS";
040 public static final String MAP_OUT = "MAP_OUTPUT_RECORDS";
041 public static final String REDUCE_IN = "REDUCE_INPUT_RECORDS";
042 public static final String REDUCE_OUT = "REDUCE_OUTPUT_RECORDS";
043 public static final String GROUPS = "REDUCE_INPUT_GROUPS";
044
045 private static final String RECORDS_023 = "org.apache.hadoop.mapreduce.TaskCounter";
046
047 @SuppressWarnings("unchecked")
048 public static Map<String, Map<String, Long>> hadoop_counters(String nodeName) throws ELEvaluationException {
049 WorkflowInstance instance = DagELFunctions.getWorkflow().getWorkflowInstance();
050 Object obj = instance.getTransientVar(nodeName + WorkflowInstance.NODE_VAR_SEPARATOR + HADOOP_COUNTERS);
051 Map<String, Map<String, Long>> counters = (Map<String, Map<String, Long>>) obj;
052 if (counters == null) {
053 counters = getCounters(nodeName);
054 // In Hadoop 0.23 they deprecated 'org.apache.hadoop.mapred.Task$Counter' and they REMOVED IT
055 // Here we are getting the new Name and inserting it using the old name if the old name is not found
056 if (counters.get(RECORDS) == null) {
057 counters.put(RECORDS, counters.get(RECORDS_023));
058 }
059 instance.setTransientVar(nodeName + WorkflowInstance.NODE_VAR_SEPARATOR + HADOOP_COUNTERS, counters);
060 }
061 return counters;
062 }
063
064 public static String hadoop_conf(String hadoopConfHostPort, String propName) {
065 Configuration conf = Services.get().get(HadoopAccessorService.class)
066 .createJobConf(hadoopConfHostPort);
067 String prop = conf.get(propName);
068 if (prop == null || prop.equals("")) {
069 conf = new Configuration();
070 prop = conf.get(propName);
071 }
072 if (prop == null)
073 prop = "";
074 return prop;
075 }
076
077 @SuppressWarnings("unchecked")
078 private static Map<String, Map<String, Long>> getCounters(String nodeName) throws ELEvaluationException {
079 String jsonCounters = DagELFunctions.getActionVar(nodeName, MapReduceActionExecutor.HADOOP_COUNTERS);
080 if (jsonCounters == null) {
081 throw new IllegalArgumentException(XLog.format("Hadoop counters not available for action [{0}]", nodeName));
082 }
083 return (Map) JSONValue.parse(jsonCounters);
084 }
085
086 }