mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
hbase support
This commit is contained in:
@@ -3,6 +3,7 @@ package com.alibaba.druid.hbase;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.common.jdbc.PreparedStatementBase;
|
||||
@@ -12,6 +13,8 @@ import com.alibaba.druid.hbase.exec.SingleTableQueryExecutePlan;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
@@ -36,6 +39,18 @@ public class HBasePreparedStatement extends PreparedStatementBase implements Pre
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
private void splitCondition(List<SQLExpr> conditions, SQLExpr expr) {
|
||||
if (expr instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOpExpr binaryExpr = (SQLBinaryOpExpr) expr;
|
||||
if (binaryExpr.getOperator() == SQLBinaryOperator.BooleanAnd) {
|
||||
splitCondition(conditions, binaryExpr.getLeft());
|
||||
splitCondition(conditions, binaryExpr.getRight());
|
||||
return;
|
||||
}
|
||||
}
|
||||
conditions.add(expr);
|
||||
}
|
||||
|
||||
public void init() throws SQLException {
|
||||
String dbType = this.hbaseConnection.getConnectProperties().getProperty("dbType");
|
||||
@@ -48,6 +63,9 @@ public class HBasePreparedStatement extends PreparedStatementBase implements Pre
|
||||
SQLStatement sqlStmt = stmtList.get(0);
|
||||
if (sqlStmt instanceof SQLSelectStatement) {
|
||||
SQLSelectStatement selectStmt = (SQLSelectStatement) sqlStmt;
|
||||
|
||||
SQLEvalVisitor evalVisitor = SQLEvalVisitorUtils.createEvalVisitor(dbType);
|
||||
selectStmt.accept(evalVisitor);
|
||||
|
||||
SQLSelectQueryBlock selectQueryBlock = (SQLSelectQueryBlock) selectStmt.getSelect().getQuery();
|
||||
|
||||
@@ -56,6 +74,9 @@ public class HBasePreparedStatement extends PreparedStatementBase implements Pre
|
||||
|
||||
SingleTableQueryExecutePlan singleTableQueryExecuetePlan = new SingleTableQueryExecutePlan();
|
||||
singleTableQueryExecuetePlan.setTableName(tableName);
|
||||
|
||||
List<SQLExpr> conditions = new ArrayList<SQLExpr>();
|
||||
splitCondition(conditions, selectQueryBlock.getWhere());
|
||||
|
||||
this.executePlan = singleTableQueryExecuetePlan;
|
||||
} else if (sqlStmt instanceof SQLInsertStatement) {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.alibaba.druid.hbase.exec;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.hadoop.hbase.client.HTableInterface;
|
||||
import org.apache.hadoop.hbase.client.ResultScanner;
|
||||
@@ -12,6 +14,16 @@ import com.alibaba.druid.hbase.HBaseResultSet;
|
||||
|
||||
public class SingleTableQueryExecutePlan extends SingleTableExecutePlan {
|
||||
|
||||
private List<String> columeNames = new ArrayList<String>();
|
||||
|
||||
public SingleTableQueryExecutePlan(){
|
||||
|
||||
}
|
||||
|
||||
public List<String> getColumeNames() {
|
||||
return columeNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HBaseResultSet executeQuery(HBasePreparedStatement statement) throws SQLException {
|
||||
try {
|
||||
|
||||
@@ -13,7 +13,9 @@ public class HBaseDriverTest extends TestCase {
|
||||
HBaseDriver driver = new HBaseDriver();
|
||||
Connection conn = driver.connect("jdbc:druid-hbase:10.20.153.63", new Properties());
|
||||
|
||||
PreparedStatement stmt = conn.prepareStatement("SELECT id, name, gender, salary FROM test_user");
|
||||
PreparedStatement stmt = conn.prepareStatement("SELECT id, name, gender, salary FROM test_user where id > 3 and id < ?");
|
||||
stmt.setInt(1, 6);
|
||||
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
System.out.print(rs.getInt("id"));
|
||||
|
||||
Reference in New Issue
Block a user