hbase support

This commit is contained in:
温少
2012-05-18 20:08:07 +08:00
parent a8ab7b14d6
commit 41d4720eda
6 changed files with 1002 additions and 3 deletions
+16 -3
View File
@@ -11,7 +11,7 @@
<description>An JDBC datasource implementation.</description>
<url>https://github.com/AlibabaTech/druid</url>
<inceptionYear>2012</inceptionYear>
<repositories>
<repository>
<id>oracleReleases</id>
@@ -226,6 +226,19 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase</artifactId>
<version>0.92.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
@@ -236,14 +249,14 @@
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.2.4</version>
<scope>provided</scope>
<scope>test</scope>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
<scope>provided</scope>
<scope>test</scope>
</dependency>
<!--
<dependency>
@@ -0,0 +1,315 @@
package com.alibaba.druid.hbase;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.hadoop.conf.Configuration;
public class HBaseConnection implements Connection {
private Configuration config;
private boolean autoCommit = true;
private String catalog;
private int transactionIsolation;
private int holdability;
private Map<String, Class<?>> typeMap = new HashMap<String, Class<?>>();
private SQLWarning warings;
public HBaseConnection(Properties info){
config = new Configuration();
this.setClientInfo(info);
}
@SuppressWarnings("unchecked")
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == Configuration.class) {
return (T) config;
}
if (iface == Connection.class || iface == HBaseConnection.class) {
return (T) this;
}
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
if (iface == Configuration.class) {
return true;
}
if (iface == Connection.class || iface == HBaseConnection.class) {
return true;
}
return false;
}
@Override
public Statement createStatement() throws SQLException {
return new HBaseStatement(this);
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public CallableStatement prepareCall(String sql) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public String nativeSQL(String sql) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
this.autoCommit = autoCommit;
}
@Override
public boolean getAutoCommit() throws SQLException {
return autoCommit;
}
@Override
public void commit() throws SQLException {
}
@Override
public void rollback() throws SQLException {
}
@Override
public void close() throws SQLException {
}
@Override
public boolean isClosed() throws SQLException {
return false;
}
@Override
public DatabaseMetaData getMetaData() throws SQLException {
return null;
}
@Override
public void setReadOnly(boolean readOnly) throws SQLException {
}
@Override
public boolean isReadOnly() throws SQLException {
return false;
}
@Override
public void setCatalog(String catalog) throws SQLException {
this.catalog = catalog;
}
@Override
public String getCatalog() throws SQLException {
return catalog;
}
@Override
public void setTransactionIsolation(int level) throws SQLException {
this.transactionIsolation = level;
}
@Override
public int getTransactionIsolation() throws SQLException {
return transactionIsolation;
}
@Override
public SQLWarning getWarnings() throws SQLException {
return warings;
}
@Override
public void clearWarnings() throws SQLException {
this.warings = null;
}
public void setWarings(SQLWarning warings) {
this.warings = warings;
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public Map<String, Class<?>> getTypeMap() throws SQLException {
return typeMap;
}
@Override
public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
this.typeMap = map;
}
@Override
public void setHoldability(int holdability) throws SQLException {
this.holdability = holdability;
}
@Override
public int getHoldability() {
return holdability;
}
@Override
public Savepoint setSavepoint() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public Savepoint setSavepoint(String name) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void rollback(Savepoint savepoint) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public Clob createClob() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public Blob createBlob() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public NClob createNClob() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public SQLXML createSQLXML() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean isValid(int timeout) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void setClientInfo(String name, String value) throws SQLClientInfoException {
this.config.set(name, value);
}
@Override
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());
}
}
}
@Override
public String getClientInfo(String name) throws SQLException {
return config.get(name);
}
@Override
public Properties getClientInfo() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
}
@@ -0,0 +1,42 @@
package com.alibaba.druid.hbase;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.util.Properties;
public class HBaseDriver implements Driver {
@Override
public Connection connect(String url, Properties info) throws SQLException {
return null;
}
@Override
public boolean acceptsURL(String url) throws SQLException {
return false;
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
return null;
}
@Override
public int getMajorVersion() {
return 0;
}
@Override
public int getMinorVersion() {
return 0;
}
@Override
public boolean jdbcCompliant() {
return false;
}
}
@@ -0,0 +1,323 @@
package com.alibaba.druid.hbase;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.NClob;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import com.alibaba.druid.mock.MockParameterMetaData;
import com.alibaba.druid.mock.MockResultSetMetaData;
public class HBasePreparedStatement extends HBaseStatement implements PreparedStatement {
private final List<Object> parameters = new ArrayList<Object>();
private MockParameterMetaData metadata = new MockParameterMetaData();
private MockResultSetMetaData resultSetMetaData = new MockResultSetMetaData();
public HBasePreparedStatement(HBaseConnection conn){
super(conn);
}
@Override
public void setNull(int parameterIndex, int sqlType) throws SQLException {
parameters.add(parameterIndex - 1, null);
}
@Override
public void setBoolean(int parameterIndex, boolean x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setByte(int parameterIndex, byte x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setShort(int parameterIndex, short x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setInt(int parameterIndex, int x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setLong(int parameterIndex, long x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setFloat(int parameterIndex, float x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setDouble(int parameterIndex, double x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setString(int parameterIndex, String x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setBytes(int parameterIndex, byte[] x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setDate(int parameterIndex, Date x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setTime(int parameterIndex, Time x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void clearParameters() throws SQLException {
checkOpen();
parameters.clear();
}
@Override
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setObject(int parameterIndex, Object x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void addBatch() throws SQLException {
checkOpen();
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException {
parameters.add(parameterIndex - 1, reader);
}
@Override
public void setRef(int parameterIndex, Ref x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setBlob(int parameterIndex, Blob x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setClob(int parameterIndex, Clob x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setArray(int parameterIndex, Array x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public ResultSetMetaData getMetaData() throws SQLException {
checkOpen();
return resultSetMetaData;
}
@Override
public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException {
parameters.add(parameterIndex - 1, null);
}
@Override
public void setURL(int parameterIndex, URL x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public ParameterMetaData getParameterMetaData() throws SQLException {
checkOpen();
return metadata;
}
@Override
public void setRowId(int parameterIndex, RowId x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setNString(int parameterIndex, String value) throws SQLException {
parameters.add(parameterIndex - 1, value);
}
@Override
public void setNCharacterStream(int parameterIndex, Reader value, long length) throws SQLException {
parameters.add(parameterIndex - 1, value);
}
@Override
public void setNClob(int parameterIndex, NClob value) throws SQLException {
parameters.add(parameterIndex - 1, value);
}
@Override
public void setClob(int parameterIndex, Reader value, long length) throws SQLException {
parameters.add(parameterIndex - 1, value);
}
@Override
public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException {
parameters.add(parameterIndex - 1, inputStream);
}
@Override
public void setNClob(int parameterIndex, Reader reader, long length) throws SQLException {
parameters.add(parameterIndex - 1, reader);
}
@Override
public void setSQLXML(int parameterIndex, SQLXML xmlObject) throws SQLException {
parameters.add(parameterIndex - 1, xmlObject);
}
@Override
public void setObject(int parameterIndex, Object x, int targetSqlType, int scaleOrLength) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
parameters.add(parameterIndex - 1, reader);
}
@Override
public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {
parameters.add(parameterIndex - 1, x);
}
@Override
public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
parameters.add(parameterIndex - 1, reader);
}
@Override
public void setNCharacterStream(int parameterIndex, Reader value) throws SQLException {
parameters.add(parameterIndex - 1, value);
}
@Override
public void setClob(int parameterIndex, Reader reader) throws SQLException {
parameters.add(parameterIndex - 1, reader);
}
@Override
public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
parameters.add(parameterIndex - 1, inputStream);
}
@Override
public void setNClob(int parameterIndex, Reader reader) throws SQLException {
parameters.add(parameterIndex - 1, reader);
}
@Override
public ResultSet executeQuery() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public int executeUpdate() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean execute() throws SQLException {
checkOpen();
return false;
}
}
@@ -0,0 +1,272 @@
package com.alibaba.druid.hbase;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
public class HBaseStatement implements Statement {
private HBaseConnection conn;
public HBaseStatement(HBaseConnection conn){
super();
this.conn = conn;
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public ResultSet executeQuery(String sql) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public int executeUpdate(String sql) throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void close() throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getMaxFieldSize() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setMaxFieldSize(int max) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getMaxRows() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setMaxRows(int max) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setEscapeProcessing(boolean enable) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getQueryTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setQueryTimeout(int seconds) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void cancel() throws SQLException {
// TODO Auto-generated method stub
}
@Override
public SQLWarning getWarnings() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public void clearWarnings() throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setCursorName(String name) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public boolean execute(String sql) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public ResultSet getResultSet() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public int getUpdateCount() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean getMoreResults() throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public void setFetchDirection(int direction) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getFetchDirection() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setFetchSize(int rows) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int getFetchSize() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getResultSetConcurrency() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getResultSetType() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void addBatch(String sql) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void clearBatch() throws SQLException {
// TODO Auto-generated method stub
}
@Override
public int[] executeBatch() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Connection getConnection() throws SQLException {
return conn;
}
@Override
public boolean getMoreResults(int current) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public ResultSet getGeneratedKeys() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public int executeUpdate(String sql, String[] columnNames) throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean execute(String sql, int[] columnIndexes) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean execute(String sql, String[] columnNames) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public int getResultSetHoldability() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isClosed() throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public void setPoolable(boolean poolable) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public boolean isPoolable() throws SQLException {
// TODO Auto-generated method stub
return false;
}
public void checkOpen() {
}
}
@@ -0,0 +1,34 @@
package com.alibaba.druid.hbase;
import junit.framework.TestCase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseConnectTest extends TestCase {
public void test_connection() throws Exception {
Configuration config = new Configuration();//HBaseConfiguration.create();
config.set("dfs.support.append", "true");
config.set("hbase.rootdir", "hdfs://10.20.153.63:9000/hbase");
config.set("hbase.cluster.distributed", "true");
config.set("hbase.master", "10.20.153.63:60000");
config.set("hbase.zookeeper.quorum", "10.20.153.63");
config.set("zookeeper.session.timeout", "30000");
config.set("hbase.regionserver.handler.count", "100");
config.set("hbase.client.keyvalue.maxsize", "67108864");
// config = HBaseConfiguration.create();
HTable table = new HTable(config, "test");
Put put = new Put(Bytes.toBytes("1001"));
put.add(Bytes.toBytes("d"), Bytes.toBytes("id"), Bytes.toBytes("x1001"));
table.put(put);
table.close();
}
}