This project has retired. For details please refer to its Attic page.
Log4JMetricsContextProcessor xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.chukwa.extraction.demux.processor.mapper;
20  
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Map.Entry;
24  import java.util.Set;
25  
26  import org.apache.hadoop.chukwa.extraction.engine.ChukwaRecord;
27  import org.apache.hadoop.chukwa.extraction.engine.ChukwaRecordKey;
28  import org.apache.hadoop.mapred.OutputCollector;
29  import org.apache.hadoop.mapred.Reporter;
30  import org.apache.log4j.Logger;
31  import org.json.simple.JSONObject;
32  import org.json.simple.JSONValue;
33  
34  public class Log4JMetricsContextProcessor extends AbstractProcessor {
35  
36    static Logger log = Logger.getLogger(Log4JMetricsContextProcessor.class);
37  
38    @Override
39    protected void parse(String recordEntry,
40        OutputCollector<ChukwaRecordKey, ChukwaRecord> output, Reporter reporter)
41        throws Throwable 
42    {
43      Log4JMetricsContextChukwaRecord record = new Log4JMetricsContextChukwaRecord(recordEntry);
44      ChukwaRecord chukwaRecord = record.getChukwaRecord();
45      this.buildGenericRecord(chukwaRecord, null, record.getTimestamp(), record.getRecordType());
46      output.collect(key, chukwaRecord);
47    }
48  
49    // create a static class to cove most of the code for unit test 
50    static class Log4JMetricsContextChukwaRecord {
51      private String recordType = null;
52      private long timestamp = 0;
53      private ChukwaRecord chukwaRecord = new ChukwaRecord();
54      
55      @SuppressWarnings("unchecked")
56      public Log4JMetricsContextChukwaRecord(String recordEntry) throws Throwable {
57        LogEntry log = new LogEntry(recordEntry);
58        JSONObject json = (JSONObject) JSONValue.parse(log.getBody());
59  
60        // round timestamp
61        timestamp = (Long) json.get("timestamp");
62        timestamp = (timestamp / 60000) * 60000;
63  
64        // get record type
65        String contextName = (String) json.get("contextName");
66        String recordName = (String) json.get("recordName");
67        recordType = contextName;
68        if (!contextName.equals(recordName)) {
69          recordType += "_" + recordName;
70        }
71  
72        for(Entry<String, Object> entry : (Set<Map.Entry>) json.entrySet()) {
73          String key = entry.getKey();
74          String value = String.valueOf(entry.getValue());
75          if(value != null) {
76            chukwaRecord.add(key, value);
77          }
78        }
79      }
80  
81      public String getRecordType() {
82        return recordType;
83      }
84  
85      public long getTimestamp() {
86        return timestamp;
87      }
88      
89      public ChukwaRecord getChukwaRecord() {
90        return chukwaRecord;
91      }
92    }
93  }
94