hbase support

This commit is contained in:
温少
2012-05-19 23:20:13 +08:00
parent 185ef3750e
commit 5fc3279337
5 changed files with 100 additions and 18 deletions
@@ -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<Object, Object> 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> T unwrap(Class<T> 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);
}
}
@@ -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
@@ -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<String, HEngine> engines = new ConcurrentHashMap<String, HEngine>();
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);
}
}
@@ -8,6 +8,7 @@ import java.util.Map;
public class HMapping {
private Map<String, HMappingColumn> columns = new LinkedHashMap<String, HMappingColumn>();
private HMappingColumn keyColumn;
public Collection<HMappingColumn> 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;
}
}
@@ -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;
}
}