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