hbase support

This commit is contained in:
温少
2012-05-19 15:04:46 +08:00
parent 35f1e3483c
commit 7c7ee9c63c
5 changed files with 53 additions and 12 deletions
@@ -25,7 +25,7 @@ import java.util.Map;
public abstract class ResultSetBase implements ResultSet {
protected boolean closed = false;
private boolean wasNull = false;
protected boolean wasNull = false;
private SQLWarning warning;
private String cursorName;
private int fetchSize = 0;
@@ -1,6 +1,7 @@
package com.alibaba.druid.hbase;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
@@ -76,11 +77,40 @@ public class HBaseResultSet extends ResultSetBase {
if ("@ROW".equals(columnName)) {
return result.getRow();
}
if ("id".equals(columnName)) { // TODO
return result.getRow();
}
byte[] qualifier = Bytes.toBytes(columnName);
byte[] value = result.getValue(family, qualifier);
this.wasNull = value == null;
return value;
}
@Override
public int getInt(String columnName) throws SQLException {
byte[] bytes = getBytes(columnName);
if (bytes == null) {
return 0;
}
return Bytes.toInt(bytes);
}
@Override
public BigDecimal getBigDecimal(String columnName) throws SQLException {
byte[] bytes = getBytes(columnName);
if (bytes == null) {
return null;
}
return Bytes.toBigDecimal(bytes);
}
@Override
public void close() throws SQLException {
@@ -1,5 +1,6 @@
package com.alibaba.druid.hbase.exec;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -22,7 +23,6 @@ public class InsertExecutePlan extends SingleTableExecutePlan {
public boolean execute(HBasePreparedStatement statement) throws SQLException {
try {
HBaseConnection connection = statement.getConnection();
HTableInterface htable = connection.getHTable(getTableName());
String dbType = connection.getConnectProperties().getProperty("dbType");
Put put = null;
@@ -50,6 +50,9 @@ public class InsertExecutePlan extends SingleTableExecutePlan {
} 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
}
@@ -62,6 +65,7 @@ public class InsertExecutePlan extends SingleTableExecutePlan {
}
}
HTableInterface htable = connection.getHTable(getTableName());
htable.put(put);
return false;
@@ -1,5 +1,6 @@
package com.alibaba.druid.hbase;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Properties;
@@ -12,15 +13,17 @@ public class HBaseDriverInsertTest extends TestCase {
HBaseDriver driver = new HBaseDriver();
Connection conn = driver.connect("jdbc:druid-hbase:10.20.153.63", new Properties());
PreparedStatement stmt = conn.prepareStatement("INSERT INTO test_user (id, name, gender) VALUES (?, ?, ?)");
stmt.setInt(1, 33);
stmt.setString(2, "ljw");
stmt.setString(3, "M");
for (int i = 0; i < 10; ++i) {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO test_user (id, name, gender, salary) VALUES (?, ?, ?, ?)");
stmt.setInt(1, i);
stmt.setString(2, "user-" + i);
stmt.setString(3, "M");
stmt.setBigDecimal(4, new BigDecimal((i + 1) * 1000));
stmt.execute();
stmt.execute();
stmt.close();
stmt.close();
}
conn.close();
}
@@ -13,12 +13,16 @@ 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 * FROM test");
PreparedStatement stmt = conn.prepareStatement("SELECT id, name, gender, salary FROM test_user");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.print(rs.getString("@ROW"));
System.out.print(rs.getInt("id"));
System.out.print(' ');
System.out.print(rs.getString("id"));
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();