From 220c76e17b9633052869b43e8aabb3698abb8c0d Mon Sep 17 00:00:00 2001 From: Chisu Yu Date: Fri, 21 Sep 2012 02:35:53 +0000 Subject: [PATCH] =?UTF-8?q?[=EC=9C=A0=EC=B9=98=EC=88=98]=20[NOBTS]=20add?= =?UTF-8?q?=20hbase=20tables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-commons/trunk@679 84d0f5b1-2673-498c-a247-62c4ff18d310 --- .../profiler/common/hbase/HBaseClient.java | 156 ++++++++++-------- .../com/profiler/common/hbase/HBaseQuery.java | 14 +- .../profiler/common/hbase/HBaseTables.java | 8 + .../com/profiler/common/util/SpanUtils.java | 27 +++ .../common/hbase/HBaseClientTest.java | 7 +- 5 files changed, 129 insertions(+), 83 deletions(-) create mode 100644 src/main/java/com/profiler/common/hbase/HBaseTables.java create mode 100644 src/main/java/com/profiler/common/util/SpanUtils.java diff --git a/src/main/java/com/profiler/common/hbase/HBaseClient.java b/src/main/java/com/profiler/common/hbase/HBaseClient.java index e9b626823..a03cb6d9f 100644 --- a/src/main/java/com/profiler/common/hbase/HBaseClient.java +++ b/src/main/java/com/profiler/common/hbase/HBaseClient.java @@ -54,39 +54,39 @@ public class HBaseClient { } private HBaseClient(int poolSize) { - Properties properties = readProperties(); - String host = properties.getProperty("hbase.client.host"); - String port = properties.getProperty("hbase.client.port"); - init(host, port, poolSize); + Properties properties = readProperties(); + String host = properties.getProperty("hbase.client.host"); + String port = properties.getProperty("hbase.client.port"); + init(host, port, poolSize); } - public HBaseClient(String zk, String port, int poolSize) { - init(zk, port, poolSize); - } + public HBaseClient(String zk, String port, int poolSize) { + init(zk, port, poolSize); + } - Properties readProperties() { - Properties properties = new Properties(); - InputStream stream = HBaseClient.class.getClassLoader().getResourceAsStream("hbase.properties"); - if(stream == null) { - throw new RuntimeException("hbase.properties not found"); - } - try { - properties.load(stream); - } catch (IOException e) { - throw new RuntimeException("hbase.properties load fail. " + e.getMessage(), e); - } finally { - if(stream != null) { - try { - stream.close(); - } catch (IOException e) { - // 무시 - } - } - } - return properties; - } + Properties readProperties() { + Properties properties = new Properties(); + InputStream stream = HBaseClient.class.getClassLoader().getResourceAsStream("hbase.properties"); + if (stream == null) { + throw new RuntimeException("hbase.properties not found"); + } + try { + properties.load(stream); + } catch (IOException e) { + throw new RuntimeException("hbase.properties load fail. " + e.getMessage(), e); + } finally { + if (stream != null) { + try { + stream.close(); + } catch (IOException e) { + // 무시 + } + } + } + return properties; + } - private void init(String zk, String port, int poolSize) { + private void init(String zk, String port, int poolSize) { Configuration cfg = HBaseConfiguration.create(); if (zk != null) { cfg.set("hbase.zookeeper.quorum", zk); @@ -107,19 +107,20 @@ public class HBaseClient { public void close() { // htableList는 안지워워도 되지. - try { - tablePool.close(); - } catch (IOException e) { - // TODO - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. - } + try { + tablePool.close(); + } catch (IOException e) { + // TODO + e.printStackTrace(); // To change body of catch statement use File | + // Settings | File Templates. + } } - HTablePool getTablePool() { - return tablePool; - } + HTablePool getTablePool() { + return tablePool; + } - public Iterator> getHBaseData(HBaseQuery query) { + public Iterator> getHBaseData(HBaseQuery query) { ResultSetIterator r = new ResultSetIterator(query); return r.getIterator(); } @@ -162,7 +163,6 @@ public class HBaseClient { } } - public void insert(String tablename, Put put) { HTable htable = (HTable) tablePool.getTable(tablename); try { @@ -170,8 +170,8 @@ public class HBaseClient { } catch (IOException e) { e.printStackTrace(); } finally { - closeHTable(htable); - } + closeHTable(htable); + } } public void insert(String tablename, List put) { @@ -181,8 +181,8 @@ public class HBaseClient { } catch (IOException e) { e.printStackTrace(); } finally { - closeHTable(htable); - } + closeHTable(htable); + } } public void delete(String tablename, Delete delete) { @@ -192,31 +192,45 @@ public class HBaseClient { } catch (IOException e) { e.printStackTrace(); } finally { - closeHTable(htable); - } + closeHTable(htable); + } } - public void execute(String tableName, HTableCallBack callBack) { - HTable htable = (HTable) tablePool.getTable(tableName); - try { - callBack.doExecute(htable); - } catch (IOException e) { - e.printStackTrace(); - // TODO ex 처리 - } finally { - closeHTable(htable); - } - } + public void execute(String tableName, HTableCallBack callBack) { + HTable htable = (HTable) tablePool.getTable(tableName); + try { + callBack.doExecute(htable); + } catch (IOException e) { + e.printStackTrace(); + // TODO ex 처리 + } finally { + closeHTable(htable); + } + } - private void closeHTable(HTable htable) { - if (htable != null) { - try { - htable.close(); - } catch (IOException e) { - LOG.warn(e.getMessage(), e); - } - } - } + public Result[] get(String tablename, List get) { + HTable htable = (HTable) tablePool.getTable(tablename); + + try { + return htable.get(get); + } catch (IOException e) { + e.printStackTrace(); + // TODO ex 처리 + } finally { + closeHTable(htable); + } + return new Result[0]; + } + + private void closeHTable(HTable htable) { + if (htable != null) { + try { + htable.close(); + } catch (IOException e) { + LOG.warn(e.getMessage(), e); + } + } + } private class ResultSetIterator { ResultScanner resultScanner = null; @@ -226,8 +240,8 @@ public class HBaseClient { public ResultSetIterator(HBaseQuery query) { try { - String startRow = query.getStartRow(); - String stopRow = query.getStopRow(); + byte[] startRow = query.getStartRow(); + byte[] stopRow = query.getStopRow(); String tableName = query.getTableName(); System.out.println("startRow=" + startRow); @@ -240,7 +254,7 @@ public class HBaseClient { if (query.isSingleRow()) { System.out.println("Query single row"); - Get get = new Get(Bytes.toBytes(startRow)); + Get get = new Get(startRow); if (columns != null) { for (HbaseColumn column : columns) { @@ -258,11 +272,11 @@ public class HBaseClient { Scan scan = new Scan(); if (startRow != null) { - scan.setStartRow(startRow.getBytes()); + scan.setStartRow(startRow); } if (stopRow != null) { - scan.setStopRow(stopRow.getBytes()); + scan.setStopRow(stopRow); } if (columns != null) { diff --git a/src/main/java/com/profiler/common/hbase/HBaseQuery.java b/src/main/java/com/profiler/common/hbase/HBaseQuery.java index 666db7d3d..2ef4a0dd9 100644 --- a/src/main/java/com/profiler/common/hbase/HBaseQuery.java +++ b/src/main/java/com/profiler/common/hbase/HBaseQuery.java @@ -8,9 +8,9 @@ public class HBaseQuery { private String tableName; - private String startRow; + private byte[] startRow; - private String stopRow; + private byte[] stopRow; private List columns; @@ -44,7 +44,7 @@ public class HBaseQuery { super(); } - public HBaseQuery(String tableName, String startRow, String stopRow, List columns) { + public HBaseQuery(String tableName, byte[] startRow, byte[] stopRow, List columns) { super(); this.tableName = tableName; this.startRow = startRow; @@ -92,19 +92,19 @@ public class HBaseQuery { this.tableName = tableName; } - public String getStartRow() { + public byte[] getStartRow() { return startRow; } - public void setStartRow(String startRow) { + public void setStartRow(byte[] startRow) { this.startRow = startRow; } - public String getStopRow() { + public byte[] getStopRow() { return stopRow; } - public void setStopRow(String stopRow) { + public void setStopRow(byte[] stopRow) { this.stopRow = stopRow; } diff --git a/src/main/java/com/profiler/common/hbase/HBaseTables.java b/src/main/java/com/profiler/common/hbase/HBaseTables.java new file mode 100644 index 000000000..9b2541e2e --- /dev/null +++ b/src/main/java/com/profiler/common/hbase/HBaseTables.java @@ -0,0 +1,8 @@ +package com.profiler.common.hbase; + +public class HBaseTables { + + public static final String TRACE_INDEX = "TraceIndex"; + + public static final String TRACES = "Traces"; +} diff --git a/src/main/java/com/profiler/common/util/SpanUtils.java b/src/main/java/com/profiler/common/util/SpanUtils.java new file mode 100644 index 000000000..c4061302a --- /dev/null +++ b/src/main/java/com/profiler/common/util/SpanUtils.java @@ -0,0 +1,27 @@ +package com.profiler.common.util; + +import org.apache.commons.lang.ArrayUtils; +import org.apache.hadoop.hbase.util.Bytes; + +import com.profiler.common.dto.thrift.Span; + +public class SpanUtils { + + public static byte[] getTraceIndexRowKey(Span span) { + byte[] agentId = Bytes.toBytes(span.getAgentID()); + byte[] time = Bytes.toBytes(span.getTimestamp()); + + return ArrayUtils.addAll(agentId, time); + } + + public static byte[] getTracesRowkey(Span span) { + return ArrayUtils.addAll(Bytes.toBytes(span.getMostTraceID()), Bytes.toBytes(span.getLeastTraceID())); + } + + public static byte[] getTraceId(Span span) { + byte[] mostTid = Bytes.toBytes(span.getMostTraceID()); + byte[] leastTid = Bytes.toBytes(span.getLeastTraceID()); + + return ArrayUtils.addAll(mostTid, leastTid); + } +} diff --git a/src/test/java/com/profiler/common/hbase/HBaseClientTest.java b/src/test/java/com/profiler/common/hbase/HBaseClientTest.java index 098158600..cd3fd5ddd 100644 --- a/src/test/java/com/profiler/common/hbase/HBaseClientTest.java +++ b/src/test/java/com/profiler/common/hbase/HBaseClientTest.java @@ -46,14 +46,12 @@ public class HBaseClientTest { Assert.assertTrue("Table is not exists", client.isTableExists(TABLE_NAME)); - - client.dropTable(TABLE_NAME); + client.dropTable(TABLE_NAME); client.isTableExists(TABLE_NAME); Assert.assertFalse("Table is not dropped", client.isTableExists(TABLE_NAME)); } - @Test public void insertRow() { HTableDescriptor desc = new HTableDescriptor(TABLE_NAME); @@ -70,11 +68,10 @@ public class HBaseClientTest { } client.insert(TABLE_NAME, putList); - List list = new ArrayList(); list.add(new HbaseColumn(COLUMN_FAMILY, "qual1")); list.add(new HbaseColumn(COLUMN_FAMILY, "qual2")); - Iterator> result = client.getHBaseData(new HBaseQuery(TABLE_NAME, "0", "9", list)); + Iterator> result = client.getHBaseData(new HBaseQuery(TABLE_NAME, Bytes.toBytes("0"), Bytes.toBytes("9"), list)); while (result.hasNext()) { Map next = result.next();