This project has retired. For details please refer to its Attic page.
HBaseUtil 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  package org.apache.hadoop.chukwa.util;
19  
20  import java.nio.charset.Charset;
21  import java.security.MessageDigest;
22  import java.security.NoSuchAlgorithmException;
23  import java.util.Calendar;
24  import java.util.TimeZone;
25  
26  import org.apache.hadoop.chukwa.extraction.hbase.AbstractProcessor;
27  import org.apache.log4j.Logger;
28  import org.mortbay.log.Log;
29  
30  public class HBaseUtil {
31    private static Logger LOG = Logger.getLogger(HBaseUtil.class);
32    
33    static MessageDigest md5 = null;
34    static {
35      try {
36        md5 = MessageDigest.getInstance("md5");
37      } catch (NoSuchAlgorithmException e) {
38        LOG.warn(ExceptionUtil.getStackTrace(e));
39      }
40    }
41  
42    public HBaseUtil() throws NoSuchAlgorithmException {
43    }
44  
45    public byte[] buildKey(long time, String metricGroup, String metric,
46        String source) {
47      String fullKey = new StringBuilder(metricGroup).append(".")
48          .append(metric).toString();
49      return buildKey(time, fullKey, source);
50    }
51  
52    public static byte[] buildKey(long time, String primaryKey) {
53      Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
54      c.setTimeInMillis(time);
55      byte[] day = Integer.toString(c.get(Calendar.DAY_OF_YEAR)).getBytes(Charset.forName("UTF-8"));
56      byte[] pk = getHash(primaryKey);
57      byte[] key = new byte[14];
58      System.arraycopy(day, 0, key, 0, day.length);
59      System.arraycopy(pk, 0, key, 2, 6);
60      return key;
61    }
62    
63    public static byte[] buildKey(long time, String primaryKey, String source) {
64      Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
65      c.setTimeInMillis(time);
66      byte[] day = Integer.toString(c.get(Calendar.DAY_OF_YEAR)).getBytes(Charset.forName("UTF-8"));
67      byte[] pk = getHash(primaryKey);
68      byte[] src = getHash(source);
69      byte[] key = new byte[14];
70      System.arraycopy(day, 0, key, 0, day.length);
71      System.arraycopy(pk, 0, key, 2, 6);
72      System.arraycopy(src, 0, key, 8, 6);
73      return key;
74    }
75    
76    private static byte[] getHash(String key) {
77      byte[] hash = new byte[6];
78      System.arraycopy(md5.digest(key.getBytes(Charset.forName("UTF-8"))), 0, hash, 0, 6);
79      return hash;
80    }
81  }