mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
hbase support
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -21,10 +21,17 @@ 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.ResultScanner;
|
||||
import org.apache.hadoop.hbase.client.Scan;
|
||||
|
||||
import com.alibaba.druid.common.jdbc.ConnectionBase;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
|
||||
public class HBaseConnection extends ConnectionBase implements Connection {
|
||||
|
||||
@@ -238,16 +245,31 @@ public class HBaseConnection extends ConnectionBase implements Connection {
|
||||
return null;
|
||||
}
|
||||
|
||||
public ResultSet executeQuery(String sql, List<Object> parameters) throws SQLException {
|
||||
String dbType = this.getConnectProperties().getProperty("dbType");
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
|
||||
|
||||
if (stmtList.size() != 1) {
|
||||
throw new SQLException("not support multi-statement");
|
||||
public ResultSet executeQuery(HBaseStatementInterface jdbcHbaseStmt, String sql, List<Object> parameters) throws SQLException {
|
||||
try {
|
||||
String dbType = this.getConnectProperties().getProperty("dbType");
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
|
||||
|
||||
if (stmtList.size() != 1) {
|
||||
throw new SQLException("not support multi-statement");
|
||||
}
|
||||
|
||||
SQLSelectStatement stmt = (SQLSelectStatement) stmtList.get(0);
|
||||
SQLSelectQueryBlock selectQueryBlock = (SQLSelectQueryBlock) stmt.getSelect().getQuery();
|
||||
|
||||
SQLExprTableSource tableSource = (SQLExprTableSource) selectQueryBlock.getFrom();
|
||||
String tableName = ((SQLIdentifierExpr) tableSource.getExpr()).getName();
|
||||
|
||||
HTable htable = new HTable(config, tableName);
|
||||
|
||||
Scan scan = new Scan();
|
||||
ResultScanner scanner = htable.getScanner(scan);
|
||||
|
||||
return new HBaseResultSet(jdbcHbaseStmt, scanner);
|
||||
} catch (SQLException ex) {
|
||||
throw ex;
|
||||
} catch (Exception ex) {
|
||||
throw new SQLException("executeQuery error", ex);
|
||||
}
|
||||
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
throw new SQLException("TODO");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ public class HBaseDriver implements Driver {
|
||||
return null;
|
||||
}
|
||||
|
||||
String rest = url.substring(PREFIX.length());
|
||||
String rest = url.substring(PREFIX.length() + 1);
|
||||
|
||||
info.put("hbase.zookeeper.quorum", rest);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.sql.SQLException;
|
||||
|
||||
import com.alibaba.druid.common.jdbc.PreparedStatementBase;
|
||||
|
||||
public class HBasePreparedStatement extends PreparedStatementBase implements PreparedStatement {
|
||||
public class HBasePreparedStatement extends PreparedStatementBase implements PreparedStatement, HBaseStatementInterface {
|
||||
|
||||
private final String sql;
|
||||
private String[] columnNames;
|
||||
@@ -31,9 +31,14 @@ public class HBasePreparedStatement extends PreparedStatementBase implements Pre
|
||||
return sql;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HBaseConnection getConnection() throws SQLException {
|
||||
return hbaseConnection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSet executeQuery() throws SQLException {
|
||||
return hbaseConnection.executeQuery(sql, getParameters());
|
||||
return hbaseConnection.executeQuery(this, sql, getParameters());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.alibaba.druid.hbase;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import org.apache.hadoop.hbase.client.Result;
|
||||
import org.apache.hadoop.hbase.client.ResultScanner;
|
||||
import org.apache.hadoop.hbase.util.Bytes;
|
||||
|
||||
import com.alibaba.druid.common.jdbc.ResultSetBase;
|
||||
|
||||
public class HBaseResultSet extends ResultSetBase {
|
||||
|
||||
private HBaseStatementInterface statement;
|
||||
private ResultScanner scanner;
|
||||
private Result result;
|
||||
|
||||
public HBaseResultSet(HBaseStatementInterface statement, ResultScanner scanner){
|
||||
super(statement);
|
||||
this.statement = statement;
|
||||
this.scanner = scanner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement getStatement() throws SQLException {
|
||||
return statement;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean next() throws SQLException {
|
||||
try {
|
||||
result = scanner.next();
|
||||
return result != null && !result.isEmpty();
|
||||
} catch (IOException ex) {
|
||||
throw new SQLException("read next error", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResultSetMetaData getMetaData() throws SQLException {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean previous() throws SQLException {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateObject(int columnIndex, Object x) throws SQLException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getObjectInternal(int columnIndex) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String columnName) throws SQLException {
|
||||
byte[] qualifier = Bytes.toBytes(columnName);
|
||||
byte[] family = Bytes.toBytes("d");
|
||||
byte[] value = result.getValue(family, qualifier);
|
||||
return Bytes.toString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
try {
|
||||
scanner.close();
|
||||
} catch (Exception ex) {
|
||||
throw new SQLException("close error", ex);
|
||||
}
|
||||
super.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +1,11 @@
|
||||
package com.alibaba.druid.hbase;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import com.alibaba.druid.common.jdbc.StatementBase;
|
||||
|
||||
public class HBaseStatement extends StatementBase implements Statement {
|
||||
public class HBaseStatement extends StatementBase implements Statement, HBaseStatementInterface {
|
||||
|
||||
private HBaseConnection conn;
|
||||
|
||||
@@ -17,154 +15,10 @@ public class HBaseStatement extends StatementBase implements Statement {
|
||||
}
|
||||
|
||||
@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 void cancel() throws SQLException {
|
||||
|
||||
}
|
||||
|
||||
@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 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 {
|
||||
public HBaseConnection 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 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,8 @@
|
||||
package com.alibaba.druid.hbase;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public interface HBaseStatementInterface extends Statement {
|
||||
HBaseConnection getConnection() throws SQLException;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -16,7 +16,7 @@ public class HBaseDriverTest extends TestCase {
|
||||
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM test");
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
|
||||
System.out.print(rs.getString("id"));
|
||||
}
|
||||
rs.close();
|
||||
stmt.close();
|
||||
|
||||
@@ -27,6 +27,7 @@ log4j.category.org.codehaus.xfire=INFO
|
||||
log4j.category.httpclient=INFO
|
||||
log4j.category.org.apache.struts.action.ActionServlet=ERROR
|
||||
log4j.category.com.alibaba.druid.mock=ERROR
|
||||
log4j.category.org.apache.hadoop.conf.Configuration=INFO
|
||||
log4j.category.org.apache.hadoop=WARN
|
||||
log4j.category.org.apache.zookeeper=ERROR
|
||||
|
||||
log4j.rootLogger=TRACE,CONSOLE
|
||||
|
||||
Reference in New Issue
Block a user