From 9700338a94792b6c30b126b262355390945868c8 Mon Sep 17 00:00:00 2001 From: Chisu Yu Date: Mon, 24 Sep 2012 05:16:03 +0000 Subject: [PATCH] =?UTF-8?q?[=EC=9C=A0=EC=B9=98=EC=88=98]=20[NOBTS]=20remov?= =?UTF-8?q?e=20hbaseclient=20type=20conversion.?= 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@697 84d0f5b1-2673-498c-a247-62c4ff18d310 --- .../profiler/common/hbase/HBaseClient.java | 120 ++++-------------- .../common/hbase/HBaseClientTest.java | 4 +- 2 files changed, 28 insertions(+), 96 deletions(-) diff --git a/src/main/java/com/profiler/common/hbase/HBaseClient.java b/src/main/java/com/profiler/common/hbase/HBaseClient.java index 6ff397c39..44078056a 100644 --- a/src/main/java/com/profiler/common/hbase/HBaseClient.java +++ b/src/main/java/com/profiler/common/hbase/HBaseClient.java @@ -1,9 +1,7 @@ package com.profiler.common.hbase; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; -import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -26,7 +24,6 @@ import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; -import org.apache.hadoop.hbase.util.Bytes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,45 +33,41 @@ public class HBaseClient { private static final Logger LOG = LoggerFactory.getLogger(HBaseClient.class); - public final static String HBASE_ROW_ID = "ROW_KEY"; - private HTablePool tablePool; private HBaseAdmin admin; private final Map htableList = new HashMap(); - private final Map fieldNameVsType = new HashMap(); - private boolean convertType = false; - public HBaseClient(Properties properties) { - String host = properties.getProperty("hbase.client.host", "localhost"); - String port = properties.getProperty("hbase.client.port", "2181"); - Integer poolSize = NumberUtils.toInt(properties.getProperty("hbase.client.poolSize"), 16); - init(host, port, poolSize, null); + public HBaseClient(Properties properties) { + String host = properties.getProperty("hbase.client.host", "localhost"); + String port = properties.getProperty("hbase.client.port", "2181"); + Integer poolSize = NumberUtils.toInt(properties.getProperty("hbase.client.poolSize"), 16); + init(host, port, poolSize, null); } public HBaseClient(String zk, String port, int poolSize) { init(zk, port, poolSize, null); } - public HBaseClient(String zk, String port, int poolSize, Configuration configuration) { + public HBaseClient(String zk, String port, int poolSize, Configuration configuration) { init(zk, port, poolSize, configuration); } - public HBaseClient(Configuration configuration, int poolSize) { + public HBaseClient(Configuration configuration, int poolSize) { init(null, null, poolSize, configuration); } private void init(String zk, String port, int poolSize, Configuration configuration) { - if(configuration == null) { - Configuration cfg = HBaseConfiguration.create(); - if (zk != null) { - cfg.set("hbase.zookeeper.quorum", zk); - } - if (port != null) { - cfg.set("hbase.zookeeper.property.clientPort", port); - } - configuration = cfg; - } + if (configuration == null) { + Configuration cfg = HBaseConfiguration.create(); + if (zk != null) { + cfg.set("hbase.zookeeper.quorum", zk); + } + if (port != null) { + cfg.set("hbase.zookeeper.property.clientPort", port); + } + configuration = cfg; + } tablePool = new HTablePool(configuration, poolSize); try { @@ -101,7 +94,7 @@ public class HBaseClient { return tablePool; } - public Iterator> getHBaseData(HBaseQuery query) { + public Iterator> getHBaseData(HBaseQuery query) { ResultSetIterator r = new ResultSetIterator(query); return r.getIterator(); } @@ -217,7 +210,7 @@ public class HBaseClient { ResultScanner resultScanner = null; Iterator resultIterator; List columns; - Iterator> rSetIterator; + Iterator> rSetIterator; public ResultSetIterator(HBaseQuery query) { try { @@ -225,10 +218,6 @@ public class HBaseClient { byte[] stopRow = query.getStopRow(); String tableName = query.getTableName(); - System.out.println("startRow=" + startRow); - System.out.println("stopRow=" + stopRow); - System.out.println("tableName=" + tableName); - columns = query.getColumns(); HTableInterface htable = getHTable(tableName); @@ -278,16 +267,16 @@ public class HBaseClient { } if (!resultIterator.hasNext()) { - rSetIterator = new ArrayList>().iterator(); + rSetIterator = new ArrayList>().iterator(); return; } - rSetIterator = new Iterator>() { + rSetIterator = new Iterator>() { public boolean hasNext() { return hasnext(); } - public Map next() { + public Map next() { return getARow(); } @@ -296,62 +285,16 @@ public class HBaseClient { }; } - private Iterator> getIterator() { + private Iterator> getIterator() { return rSetIterator; } - private void addConvertedType(byte[] value, String colName, Map result) { - Integer type = fieldNameVsType.get(colName); - - if (type == null) { - type = HBaseTypes.STRING; - } - switch (type) { - case HBaseTypes.INTEGER: - // result.put(colName, Bytes.toInt(value)); - result.put(colName, Integer.valueOf(Bytes.toString(value))); - break; - case HBaseTypes.FLOAT: - // result.put(colName, Bytes.toFloat(value)); - result.put(colName, Float.valueOf(Bytes.toString(value))); - break; - case HBaseTypes.LONG: - // result.put(colName, Bytes.toLong(value)); - result.put(colName, Long.valueOf(Bytes.toString(value))); - break; - case HBaseTypes.DOUBLE: - // result.put(colName, Bytes.toDouble(value)); - result.put(colName, Double.valueOf(Bytes.toString(value))); - break; - case HBaseTypes.DATE: - result.put(colName, new Date(Bytes.toLong(value))); - // result.put(colName, new - // Date(Long.valueOf(Bytes.toString(value)))); - break; - case HBaseTypes.BOOLEAN: - // result.put(colName, Bytes.toBoolean(value)); - result.put(colName, Boolean.valueOf(Bytes.toString(value))); - break; - case HBaseTypes.BINARY: - result.put(colName, value); - break; - case HBaseTypes.STRING: - result.put(colName, Bytes.toString(value)); - break; - default: - result.put(colName, Bytes.toString(value)); - break; - } - } - - private Map getARow() { + private Map getARow() { if (resultIterator == null) return null; Result res = resultIterator.next(); - System.out.println("next=" + res); - - Map result = new HashMap(); + Map result = new HashMap(); if (!res.isEmpty()) { byte[] value; if (columns != null) { @@ -363,18 +306,10 @@ public class HBaseClient { continue; } - if (!convertType) { - result.put(colName, Bytes.toString(value)); - continue; - } - - // convert type - addConvertedType(value, colName, result); + result.put(colName, value); } } value = res.getRow(); - - addConvertedType(value, HBASE_ROW_ID, result); } return result; @@ -390,7 +325,6 @@ public class HBaseClient { close(); return false; } - } catch (Exception e) { close(); e.printStackTrace(); @@ -409,6 +343,4 @@ public class HBaseClient { } } } - - public static final String CONVERT_TYPE = "convertType"; } diff --git a/src/test/java/com/profiler/common/hbase/HBaseClientTest.java b/src/test/java/com/profiler/common/hbase/HBaseClientTest.java index 8d36fdcde..bcfc75e84 100644 --- a/src/test/java/com/profiler/common/hbase/HBaseClientTest.java +++ b/src/test/java/com/profiler/common/hbase/HBaseClientTest.java @@ -70,10 +70,10 @@ public class HBaseClientTest { 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, Bytes.toBytes("0"), Bytes.toBytes("9"), list)); + Iterator> result = client.getHBaseData(new HBaseQuery(TABLE_NAME, Bytes.toBytes("0"), Bytes.toBytes("9"), list)); while (result.hasNext()) { - Map next = result.next(); + Map next = result.next(); System.out.println(next); } }