溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

HBase中SHELL操作和API的用法示例

發(fā)布時間:2021-12-08 15:04:32 來源:億速云 閱讀:138 作者:小新 欄目:云計算

這篇文章主要為大家展示了“HBase中SHELL操作和API的用法示例”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學習一下“HBase中SHELL操作和API的用法示例”這篇文章吧。

1、表結(jié)構(gòu):

HBase中SHELL操作和API的用法示例

2、SHELL操作

    命令:hbase shell

  • 顯示表:list

  • 創(chuàng)建表:create 'tb_name','column_family_1','column_family_2',...;

    或者 create 'user', {NAME => 'column_family_1', VERSIONS => '3'}

  •  插入數(shù)據(jù):put 'tb_name','rk_on','column_family : key','value'

  • 獲取數(shù)據(jù):

                獲取所有數(shù)據(jù):get 'tb_name','rk_on'

                獲取列族數(shù)據(jù):get 'tb_name','rk_on','column_family'

                            或者 get 'tb_name', 'rk_on', {column=> ['cf_name1', 'cf_name2']}

                獲取列族中列數(shù)據(jù):get 'tb_name','rk_on','column_family:key','column_family:column2',..

                            或者 get 'tb_name', 'rk_on', {COLUMN => ['cf_name:key', 'cf_name:key']}

                獲取各個版本的數(shù)據(jù)

                        列族版本:get 'tb_name', 'rk_on', {COLUMN => 'cf_name1', VERSIONS => Number}

                        列版本:get 'tb_name', 'rk_on', {COLUMN => 'cf_name1:c_name', VERSIONS => Number}

                        時間范圍內(nèi)的列版本:get 'user', 'rk0001', {COLUMN => 'cfn:key', VERSIONS => Number, TIMERANGE => [1392368783980, 1392380169184]}       

                        VERSION:查詢版本數(shù)量

                        TIMERANGE:時間戳范圍

  • 完全匹配:get 'tb_name', 'rk_on', {FILTER => "ValueFilter(=, 'binary:value')"}

                rk_on中含有value的信息

  • 匹配:get 'tb_name', 'rk_on', {FILTER => "(ValueFilter(=,'substring:a'))"}

                列示符中含有a的信息

  • scan查詢:

        scan 'tb_name'

        查詢user表中列族為cfn的信息
        scan 'tb_name', {COLUMNS => 'cfn'}
        scan 'tb_name', {COLUMNS => 'cfn', RAW => true, VERSIONS => 5}
        scan 'tb_name', {COLUMNS => 'cfn', RAW => true, VERSIONS => 3}
        查詢user表中列族為cfn1和cfn2的信息
        scan 'user', {COLUMNS => ['cfn1', 'cfn2']}
        scan 'user', {COLUMNS => ['cfn1:key1', 'cfn2:key2']}

        查詢user表中列族為info、列標示符為name的信息
        scan 'user', {COLUMNS => 'info:name'}

        查詢user表中列族為info、列標示符為name的信息,并且版本最新的5個
        scan 'user', {COLUMNS => 'info:name', VERSIONS => 5}

        查詢user表中列族為info和data且列標示符中含有a字符的信息
        scan 'user', {COLUMNS => ['info', 'data'], FILTER => "(QualifierFilter(=,'substring:a'))"}

        查詢user表中列族為info,rk范圍是[rk0001, rk0003)的數(shù)據(jù)
        scan 'people', {COLUMNS => 'info', STARTROW => 'rk0001', ENDROW => 'rk0003'}

        查詢user表中row key以rk字符開頭的
        scan 'user',{FILTER=>"PrefixFilter('rk')"}

        查詢user表中指定范圍的數(shù)據(jù)
        scan 'user', {TIMERANGE => [1392368783980, 1392380169184]}
        

  • 刪除數(shù)據(jù)

        刪除user表row key為rk0001,列標示符為info:name的數(shù)據(jù)
        delete 'people', 'rk0001', 'info:name'
        刪除user表row key為rk0001,列標示符為info:name,timestamp為1392383705316的數(shù)據(jù)
        delete 'user', 'rk0001', 'info:name', 1392383705316

  • 清空user表中的數(shù)據(jù)
    truncate 'people'

  • 修改表結(jié)構(gòu)
    首先停用user表(新版本不用)
    disable 'user'

  • 添加兩個列族f1和f2
    alter 'people', NAME => 'f1'
    alter 'user', NAME => 'f2'

  • 啟用表
    enable 'user'

  • ###disable 'user'(新版本不用)
    刪除一個列族:
    alter 'user', NAME => 'f1', METHOD => 'delete' 或 alter 'user', 'delete' => 'f1'

  • 添加列族f1同時刪除列族f2
    alter 'user', {NAME => 'f1'}, {NAME => 'f2', METHOD => 'delete'}

  • 將user表的f1列族版本號改為5
    alter 'people', NAME => 'info', VERSIONS => 5

  • 啟用表
    enable 'user'

  • 刪除表
    disable 'user'
    drop 'user'


2、API:

幾個相關(guān)類與HBase數(shù)據(jù)模型之間的對應(yīng)關(guān)系

java類HBase數(shù)據(jù)模型
HBaseAdmin數(shù)據(jù)庫(DataBase)
HBaseConfiguration
HTable表(Table)
HTableDescriptor列族(Column Family)
Put列修飾符(Column Qualifier)
Get
Scanner

一、HBaseConfiguration

關(guān)系:org.apache.hadoop.hbase.HBaseConfiguration

作用:對HBase進行配置

 返回值 函數(shù) 描述
 void addResource(Path file) 通過給定的路徑所指的文件來添加資源
 void clear() 清空所有已設(shè)置的屬性
 string get(String name) 獲取屬性名對應(yīng)的值
 String getBoolean(String name, boolean defaultValue) 獲取為boolean類型的屬性值,如果其屬性值類型部位boolean,則返回默認屬性值
 void set(String name, String value) 通過屬性名來設(shè)置值
 void setBoolean(String name, boolean value) 設(shè)置boolean類型的屬性值

 用法示例:

HBaseConfiguration hconfig = new HBaseConfiguration();
hconfig.set("hbase.zookeeper.property.clientPort","2181");

  該方法設(shè)置了"hbase.zookeeper.property.clientPort"的端口號為2181。一般情況下,HBaseConfiguration會使用構(gòu)造函數(shù)進行初始化,然后在使用其他方法。

二、HBaseAdmin

關(guān)系:org.apache.hadoop.hbase.client.HBaseAdmin

作用:提供了一個接口來管理HBase數(shù)據(jù)庫的表信息。它提供的方法包括:創(chuàng)建表,刪除表,列出表項,使表有效或無效,以及添加或刪除表列族成員等。

 返回值 函數(shù) 描述
      void addColumn(String tableName, HColumnDescriptor column) 向一個已經(jīng)存在的表添加咧
 checkHBaseAvailable(HBaseConfiguration conf) 靜態(tài)函數(shù),查看HBase是否處于運行狀態(tài)
 createTable(HTableDescriptor desc) 創(chuàng)建一個表,同步操作
 deleteTable(byte[] tableName) 刪除一個已經(jīng)存在的表
 enableTable(byte[] tableName) 使表處于有效狀態(tài)
 disableTable(byte[] tableName) 使表處于無效狀態(tài)
 HTableDescriptor[] listTables() 列出所有用戶控件表項
 void modifyTable(byte[] tableName, HTableDescriptor htd) 修改表的模式,是異步的操作,可能需要花費一定的時間
 boolean tableExists(String tableName) 檢查表是否存在

用法示例:

HBaseAdmin admin = new HBaseAdmin(config);
admin.disableTable("tablename")

三、HTableDescriptor

關(guān)系:org.apache.hadoop.hbase.HTableDescriptor

作用:包含了表的名字極其對應(yīng)表的列族

返回值函數(shù)描述
voidaddFamily(HColumnDescriptor)添加一個列族
HColumnDescriptorremoveFamily(byte[] column)移除一個列族
byte[]getName()獲取表的名字
byte[]getValue(byte[] key)獲取屬性的值
voidsetValue(String key, String value)設(shè)置屬性的值

用法示例:

HTableDescriptor htd = new HTableDescriptor(table);
htd.addFamily(new HcolumnDescriptor("family"));

在上述例子中,通過一個HColumnDescriptor實例,為HTableDescriptor添加了一個列族:family

四、HColumnDescriptor

關(guān)系:org.apache.hadoop.hbase.HColumnDescriptor

作用:維護著關(guān)于列族的信息,例如版本號,壓縮設(shè)置等。它通常在創(chuàng)建表或者為表添加列族的時候使用。列族被創(chuàng)建后不能直接修改,只能通過刪除然后重新創(chuàng)建的方式。列族被刪除的時候,列族里面的數(shù)據(jù)也會同時被刪除。

返回值函數(shù)描述
byte[]getName()獲取列族的名字
byte[]getValue(byte[] key)獲取對應(yīng)的屬性的值
voidsetValue(String key, String value)設(shè)置對應(yīng)屬性的值

用法示例:

HTableDescriptor htd = new HTableDescriptor(tablename);
HColumnDescriptor col = new HColumnDescriptor("content:");
htd.addFamily(col);

此例添加了一個content的列族

五、HTable

關(guān)系:org.apache.hadoop.hbase.client.HTable

作用:可以用來和HBase表直接通信。此方法對于更新操作來說是非線程安全的。

返回值函數(shù)描述
voidcheckAdnPut(byte[] row, byte[] family, byte[] qualifier, byte[] value, Put put自動的檢查row/family/qualifier是否與給定的值匹配
voidclose()釋放所有的資源或掛起內(nèi)部緩沖區(qū)中的更新
Booleanexists(Get get)檢查Get實例所指定的值是否存在于HTable的列中
Resultget(Get get)獲取指定行的某些單元格所對應(yīng)的值
byte[][]getEndKeys()獲取當前一打開的表每個區(qū)域的結(jié)束鍵值
ResultScannergetScanner(byte[] family)獲取當前給定列族的scanner實例
HTableDescriptorgetTableDescriptor()獲取當前表的HTableDescriptor實例
byte[]getTableName()獲取表名
static booleanisTableEnabled(HBaseConfiguration conf, String tableName)檢查表是否有效
voidput(Put put)向表中添加值

用法示例:

HTable table = new HTable(conf, Bytes.toBytes(tablename));
ResultScanner scanner =  table.getScanner(family);

六、Put

關(guān)系:org.apache.hadoop.hbase.client.Put

作用:用來對單個行執(zhí)行添加操作

返回值函數(shù)描述
Putadd(byte[] family, byte[] qualifier, byte[] value)將指定的列和對應(yīng)的值添加到Put實例中
Putadd(byte[] family, byte[] qualifier, long ts, byte[] value)將指定的列和對應(yīng)的值及時間戳添加到Put實例中
byte[]getRow()獲取Put實例的行
RowLockgetRowLock()獲取Put實例的行鎖
longgetTimeStamp()獲取Put實例的時間戳
booleanisEmpty()檢查familyMap是否為空
PutsetTimeStamp(long timeStamp)設(shè)置Put實例的時間戳

用法示例:

HTable table = new HTable(conf,Bytes.toBytes(tablename));
Put p = new Put(brow);//為指定行創(chuàng)建一個Put操作p.add(family,qualifier,value);
table.put(p);

七、Get

關(guān)系:org.apache.hadoop.hbase.client.Get

作用:用來獲取單個行的相關(guān)信息

返回值函數(shù)描述
GetaddColumn(byte[] family, byte[] qualifier)獲取指定列族和列修飾符對應(yīng)的列
GetaddFamily(byte[] family)通過指定的列族獲取其對應(yīng)列的所有列
GetsetTimeRange(long minStamp,long maxStamp)獲取指定取件的列的版本號
GetsetFilter(Filter filter)當執(zhí)行Get操作時設(shè)置服務(wù)器端的過濾器

用法示例:

HTable table = new HTable(conf, Bytes.toBytes(tablename));
Get g = new Get(Bytes.toBytes(row));

八、Result

關(guān)系:org.apache.hadoop.hbase.client.Result

作用:存儲Get或者Scan操作后獲取表的單行值。使用此類提供的方法可以直接獲取值或者各種Map結(jié)構(gòu)(key-value對)

返回值函數(shù)描述
booleancontainsColumn(byte[] family, byte[] qualifier)檢查指定的列是否存在
NavigableMap<byte[],byte[]>getFamilyMap(byte[] family)獲取對應(yīng)列族所包含的修飾符與值的鍵值對
byte[]getValue(byte[] family, byte[] qualifier)獲取對應(yīng)列的最新值

九、ResultScanner

關(guān)系:Interface

作用:客戶端獲取值的接口

返回值函數(shù)描述
voidclose()關(guān)閉scanner并釋放分配給它的資源
Resultnext()獲取下一行的值

相關(guān)代碼:

package cn.itcast.hbase;

import java.io.IOException;

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 org.apache.hadoop.hbase.client.Delete;
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.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;

public class HbaseDemo {

    private Configuration conf = null;
    
    @Before
    public void init(){
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
    }
    
    @Test
    public void testDrop() throws Exception{
        HBaseAdmin admin = new HBaseAdmin(conf);
        admin.disableTable("account");
        admin.deleteTable("account");
        admin.close();
    }
    
    @Test
    public void testPut() throws Exception{
        HTable table = new HTable(conf, "user");
        Put put = new Put(Bytes.toBytes("rk0003"));
        put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("liuyan"));
        table.put(put);
        table.close();
    }
    
    @Test
    public void testGet() throws Exception{
        //HTablePool pool = new HTablePool(conf, 10);
        //HTable table = (HTable) pool.getTable("user");
        HTable table = new HTable(conf, "user");
        Get get = new Get(Bytes.toBytes("rk0001"));
        //get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
        get.setMaxVersions(5);
        Result result = table.get(get);
        //result.getValue(family, qualifier)
        for(KeyValue kv : result.list()){
            String family = new String(kv.getFamily());
            System.out.println(family);
            String qualifier = new String(kv.getQualifier());
            System.out.println(qualifier);
            System.out.println(new String(kv.getValue()));
        }
        table.close();
    }
    
    @Test
    public void testScan() throws Exception{
        HTablePool pool = new HTablePool(conf, 10);
        HTableInterface table = pool.getTable("user");
        Scan scan = new Scan(Bytes.toBytes("rk0001"), Bytes.toBytes("rk0002"));
        scan.addFamily(Bytes.toBytes("info"));
        ResultScanner scanner = table.getScanner(scan);
        for(Result r : scanner){
            /**
            for(KeyValue kv : r.list()){
                String family = new String(kv.getFamily());
                System.out.println(family);
                String qualifier = new String(kv.getQualifier());
                System.out.println(qualifier);
                System.out.println(new String(kv.getValue()));
            }
            */
            byte[] value = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
            System.out.println(new String(value));
        }
        pool.close();
    }
    
    
    @Test
    public void testDel() throws Exception{
        HTable table = new HTable(conf, "user");
        Delete del = new Delete(Bytes.toBytes("rk0001"));
        del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("pic"));
        table.delete(del);
        table.close();
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
        HBaseAdmin admin = new HBaseAdmin(conf);
        HTableDescriptor td = new HTableDescriptor("account");
        HColumnDescriptor cd = new HColumnDescriptor("info");
        cd.setMaxVersions(10);
        td.addFamily(cd);
        admin.createTable(td);
        admin.close();

    }
    
    public void createTable(String tableName, int maxVersion, String... cf){
        
    }

}

以上是“HBase中SHELL操作和API的用法示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI