上一篇博客說(shuō)了使用 HBase 的客戶(hù)端 API 來(lái)操作管理 HBase 中的表弦追,今天我們看看怎樣通過(guò) API 來(lái)操作表中的數(shù)據(jù)予颤。
介紹
在 HBase 中對(duì)數(shù)據(jù)表中的數(shù)據(jù)的操做我們一般是通過(guò) Table缰趋, Put总放, Get, Delete谷浅,Scan扒俯,Result等幾個(gè)類(lèi)來(lái)實(shí)現(xiàn)奶卓。
- Table 是表對(duì)象,對(duì)應(yīng)數(shù)據(jù)庫(kù)中的一張表撼玄,我們可以在表上執(zhí)行添加夺姑,修改,刪除和查詢(xún)操作掌猛。
- Put 主要是用了對(duì)數(shù)據(jù)表中的記錄執(zhí)行寫(xiě)入/更新操作瑟幕。
- Get 主要是用了對(duì)數(shù)據(jù)表中的記錄執(zhí)行查詢(xún)操作。
- Delete 主要是用了對(duì)數(shù)據(jù)表中的記錄執(zhí)行查詢(xún)操作留潦。
- Scan 用來(lái)在數(shù)據(jù)表中執(zhí)行查詢(xún)操作只盹。
- Result 用來(lái)保存查詢(xún)的結(jié)果記錄。
寫(xiě)入數(shù)據(jù)操作
- 在寫(xiě)入數(shù)據(jù)時(shí)兔院,我們需要首先獲取到需要操作的Table對(duì)象殖卑。
- 然后創(chuàng)建一個(gè)Put對(duì)象來(lái)執(zhí)行更新操作,創(chuàng)建對(duì)象時(shí)需要給定一個(gè)行名坊萝。
- 然后在Put對(duì)象中添加需要執(zhí)行的操作孵稽,這里是添加數(shù)據(jù)。
- 數(shù)據(jù)填充完后十偶,在表上執(zhí)行put操作菩鲜。
- 最后,不要忘了關(guān)閉表惦积。
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();
}
獲取數(shù)據(jù)
- 需要獲取到需要操作的Table對(duì)象接校。
- 創(chuàng)建Get對(duì)象來(lái)執(zhí)行獲取操作,創(chuàng)建Get對(duì)象的時(shí)候需要告訴它是要獲取哪一行數(shù)據(jù)狮崩。
- 然后在表上執(zhí)行g(shù)et操作來(lái)獲取數(shù)據(jù)蛛勉。
- 取到數(shù)據(jù)后,數(shù)據(jù)是保持在Result對(duì)象中睦柴,我們可以通過(guò)Result對(duì)象的一些方法來(lái)取得需要的值诽凌。
- 最后,不要忘了關(guān)閉表坦敌。
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();
}
刪除數(shù)據(jù)
- 需要獲取到需要操作的Table對(duì)象侣诵。
- 創(chuàng)建Delete對(duì)象來(lái)執(zhí)行刪除操作,創(chuàng)建Delete對(duì)象的時(shí)候需要告訴它是要?jiǎng)h除哪一行數(shù)據(jù)狱窘。
- 然后在表上執(zhí)行delete操作來(lái)刪除數(shù)據(jù)杜顺。
- 最后,不要忘了關(guān)閉表训柴。
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();
}
查詢(xún)數(shù)據(jù)
- 需要獲取到需要操作的Table對(duì)象哑舒。
- 創(chuàng)建Scan對(duì)象來(lái)執(zhí)行查詢(xún)操作。
- 然后在表上執(zhí)行scan操作并得到ResultScanner對(duì)象幻馁。
- 然后我們?cè)赗esultScanner上執(zhí)行迭代操作來(lái)獲取其中的值洗鸵。
- 最后越锈,不要忘了關(guān)閉表。
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();
}
完整代碼
最后是完整的執(zhí)行數(shù)據(jù)庫(kù)操作的例子代碼膘滨。
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();
}
}