Hbase基本操作示例
阿新 • • 發佈:2019-01-30
Hadoop Hbase通過行關鍵字、列(列族名:列名)和時間戳的三元組確定一個儲存單元(cell),即由
{row key, column family, column name, timestamp} 可以唯一確定一個儲存值,即一個鍵值對:
{row key, column family, column name, timestamp} -> value
下面演示了Hbase的基本操作。包括
1、建立表
2、刪除表
3、新增記錄
4、刪除記錄
5、查詢記錄等
忘記的時候可以過來看看。
- import java.io.IOException;
-
import
- import java.util.List;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.HColumnDescriptor;
- import org.apache.hadoop.hbase.HTableDescriptor;
- import org.apache.hadoop.hbase.KeyValue;
-
import
- import org.apache.hadoop.hbase.client.Get;
- import org.apache.hadoop.hbase.client.HBaseAdmin;
- import org.apache.hadoop.hbase.client.HTable;
- import org.apache.hadoop.hbase.client.Put;
- import org.apache.hadoop.hbase.client.Result;
-
import org.apache.hadoop.hbase.client.ResultScanner;
- import org.apache.hadoop.hbase.client.Scan;
- publicclass HBaseOperation {
- // 相關屬性
- private Configuration conf;
- private HBaseAdmin admin;
- public HBaseOperation(Configuration conf) throws IOException {
- this.conf = HBaseConfiguration.create(conf);
- this.conf.set("hbase.zookeeper.quorum", "namenode");
- this.conf.set("hbase.zookeeper.property.clientPort", "2181");
- this.admin = new HBaseAdmin(this.conf);
- }
- public HBaseOperation() throws IOException {
- Configuration cnf = new Configuration();
- this.conf = HBaseConfiguration.create(cnf);
- this.conf.set("hbase.zookeeper.quorum", "namenode");
- this.conf.set("hbase.zookeeper.property.clientPort", "2181");
- this.admin = new HBaseAdmin(this.conf);
- }
- /**
- * 建立表
- * @param tableName 表名
- * @param colFamilies <span style="white-space:pre"> </span>列族
- * @throws IOException
- */
- publicvoid createTable(String tableName, String colFamilies[])
- throws IOException {
- if (this.admin.tableExists(tableName)) { // 表已經存在
- System.out.println("Table: " + tableName + " already exists !");
- this.admin.deleteTable(tableName); // 刪除表
- }
- HTableDescriptor dsc = new HTableDescriptor(tableName); // 新建表描述
- int len = colFamilies.length;
- for (int i = 0; i < len; i++) {
- HColumnDescriptor family = new HColumnDescriptor(colFamilies[i]);
- dsc.addFamily(family); // 在表描述中新增列族
- }
- admin.createTable(dsc); // 建立表
- System.out.println("create table " + tableName + " ok.");
- }
- /**
- * 刪除一張表
- * @param tableName
- * @throws IOException
- */
- publicvoid deleteTable(String tableName) throws IOException {
- if (this.admin.tableExists(tableName)) {
- admin.disableTable(tableName);
- admin.deleteTable(tableName);
- System.out.println("delete table " + tableName + " ok.");
- } else {
- System.out.println("Table Not Exists !");
- }
- }
- /**
- * 新增記錄
- * @param tableName 表名字
- * @param rowkey 行關鍵字
- * @param family 列族
- * @param qualifier 列名
- * @param value 值
- * @throws IOException
- */
- publicvoid insertRecord(String tableName, String rowkey, String family,
- String qualifier, String value) throws IOException {
- HTable table = new HTable(this.conf, tableName);
- Put put = new Put(rowkey.getBytes());
- put.add(family.getBytes(), qualifier.getBytes(), value.getBytes());
- table.put(put);
- System.out.println("insert recored " + rowkey + " to table "
- + tableName + " ok.");
- table.close();
- }
- /**
- * 新增一組記錄
- * @param tableName
- * @param putList
- * @throws IOException
- */
- publicvoid insertRecordList(String tableName, List<Put> putList)
- throws IOException {
- HTable table = new HTable(this.conf, tableName);
- for (int i = 0; i < putList.size(); i++) {
- Put put = putList.get(i);
- table.put(put);
- }
- putList.clear();
- table.close();
- }
- /**
- * 刪除一行資料
- * @param tableName 表名
- * @param rowkey 行關鍵字
- * @throws IOException
- */
- publicvoid deleteRecord(String tableName, String rowkey)
- throws IOException {
- HTable table = new HTable(this.conf, tableName);
- Delete del = new Delete(rowkey.getBytes());
- table.delete(del);
- System.out.println("del recored " + rowkey + " ok.");
- table.close();
- }
- /**
- * 獲取一條記錄
- * @param tableName 表名
- * @param rowkey 行關鍵字
- * @return
- * @throws IOException
- */
- public Result getOneRecord(String tableName, String rowkey)
- throws IOException {
- HTable table = new HTable(this.conf, tableName);
- Get get = new Get(rowkey.getBytes()); //get物件
- Result rs = table.get(get);
- for (KeyValue kv : rs.raw()) {
- System.out.print(new String(kv.getRow()) + " ");
- System.out.print(new String(kv.getFamily()) + ":");
- System.out.print(new String(kv.getQualifier()) + " ");