diff --git a/src/main/java/com/alibaba/druid/hbase/exec/SingleTableQueryExecutePlan.java b/src/main/java/com/alibaba/druid/hbase/exec/SingleTableQueryExecutePlan.java index 34f8bbb272..65e13ff434 100644 --- a/src/main/java/com/alibaba/druid/hbase/exec/SingleTableQueryExecutePlan.java +++ b/src/main/java/com/alibaba/druid/hbase/exec/SingleTableQueryExecutePlan.java @@ -12,6 +12,8 @@ import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.RowFilter; +import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; +import org.apache.hadoop.hbase.util.Bytes; import com.alibaba.druid.hbase.HBaseConnection; import com.alibaba.druid.hbase.HBasePreparedStatement; @@ -27,6 +29,8 @@ public class SingleTableQueryExecutePlan extends SingleTableExecutePlan { private List columeNames = new ArrayList(); private List conditions = new ArrayList(); + private byte[] family = Bytes.toBytes("d"); + public List getConditions() { return conditions; } @@ -49,7 +53,7 @@ public class SingleTableQueryExecutePlan extends SingleTableExecutePlan { HBaseConnection connection = statement.getConnection(); HTableInterface htable = connection.getHTable(getTableName()); String dbType = connection.getConnectProperties().getProperty("dbType"); - + Scan scan = new Scan(); for (SQLExpr item : conditions) { @@ -70,7 +74,27 @@ public class SingleTableQueryExecutePlan extends SingleTableExecutePlan { throw new SQLException("TODO"); } } else { - + byte[] qualifier = Bytes.toBytes(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); } } @@ -83,7 +107,26 @@ public class SingleTableQueryExecutePlan extends SingleTableExecutePlan { throw new SQLException("executeQuery error", e); } } - + + CompareOp toCompareOp(SQLBinaryOperator operator) { + switch (operator) { + case Equality: + return CompareOp.EQUAL; + case NotEqual: + return CompareOp.NOT_EQUAL; + case GreaterThan: + return CompareOp.GREATER; + case GreaterThanOrEqual: + return CompareOp.GREATER_OR_EQUAL; + case LessThan: + return CompareOp.LESS; + case LessThanOrEqual: + return CompareOp.LESS_OR_EQUAL; + default: + throw new UnsupportedOperationException("TODO"); + } + } + void setFilter(Scan scan, Filter filter) { if (scan.getFilter() == null) { scan.setFilter(filter); diff --git a/src/test/java/com/alibaba/druid/hbase/HBaseDriverTest_notRowFilter.java b/src/test/java/com/alibaba/druid/hbase/HBaseDriverTest_notRowFilter.java new file mode 100644 index 0000000000..24628e6ded --- /dev/null +++ b/src/test/java/com/alibaba/druid/hbase/HBaseDriverTest_notRowFilter.java @@ -0,0 +1,35 @@ +package com.alibaba.druid.hbase; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.Properties; + +import junit.framework.TestCase; + + +public class HBaseDriverTest_notRowFilter extends TestCase { + public void test_select_0 () throws Exception { + 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 where name <= ?"); + stmt.setString(1, "user-5"); + + ResultSet rs = stmt.executeQuery(); + while (rs.next()) { + System.out.print(rs.getInt("id")); + System.out.print(' '); + System.out.print(rs.getString("name")); + System.out.print(' '); + System.out.print(rs.getString("gender")); + System.out.print(' '); + System.out.print(rs.getBigDecimal("salary")); + System.out.println(); + } + rs.close(); + stmt.close(); + + conn.close(); + } +}