This project has retired. For details please refer to its Attic page.
HadoopMetricsProcessor 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.hbase;
20  
21  import java.nio.charset.Charset;
22  import java.security.NoSuchAlgorithmException;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.Map.Entry;
27  
28  import org.apache.log4j.Logger;
29  import org.json.simple.JSONObject;
30  import org.json.simple.JSONValue;
31  
32  public class HadoopMetricsProcessor extends AbstractProcessor {
33    
34    static Logger LOG = Logger.getLogger(HadoopMetricsProcessor.class);
35    static final String timestampField = "timestamp";
36    static final String contextNameField = "contextName";
37    static final String recordNameField = "recordName";
38    static final String hostName = "Hostname";
39    static final String processName = "ProcessName";
40    static final byte[] cf = "t".getBytes(Charset.forName("UTF-8"));
41  
42    public HadoopMetricsProcessor() throws NoSuchAlgorithmException {
43    }
44  
45    @Override
46    protected void parse(byte[] recordEntry) throws Throwable {
47        String body = new String(recordEntry, Charset.forName("UTF-8"));
48        int start = body.indexOf('{');
49        JSONObject json = (JSONObject) JSONValue.parse(body.substring(start));
50  
51        time = ((Long) json.get(timestampField)).longValue();
52        String contextName = (String) json.get(contextNameField);
53        String recordName = (String) json.get(recordNameField);
54        String src = ((String) json.get(hostName)).toLowerCase();
55        if(json.get(processName)!=null) {
56          src = new StringBuilder(src).append(":").append(json.get(processName)).toString();
57        }
58        for(Entry<String, Object> entry : (Set<Map.Entry>) json.entrySet()) {
59          String keyName = entry.getKey();
60          if (timestampField.intern() == keyName.intern()) {
61            continue;
62          } else if (contextNameField.intern() == keyName.intern()) {
63            continue;
64          } else if (recordNameField.intern() == keyName.intern()) {
65            continue;
66          } else if (hostName.intern() == keyName.intern()) {
67            continue;
68          } else if (processName.intern() == keyName.intern()) {
69            continue;
70          } else {
71            if(json.get(keyName)!=null) {
72              String v = entry.getValue().toString();
73              String primaryKey = new StringBuilder(contextName).append(".")
74                  .append(recordName).append(".").append(keyName).toString();
75              addRecord(time, primaryKey, src, v.getBytes(Charset.forName("UTF-8")), output);
76            }
77          }
78        }
79    }
80  
81  }