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.mapred.Counters;
021    import org.apache.oozie.DagELFunctions;
022    import org.apache.oozie.util.ELEvaluationException;
023    import org.apache.oozie.util.XLog;
024    import org.apache.oozie.workflow.WorkflowInstance;
025    import org.json.simple.JSONValue;
026    
027    import java.util.HashMap;
028    import java.util.Map;
029    
030    /**
031     * Hadoop EL functions.
032     */
033    public class HadoopELFunctions {
034    
035        private static final String HADOOP_COUNTERS = "oozie.el.action.hadoop.counters";
036    
037        public static final String RECORDS = "org.apache.hadoop.mapred.Task$Counter";
038        public static final String MAP_IN = "MAP_INPUT_RECORDS";
039        public static final String MAP_OUT = "MAP_OUTPUT_RECORDS";
040        public static final String REDUCE_IN = "REDUCE_INPUT_RECORDS";
041        public static final String REDUCE_OUT = "REDUCE_OUTPUT_RECORDS";
042        public static final String GROUPS = "REDUCE_INPUT_GROUPS";
043    
044        @SuppressWarnings("unchecked")
045        public static Map<String, Map<String, Long>> hadoop_counters(String nodeName) throws ELEvaluationException {
046            WorkflowInstance instance = DagELFunctions.getWorkflow().getWorkflowInstance();
047            Object obj = instance.getTransientVar(nodeName + WorkflowInstance.NODE_VAR_SEPARATOR + HADOOP_COUNTERS);
048            Map<String, Map<String, Long>> counters = (Map<String, Map<String, Long>>) obj;
049            if (counters == null) {
050                counters = getCounters(nodeName);
051                instance.setTransientVar(nodeName + WorkflowInstance.NODE_VAR_SEPARATOR + HADOOP_COUNTERS, counters);
052            }
053            return counters;
054        }
055    
056        @SuppressWarnings("unchecked")
057        private static Map<String, Map<String, Long>> getCounters(String nodeName) throws ELEvaluationException {
058            String jsonCounters = DagELFunctions.getActionVar(nodeName, MapReduceActionExecutor.HADOOP_COUNTERS);
059            if (jsonCounters == null) {
060                throw new IllegalArgumentException(XLog.format("Hadoop counters not available for action [{0}]", nodeName));
061            }
062            return (Map) JSONValue.parse(jsonCounters);
063        }
064    
065    }