hbase support

This commit is contained in:
温少
2012-05-21 21:29:39 +08:00
parent ca42a6a921
commit 664a4b1258
3 changed files with 47 additions and 44 deletions
@@ -1,6 +1,7 @@
package com.alibaba.druid.hdriver.impl.execute;
import com.alibaba.druid.hdriver.impl.mapping.HMapping;
import com.alibaba.druid.hdriver.impl.mapping.HMappingDefaultImpl;
public class SingleTableExecutePlan extends ExecutePlanAdapter {
@@ -17,6 +18,10 @@ public class SingleTableExecutePlan extends ExecutePlanAdapter {
}
public HMapping getMapping() {
HMapping mapping = this.mapping;
if (mapping == null) {
mapping = new HMappingDefaultImpl();
}
return mapping;
}
@@ -1,5 +1,6 @@
package com.alibaba.druid.hdriver.impl.execute;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@@ -33,6 +34,11 @@ public class SingleTableQueryExecutePlan extends SingleTableExecutePlan {
private HResultSetMetaDataImpl resultMetaData;
private String dbType = "hbase";
private Scan scan;
private HPreparedStatementImpl statement;
public SingleTableQueryExecutePlan(){
}
@@ -66,51 +72,13 @@ public class SingleTableQueryExecutePlan extends SingleTableExecutePlan {
}
HBaseConnectionImpl connection = statement.getConnection();
String dbType = connection.getConnectProperties().getProperty("dbType");
Scan scan = new Scan();
scan = new Scan();
this.statement = statement;
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 = mapping.toBytes(fieldName, value);
if (mapping.isRow(fieldName)) {
if (condition.getOperator() == SQLBinaryOperator.GreaterThanOrEqual) {
scan.setStartRow(bytes);
} else if (condition.getOperator() == SQLBinaryOperator.LessThan) {
scan.setStopRow(bytes);
} else if (condition.getOperator() == SQLBinaryOperator.LessThanOrEqual) {
RowFilter filter = new RowFilter(CompareOp.LESS_OR_EQUAL, new BinaryComparator(bytes));
setFilter(scan, filter);
} else {
throw new SQLException("TODO");
}
} else {
byte[] qualifier = mapping.getQualifier(fieldName);
byte[] family = mapping.getFamily(fieldName);
CompareOp compareOp;
if (condition.getOperator() == SQLBinaryOperator.Equality) {
compareOp = CompareOp.EQUAL;
} else if (condition.getOperator() == SQLBinaryOperator.GreaterThan) {
compareOp = CompareOp.GREATER;
} else if (condition.getOperator() == SQLBinaryOperator.GreaterThanOrEqual) {
compareOp = CompareOp.GREATER_OR_EQUAL;
} else if (condition.getOperator() == SQLBinaryOperator.LessThan) {
compareOp = CompareOp.LESS;
} else if (condition.getOperator() == SQLBinaryOperator.LessThanOrEqual) {
compareOp = CompareOp.LESS_OR_EQUAL;
} else if (condition.getOperator() == SQLBinaryOperator.NotEqual) {
compareOp = CompareOp.NOT_EQUAL;
} else {
throw new SQLException("TODO");
}
SingleColumnValueFilter filter = new SingleColumnValueFilter(family, qualifier, compareOp, bytes);
setFilter(scan, filter);
}
setFilter(condition);
}
HTableInterface htable = connection.getHTable(getTableName());
@@ -124,6 +92,39 @@ public class SingleTableQueryExecutePlan extends SingleTableExecutePlan {
throw e;
} catch (Exception e) {
throw new SQLException("executeQuery error", e);
} finally {
scan = null;
this.statement = null;
}
}
private void setFilter(SQLBinaryOpExpr condition) throws IOException,
SQLException {
HMapping mapping = this.getMapping();
String fieldName = ((SQLIdentifierExpr) condition.getLeft()).getName();
Object value = SQLEvalVisitorUtils.eval(dbType, condition.getRight(), statement.getParameters());
byte[] bytes = mapping.toBytes(fieldName, value);
if (mapping.isRow(fieldName)) {
if (condition.getOperator() == SQLBinaryOperator.GreaterThanOrEqual) {
scan.setStartRow(bytes);
} else if (condition.getOperator() == SQLBinaryOperator.LessThan) {
scan.setStopRow(bytes);
} else if (condition.getOperator() == SQLBinaryOperator.LessThanOrEqual) {
RowFilter filter = new RowFilter(CompareOp.LESS_OR_EQUAL, new BinaryComparator(bytes));
setFilter(scan, filter);
} else {
throw new SQLException("TODO");
}
} else {
byte[] qualifier = mapping.getQualifier(fieldName);
byte[] family = mapping.getFamily(fieldName);
CompareOp compareOp = toCompareOp(condition.getOperator());
SingleColumnValueFilter filter = new SingleColumnValueFilter(family, qualifier, compareOp, bytes);
setFilter(scan, filter);
}
}
@@ -1,8 +1,5 @@
package com.alibaba.druid.hdriver.impl.mapping;
import java.io.IOException;
import java.math.BigDecimal;
import org.apache.hadoop.hbase.util.Bytes;
public class HMappingDefaultImpl extends HMappingAdapter implements HMapping {