上一篇部落格說了使用 HBase 的用戶端 API 來操作管理 HBase 中的表,今天我們看看怎樣通過 API 來動作表中的資料。 介紹
在 HBase 中對資料表中的資料的操做我們一般是通過 Table, Put, Get, Delete,Scan,Result等幾個類來實現。 Table 是表對象,對應資料庫中的一張表,我們可以在表上執行添加,修改,刪除和查詢操作。 Put 主要是用了對資料表中的記錄執行寫入/更新操作。 Get 主要是用了對資料表中的記錄執行查詢操作。 Delete 主要是用了對資料表中的記錄執行查詢操作。 Scan 用來在資料表中執行查詢操作。 Result 用來儲存查詢的結果記錄。 寫入資料操作 在寫入資料時,我們需要首先擷取到需要操作的Table對象。 然後建立一個Put對象來執行更新操作,建立對象時需要給定一個行名。 然後在Put對象中添加需要執行的操作,這裡是添加資料。 資料填充完後,在表上執行put操作。 最後,不要忘了關閉表。
private void putRow(String row, String username, String password, String home, String office) throws IOException { Table table = connection.getTable(TableName.valueOf("user")); Put put = new Put(Bytes.toBytes(row)); put.addColumn(Bytes.toBytes("base"), Bytes.toBytes("username"), Bytes.toBytes(username)); put.addColumn(Bytes.toBytes("base"), Bytes.toBytes("password"), Bytes.toBytes(password)); put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("home"), Bytes.toBytes(home)); put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("office"), Bytes.toBytes(office)); table.put(put); table.close(); }
擷取資料 需要擷取到需要操作的Table對象。 建立Get對象來執行擷取操作,建立Get對象的時候需要告訴它是要擷取哪一行資料。 然後在表上執行get操作來擷取資料。 取到資料後,資料是保持在Result對象中,我們可以通過Result對象的一些方法來取得需要的值。 最後,不要忘了關閉表。
private void getRow(String row) throws IOException { Table table = connection.getTable(TableName.valueOf("user")); Get get = new Get(Bytes.toBytes(row)); Result result = table.get(get); if (Bytes.toString(result.getRow()) != null) { StringBuilder sb = new StringBuilder(); sb.append(Bytes.toString(result.getRow())); sb.append("["); sb.append("base:username=" + Bytes.toString(result.getValue(Bytes.toBytes("base"), Bytes.toBytes("username")))); sb.append(", base:password=" + Bytes.toString(result.getValue(Bytes.toBytes("base"), Bytes.toBytes("password")))); sb.append(", address:home=" + Bytes.toString(result.getValue(Bytes.toBytes("address"), Bytes.toBytes("home")))); sb.append(", address:office=" + Bytes.toString(result.getValue(Bytes.toBytes("address"), Bytes.toBytes("office")))); sb.append("]"); System.out.println(sb.toString()); } table.close(); }
刪除資料 需要擷取到需要操作的Table對象。 建立Delete對象來執行刪除操作,建立Delete對象的時候需要告訴它是要刪除哪一行資料。 然後在表上執行delete操作來刪除資料。 最後,不要忘了關閉表。
private void deleteRow(String row) throws IOException { Table table = connection.getTable(TableName.valueOf("user")); Delete delete = new Delete(Bytes.toBytes(row)); table.delete(delete); table.close(); }
查詢資料 需要擷取到需要操作的Table對象。 建立Scan對象來執行查詢操作。 然後在表上執行scan操作並得到ResultScanner對象。 然後我們在ResultScanner上執行迭代操作來擷取其中的值。 最後,不要忘了關閉表。
private void getRows() throws IOException { Table table = connection.getTable(TableName.valueOf("user")); Scan scan = new Scan(); ResultScanner resultScanner = table.getScanner(scan); Iterator<Result> it = resultScanner.iterator(); while (it.hasNext()) { Result result = it.next(); getRow(result); } table.close(); }
完整代碼
最後是完整的執行資料庫操作的例子代碼。
package my.hbasestudy;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;import java.util.Iterator;import java.util.List;public class TestAPI { private static final String TABLE_NAME = "user"; private static final String COLUMN_FAMILY_BASE = "base"; private static final String COLUMN_FAMILY_ADDRESS = "address"; private static final String COLUMN_USERNAME = "username"; private static final String COLUMN_PASSWORD = "password"; private static final String COLUMN_HOME = "home"; private static final String COLUMN_OFFICE = "office"; private Connection connection; public static void main(String[] args) throws Exception { Configuration config = HBaseConfiguration.create(); Connection connection = ConnectionFactory.createConnection(config); long t1 = System.currentTimeMillis(); TestAPI t = new TestAPI(connection); t.listTable(); t.createTable(); t.listTable(); t.putRows(); t.getRows(); t.deleteRows(); t.getRows(); t.deleteTable(); long t2 = System.currentTimeMillis(); System.out.println("Time: " + (t2 - t1)); connection.close(); } public TestAPI(Connection connection) { this.connection = connection; } private void listTable() throws IOException { Admin admin = connection.getAdmin(); try { List<TableDescriptor> tableDescriptors = admin.listTableDescriptors(); for (TableDescriptor tableDescriptor : tableDescriptors) { TableName tableName = tableDescriptor.getTableName(); System.out.println("Table: " + tableName); System.out.println("\texists: " + admin.tableExists(tableName)); System.out.println("\tenabled: " + admin.isTableEnabled(tableName)); } } finally { admin.close(); } } private void createTable() throws IOException { Admin admin = connection.getAdmin(); try { TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(TableName.valueOf(TABLE_NAME)) .addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(COLUMN_FAMILY_BASE)).build()) .addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(COLUMN_FAMILY_ADDRESS)).build()) .build(); admin.createTable(tableDesc); } finally { admin.close(); } } private void deleteTable() throws IOException { Admin admin = connection.getAdmin(); try { admin.disableTable(TableName.valueOf(TABLE_NAME)); admin.deleteTable(TableName.valueOf(TABLE_NAME)); } finally { admin.close(); } } private void putRows() throws IOException { for (int i = 0; i < 10; i++) { putRow("row_" + i, "user_" + i, "password_" + i, "home_" + i, "office_" + i); } } private void putRow(String row, String username, String password, String home, String office) throws IOException { Table table = connection.getTable(TableName.valueOf(TABLE_NAME)); Put put = new Put(Bytes.toBytes(row)); put.addColumn(Bytes.toBytes(COLUMN_FAMILY_BASE), Bytes.toBytes(COLUMN_USERNAME), Bytes.toBytes(username)); put.addColumn(Bytes.toBytes(COLUMN_FAMILY_BASE), Bytes.toBytes(COLUMN_PASSWORD), Bytes.toBytes(password)); put.addColumn(Bytes.toBytes(COLUMN_FAMILY_ADDRESS), Bytes.toBytes(COLUMN_HOME), Bytes.toBytes(home)); put.addColumn(Bytes.toBytes(COLUMN_FAMILY_ADDRESS), Bytes.toBytes(COLUMN_OFFICE), Bytes.toBytes(office)); table.put(put); table.close(); } private void getRows() throws IOException { Table table = connection.getTable(TableName.valueOf(TABLE_NAME)); Scan scan = new Scan(); ResultScanner resultScanner = table.getScanner(scan); Iterator<Result> it = resultScanner.iterator(); while (it.hasNext()) { Result result = it.next(); getRow(result); } table.close(); } private void getRow(String row) throws IOException { Table table = connection.getTable(TableName.valueOf(TABLE_NAME)); Get get = new Get(Bytes.toBytes(row)); Result result = table.get(get); getRow(result); table.close(); } private void getRow(Result result) { if (Bytes.toString(result.getRow()) != null) { StringBuilder sb = new StringBuilder(); sb.append(Bytes.toString(result.getRow())); sb.append("["); sb.append("base:username=" + Bytes.toString(result.getValue(Bytes.toBytes("base"), Bytes.toBytes("username")))); sb.append(", base:password=" + Bytes.toString(result.getValue(Bytes.toBytes("base"), Bytes.toBytes("password")))); sb.append(", address:home=" + Bytes.toString(result.getValue(Bytes.toBytes("address"), Bytes.toBytes("home")))); sb.append(", address:office=" + Bytes.toString(result.getValue(Bytes.toBytes("address"), Bytes.toBytes("office")))); sb.append("]"); System.out.println(sb.toString()); } } private void deleteRows() throws IOException { Table table = connection.getTable(TableName.valueOf(TABLE_NAME)); Scan scan = new Scan(); ResultScanner resultScanner = table.getScanner(scan); Iterator<Result> it = resultScanner.iterator(); while (it.hasNext()) { Result result = it.next(); Delete delete = new Delete(result.getRow()); table.delete(delete); } table.close(); } private void deleteRow(String row) throws IOException { Table table = connection.getTable(TableName.valueOf(TABLE_NAME)); Delete delete = new Delete(Bytes.toBytes(row)); table.delete(delete); table.close(); }}