This project has retired. For details please refer to its
Attic page.
HBaseUtil xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 }