From 3551511b97dc344ef815d428b72f2c37589c8b1f Mon Sep 17 00:00:00 2001 From: Woonduk Kang Date: Thu, 20 Sep 2012 03:04:50 +0000 Subject: [PATCH] =?UTF-8?q?[=EA=B0=95=EC=9A=B4=EB=8D=95]=20[LUCYSUS-1744]?= =?UTF-8?q?=20hbaseclient=EC=9D=98=20pool=EB=B0=A9=EC=8B=9D=EA=B3=BC=20api?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD.?= 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@671 84d0f5b1-2673-498c-a247-62c4ff18d310 --- .../profiler/common/hbase/HBaseClient.java | 70 ++++++++++++++----- .../profiler/common/hbase/HTableCallBack.java | 11 +++ src/main/resources/hbase.properties | 5 ++ .../common/hbase/HBaseClientTest.java | 13 ++-- 4 files changed, 72 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/profiler/common/hbase/HTableCallBack.java create mode 100644 src/main/resources/hbase.properties diff --git a/src/main/java/com/profiler/common/hbase/HBaseClient.java b/src/main/java/com/profiler/common/hbase/HBaseClient.java index 0ac43cb5b..0873696c8 100644 --- a/src/main/java/com/profiler/common/hbase/HBaseClient.java +++ b/src/main/java/com/profiler/common/hbase/HBaseClient.java @@ -106,16 +106,20 @@ public class HBaseClient { } public void close() { - for (String htableName : htableList.keySet()) { - try { - tablePool.closeTablePool(htableName); - } catch (IOException e) { - e.printStackTrace(); - } - } + // htableList는 안지워워도 되지. + try { + tablePool.close(); + } catch (IOException e) { + // TODO + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } } - public Iterator> getHBaseData(HBaseQuery query) { + HTablePool getTablePool() { + return tablePool; + } + + public Iterator> getHBaseData(HBaseQuery query) { ResultSetIterator r = new ResultSetIterator(query); return r.getIterator(); } @@ -158,14 +162,14 @@ public class HBaseClient { } } - public void flush(byte[] tablename) { - HTable htable = (HTable) tablePool.getTable(tablename); - try { - htable.flushCommits(); - } catch (IOException e) { - e.printStackTrace(); - } - } +// public void flush(byte[] tablename) { +// HTable htable = (HTable) tablePool.getTable(tablename); +// try { +// htable.flushCommits(); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// } public void insert(byte[] tablename, Put put) { HTable htable = (HTable) tablePool.getTable(tablename); @@ -173,7 +177,9 @@ public class HBaseClient { htable.put(put); } catch (IOException e) { e.printStackTrace(); - } + } finally { + closeHTable(htable); + } } public void insert(byte[] tablename, List put) { @@ -182,7 +188,9 @@ public class HBaseClient { htable.put(put); } catch (IOException e) { e.printStackTrace(); - } + } finally { + closeHTable(htable); + } } public void delete(byte[] tablename, Delete delete) { @@ -191,9 +199,33 @@ public class HBaseClient { htable.delete(delete); } catch (IOException e) { e.printStackTrace(); - } + } 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); + } + } + } + private class ResultSetIterator { ResultScanner resultScanner = null; Iterator resultIterator; diff --git a/src/main/java/com/profiler/common/hbase/HTableCallBack.java b/src/main/java/com/profiler/common/hbase/HTableCallBack.java new file mode 100644 index 000000000..23c8eca96 --- /dev/null +++ b/src/main/java/com/profiler/common/hbase/HTableCallBack.java @@ -0,0 +1,11 @@ +package com.profiler.common.hbase; + +import org.apache.hadoop.hbase.client.HTable; + +import java.io.IOException; + +public interface HTableCallBack { + void doExecute(HTable hTable) throws IOException; + +// void doMultiExecute(HTable... tables) throws IOException; +} diff --git a/src/main/resources/hbase.properties b/src/main/resources/hbase.properties new file mode 100644 index 000000000..293ae9edd --- /dev/null +++ b/src/main/resources/hbase.properties @@ -0,0 +1,5 @@ +#hbase.client.host=localhost +#hbase.client.host=10.64.84.188 +hbase.client.host=10.25.131.38 + +hbase.client.port=2181 \ No newline at end of file diff --git a/src/test/java/com/profiler/common/hbase/HBaseClientTest.java b/src/test/java/com/profiler/common/hbase/HBaseClientTest.java index ae731527f..6aba1aa97 100644 --- a/src/test/java/com/profiler/common/hbase/HBaseClientTest.java +++ b/src/test/java/com/profiler/common/hbase/HBaseClientTest.java @@ -9,6 +9,8 @@ import junit.framework.Assert; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; +import org.apache.hadoop.hbase.client.HTableInterface; +import org.apache.hadoop.hbase.client.HTablePool; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.util.Bytes; import org.junit.BeforeClass; @@ -44,12 +46,14 @@ 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); @@ -66,14 +70,7 @@ public class HBaseClientTest { } client.insert(Bytes.toBytes(TABLE_NAME), putList); - client.flush(Bytes.toBytes(TABLE_NAME)); - try { - Thread.sleep(10000L); - } catch (InterruptedException e) { - e.printStackTrace(); - } - List list = new ArrayList(); list.add(new HbaseColumn(COLUMN_FAMILY, "qual1")); list.add(new HbaseColumn(COLUMN_FAMILY, "qual2"));