hbase support

This commit is contained in:
温少
2012-05-19 15:36:08 +08:00
parent 47fda0b1c2
commit 637903029f
4 changed files with 72 additions and 23 deletions
@@ -0,0 +1,37 @@
package com.alibaba.druid.hbase.exec;
import java.io.IOException;
import java.math.BigDecimal;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseUtils {
public static byte[] toBytes(Object value) throws IOException {
if (value == null) {
return null;
}
byte[] bytes;
if (value instanceof String) {
String strValue = (String) value;
bytes = Bytes.toBytes(strValue);
} else if (value instanceof Integer) {
int intValue = ((Integer) value).intValue();
bytes = Bytes.toBytes(intValue);
} else if (value instanceof Long) {
long longValue = ((Long) value).longValue();
bytes = Bytes.toBytes(longValue);
} else if (value instanceof Boolean) {
boolean booleanValue = ((Boolean) value).booleanValue();
bytes = Bytes.toBytes(booleanValue);
} else if (value instanceof BigDecimal) {
BigDecimal decimalValue = (BigDecimal) value;
bytes = Bytes.toBytes(decimalValue);
} else {
throw new IOException("TODO"); // TODO
}
return bytes;
}
}
@@ -1,6 +1,5 @@
package com.alibaba.druid.hbase.exec;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -36,26 +35,7 @@ public class InsertExecutePlan extends SingleTableExecutePlan {
continue;
}
byte[] bytes;
if (value instanceof String) {
String strValue = (String) value;
bytes = Bytes.toBytes(strValue);
} else if (value instanceof Integer) {
int intValue = ((Integer) value).intValue();
bytes = Bytes.toBytes(intValue);
} else if (value instanceof Long) {
long longValue = ((Long) value).longValue();
bytes = Bytes.toBytes(longValue);
} else if (value instanceof Boolean) {
boolean booleanValue = ((Boolean) value).booleanValue();
bytes = Bytes.toBytes(booleanValue);
} else if (value instanceof BigDecimal) {
BigDecimal decimalValue = (BigDecimal) value;
bytes = Bytes.toBytes(decimalValue);
} else {
throw new SQLException("TODO"); // TODO
}
byte[] bytes = HBaseUtils.toBytes(value);
if (put == null) { // first value is key, TODO
put = new Put(bytes);
@@ -12,6 +12,10 @@ import com.alibaba.druid.hbase.HBaseConnection;
import com.alibaba.druid.hbase.HBasePreparedStatement;
import com.alibaba.druid.hbase.HBaseResultSet;
import com.alibaba.druid.sql.ast.SQLExpr;
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.visitor.SQLEvalVisitorUtils;
public class SingleTableQueryExecutePlan extends SingleTableExecutePlan {
@@ -39,8 +43,27 @@ public class SingleTableQueryExecutePlan extends SingleTableExecutePlan {
try {
HBaseConnection connection = statement.getConnection();
HTableInterface htable = connection.getHTable(getTableName());
String dbType = connection.getConnectProperties().getProperty("dbType");
Scan scan = new Scan();
for (SQLExpr item : conditions) {
SQLBinaryOpExpr condition = (SQLBinaryOpExpr) item;
String fieldName = ((SQLIdentifierExpr) condition.getLeft()).getName();
Object value = SQLEvalVisitorUtils.eval(dbType, condition.getRight(), statement.getParameters());
byte[] bytes = HBaseUtils.toBytes(value);
if ("id".equals(fieldName)) {
if (condition.getOperator() == SQLBinaryOperator.GreaterThan) {
scan.setStartRow(bytes);
} else if (condition.getOperator() == SQLBinaryOperator.LessThan) {
scan.setStopRow(bytes);
} else {
throw new SQLException("TODO");
}
}
}
ResultScanner scanner = htable.getScanner(scan);
return new HBaseResultSet(statement, htable, scanner);
@@ -20,6 +20,7 @@ import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlEvalVisitorImpl;
import com.alibaba.druid.util.JdbcUtils;
@@ -40,12 +41,20 @@ public class SQLEvalVisitorUtils {
return eval(dbType, sqlObject, Arrays.asList(parameters));
}
public static Object getValue(SQLObject sqlObject) {
if (sqlObject instanceof SQLNumericLiteralExpr) {
return ((SQLNumericLiteralExpr) sqlObject).getNumber();
}
return sqlObject.getAttributes().get(EVAL_VALUE);
}
public static Object eval(String dbType, SQLObject sqlObject, List<Object> parameters) {
SQLEvalVisitor visitor = createEvalVisitor(dbType);
visitor.setParameters(parameters);
sqlObject.accept(visitor);
Object value = sqlObject.getAttributes().get(EVAL_VALUE);
Object value = getValue(sqlObject);
if (value == null) {
if (!sqlObject.getAttributes().containsKey(EVAL_VALUE)) {
throw new DruidRuntimeException("eval error : " + SQLUtils.toSQLString(sqlObject, dbType));