From 5fc32793373133ef990e62ca2f4958dca1897185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E5=B0=91?= Date: Sat, 19 May 2012 23:20:13 +0800 Subject: [PATCH] hbase support --- .../alibaba/druid/hbase/HBaseConnection.java | 30 +++++----- .../java/com/alibaba/druid/hbase/HDriver.java | 6 +- .../java/com/alibaba/druid/hbase/HEngine.java | 56 +++++++++++++++++++ .../alibaba/druid/hbase/mapping/HMapping.java | 13 ++++- .../druid/hbase/mapping/HMappingColumn.java | 13 ++++- 5 files changed, 100 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/alibaba/druid/hbase/HEngine.java diff --git a/src/main/java/com/alibaba/druid/hbase/HBaseConnection.java b/src/main/java/com/alibaba/druid/hbase/HBaseConnection.java index 6c4f2e95c1..df7bd4f4b8 100644 --- a/src/main/java/com/alibaba/druid/hbase/HBaseConnection.java +++ b/src/main/java/com/alibaba/druid/hbase/HBaseConnection.java @@ -1,6 +1,5 @@ package com.alibaba.druid.hbase; -import java.io.IOException; import java.sql.Array; import java.sql.Blob; import java.sql.CallableStatement; @@ -20,21 +19,26 @@ import java.util.Map; import java.util.Properties; import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.HTableInterface; import com.alibaba.druid.common.jdbc.ConnectionBase; public class HBaseConnection extends ConnectionBase implements Connection { - private Configuration config; + + private final HEngine engine; - public HBaseConnection(String url, Properties info){ - super(url, info); - config = new Configuration(false); + public HBaseConnection(HEngine engine, Properties info){ + super(engine.getUrl(), info); + this.engine = engine; + this.setClientInfo(info); } + public HEngine getEngine() { + return engine; + } + @Override public void close() throws SQLException { @@ -98,7 +102,7 @@ public class HBaseConnection extends ConnectionBase implements Connection { @Override public String getClientInfo(String name) throws SQLException { - return config.get(name); + return engine.getConfig().get(name); } @Override @@ -203,14 +207,14 @@ public class HBaseConnection extends ConnectionBase implements Connection { public void setClientInfo(Properties properties) { for (Map.Entry entry : properties.entrySet()) { if (entry.getKey() != null && entry.getValue() != null) { - config.set(entry.getKey().toString(), entry.getValue().toString()); + engine.getConfig().set(entry.getKey().toString(), entry.getValue().toString()); } } } @Override public void setClientInfo(String name, String value) throws SQLClientInfoException { - this.config.set(name, value); + this.engine.getConfig().set(name, value); } @Override @@ -227,7 +231,7 @@ public class HBaseConnection extends ConnectionBase implements Connection { @Override public T unwrap(Class iface) throws SQLException { if (iface == Configuration.class) { - return (T) config; + return (T) engine.getConfig(); } if (iface == Connection.class || iface == HBaseConnection.class) { @@ -236,9 +240,9 @@ public class HBaseConnection extends ConnectionBase implements Connection { return null; } - - public HTableInterface getHTable(String tableName) throws IOException { - return new HTable(config, tableName); + + public HTableInterface getHTable(String tableName) { + return engine.getHTable(tableName); } } diff --git a/src/main/java/com/alibaba/druid/hbase/HDriver.java b/src/main/java/com/alibaba/druid/hbase/HDriver.java index bae2ae8b41..fb40df47cc 100644 --- a/src/main/java/com/alibaba/druid/hbase/HDriver.java +++ b/src/main/java/com/alibaba/druid/hbase/HDriver.java @@ -16,10 +16,12 @@ public class HDriver implements Driver { } String rest = url.substring(PREFIX.length()); - + + HEngine engine = HEngine.getHEngine(url, info); + info.put("hbase.zookeeper.quorum", rest); - return new HBaseConnection(url, info); + return new HBaseConnection(engine, info); } @Override diff --git a/src/main/java/com/alibaba/druid/hbase/HEngine.java b/src/main/java/com/alibaba/druid/hbase/HEngine.java new file mode 100644 index 0000000000..74878c5d5e --- /dev/null +++ b/src/main/java/com/alibaba/druid/hbase/HEngine.java @@ -0,0 +1,56 @@ +package com.alibaba.druid.hbase; + +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.client.HTableInterface; +import org.apache.hadoop.hbase.client.HTablePool; + +public class HEngine { + + private static ConcurrentMap engines = new ConcurrentHashMap(); + + public static HEngine getHEngine(String url, Properties connectProperties) { + HEngine engine = engines.get(url); + if (engine == null) { + engine = new HEngine(url, connectProperties); + engines.putIfAbsent(url, engine); + engine = engines.get(url); + } + return engine; + } + + // ============================= + + private final String url; + + private HTablePool tablePool; + + private final Configuration config; + + public HEngine(String url, Properties connectProperties){ + super(); + this.url = url; + + config = new Configuration(false); + } + + public String getUrl() { + return url; + } + + public HTablePool getTablePool() { + return tablePool; + } + + public Configuration getConfig() { + return config; + } + + public HTableInterface getHTable(String tableName) { + return tablePool.getTable(tableName); + } + +} diff --git a/src/main/java/com/alibaba/druid/hbase/mapping/HMapping.java b/src/main/java/com/alibaba/druid/hbase/mapping/HMapping.java index ca33f2c115..168c07a526 100644 --- a/src/main/java/com/alibaba/druid/hbase/mapping/HMapping.java +++ b/src/main/java/com/alibaba/druid/hbase/mapping/HMapping.java @@ -8,6 +8,7 @@ import java.util.Map; public class HMapping { private Map columns = new LinkedHashMap(); + private HMappingColumn keyColumn; public Collection getColumns() { return Collections.unmodifiableCollection(columns.values()); @@ -16,8 +17,18 @@ public class HMapping { public void addColumn(HMappingColumn column) { this.columns.put(column.getName(), column); } - + public HMappingColumn getColumn(String columnName) { return columns.get(columnName); } + + public HMappingColumn getKeyColumn() { + return keyColumn; + } + + public void setKeyColumn(HMappingColumn keyColumn) { + keyColumn.setKey(true); + this.keyColumn = keyColumn; + } + } diff --git a/src/main/java/com/alibaba/druid/hbase/mapping/HMappingColumn.java b/src/main/java/com/alibaba/druid/hbase/mapping/HMappingColumn.java index 0046610c4a..d61f6caafd 100644 --- a/src/main/java/com/alibaba/druid/hbase/mapping/HMappingColumn.java +++ b/src/main/java/com/alibaba/druid/hbase/mapping/HMappingColumn.java @@ -2,8 +2,9 @@ package com.alibaba.druid.hbase.mapping; public class HMappingColumn { - private String name; - private Class type; + private String name; + private Class type; + private transient boolean key = false; public String getName() { return name; @@ -21,4 +22,12 @@ public class HMappingColumn { this.type = type; } + public boolean isKey() { + return key; + } + + public void setKey(boolean key) { + this.key = key; + } + }