This project has retired. For details please refer to its Attic page.
Reporter 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.datacollection.writer.hbase;
20  
21  import java.lang.reflect.Type;
22  import java.nio.charset.Charset;
23  import java.security.MessageDigest;
24  import java.security.NoSuchAlgorithmException;
25  import java.util.ArrayList;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.hadoop.hbase.client.Put;
31  import org.mortbay.log.Log;
32  
33  import com.google.gson.Gson;
34  import com.google.gson.reflect.TypeToken;
35  
36  public class Reporter {
37    private ArrayList<Put> meta = new ArrayList<Put>();
38    private MessageDigest md5 = null;
39    private final static Charset UTF8 = Charset.forName("UTF-8");
40  
41    public Reporter() throws NoSuchAlgorithmException {
42      md5 = MessageDigest.getInstance("md5");
43    }
44  
45    public void putSource(String type, String source) {
46      byte[] value = getHash(source);
47      String buffer;
48  
49      try {
50        Type metaType = new TypeToken<Map<String, String>>(){}.getType();
51        Map<String, String> meta = new HashMap<String, String>();
52        meta.put("sig", new String(value, UTF8));
53        meta.put("type", "source");
54        Gson gson = new Gson();
55        buffer = gson.toJson(meta, metaType);
56        put(type.getBytes(UTF8), source.getBytes(UTF8), buffer.toString().getBytes(UTF8));
57      } catch (Exception e) {
58        Log.warn("Error encoding metadata.");
59        Log.warn(e);
60      }
61    }
62  
63    public void putMetric(String type, String metric) {
64      String buf = new StringBuilder(type).append(".").append(metric).toString();
65      byte[] pk = getHash(buf);
66      String buffer;
67      try {
68        Type metaType = new TypeToken<Map<String, String>>(){}.getType();
69        Map<String, String> meta = new HashMap<String, String>();
70        meta.put("sig", new String(pk, "UTF-8"));
71        meta.put("type", "metric");
72        Gson gson = new Gson();
73        buffer = gson.toJson(meta, metaType);
74        put(type.getBytes(UTF8), metric.getBytes(UTF8), buffer.toString().getBytes(UTF8));
75      } catch (Exception e) {
76        Log.warn("Error encoding metadata.");
77        Log.warn(e);
78      }
79    }
80  
81    public void put(String key, String source, String info) {
82      put(key.getBytes(UTF8), source.getBytes(UTF8), info.getBytes(UTF8));
83    }
84  
85    public void put(byte[] key, byte[] source, byte[] info) {
86      Put put = new Put(key);
87      put.addColumn("k".getBytes(UTF8), source, info);
88      meta.add(put);
89    }
90  
91    public void clear() {
92      meta.clear();
93    }
94  
95    public List<Put> getInfo() {
96      return meta;
97    }
98  
99    private byte[] getHash(String key) {
100     byte[] hash = new byte[5];
101     System.arraycopy(md5.digest(key.getBytes(UTF8)), 0, hash, 0, 5);
102     return hash;
103   }
104 
105   public void putClusterName(String type, String clusterName) {
106     byte[] value = getHash(clusterName);
107     String buffer;
108     Type metaType = new TypeToken<Map<String, String>>(){}.getType();
109     Map<String, String> meta = new HashMap<String, String>();
110     meta.put("sig", new String(value, UTF8));
111     meta.put("type", "cluster");
112     Gson gson = new Gson();
113     buffer = gson.toJson(meta, metaType);
114     put(type.getBytes(UTF8), clusterName.getBytes(UTF8), buffer.toString().getBytes(UTF8));
115   }
116 
117 }