Hbase Shell命令詳解+API操作


HBase Shell 操作


3.1 基本操作
1.進入 HBase 客戶端命令行,在hbase-2.1.3目錄下

 


bin/hbase shell


 


2.查看幫助命令

 


hbase(main):001:0> help


 

 



3.查看當前數據庫中有哪些表

 


hbase(main):002:0> list


 


3.2 表的操作


1.創建表

 


hbase(main):002:0> create 'student','info'


 

注意:刪除字符的按鍵BackSpace的刪除順序是反過來的,即從左往右刪。
2.插入數據到表

 


hbase(main):003:0> put 'student','1001','info:sex','male'
hbase(main):004:0> put 'student','1001','info:age','18'
hbase(main):005:0> put 'student','1002','info:name','Janna'
hbase(main):006:0> put 'student','1002','info:sex','female'
hbase(main):007:0> put 'student','1002','info:age','20'



3.掃描查看表數據

 


hbase(main):008:0> scan 'student'
hbase(main):009:0> scan 'student',{STARTROW => '1001', STOPROW => '1001'} 前閉后開
hbase(main):010:0> scan 'student',{STARTROW => '1001'}


 

4.查看表結構

 


hbase(main):0011:0> describe 'student'



5.更新指定字段的數據

 


hbase(main):012:0> put 'student','1001','info:name','Nick'
hbase(main):013:0> put 'student','1001','info:age','100'



6.查看【指定行】或【指定列族:列】的數據

 


hbase(main):014:0> get 'student','1001'
hbase(main):015:0> get 'student','1002','info:name'



7.統計表數據行數

 


hbase(main):021:0> count 'student'



8.刪除數據
刪除某 rowkey 的全部數據:

 


hbase(main):016:0> deleteall 'student','1001'



注意:刪除操作默認的時間戳是當前時間。在 HBase 中,增刪改數據都是打時間戳!!!
刪除某rowkey的某一列數據:

 


hbase(main):017:0> delete 'student','1002','info:sex'



注意:shell刪除操作會將數據的所有版本都刪除掉。但是在 HBase 的 API 操作中可以細粒度的控制刪除哪一個版本。

9.清空表數據

 


hbase(main):018:0> truncate 'student'



提示:清空表的操作順序為先 disable,然后再 truncate。

10.刪除表
首先需要先讓該表為 disable 狀態:

 


hbase(main):019:0> disable 'student'



然后才能 drop 這個表:

 


hbase(main):020:0> drop 'student'



提示:如果直接 drop 表,會報錯:ERROR: Table student is enabled. Disable it first.

11.變更表信息
設置將info列族中的數據存放3個版本:

 


hbase(main):022:0> alter 'student',{NAME => 'info', VERSIONS => 3}
hbase(main):022:0> get 'student','1001',{COLUMN => 'info:name', VERSIONS => 3}


 

 

Hbase API操作

1.環境准備:

創建maven項目,更改pom.xml文件配置

<dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.1.3</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>${hadoop.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
    </dependencies>

 

2.API

一系列API操作集合如下:

package com.gec.demo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;

import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
import org.apache.hadoop.hbase.util.Bytes;
import sun.java2d.pipe.OutlineTextRenderer;

import java.io.IOException;

import static com.gec.demo.utli.getTable.getHTable;

public class HbaseOperation {
    static Admin admin = null;

    static Connection conn = null;

    static Configuration conf = null;

    static {
        // HBase配置文件
        conf = HBaseConfiguration.create();

        // 設置zookeeper地址
        conf.set("hbase.zookeeper.quorum", "hadoop-001");
        conf.set("hbase.zookeeper.property.clientPort", "2181");

        try {
            // 獲取連接對象,執行
            conn = ConnectionFactory.createConnection(conf);
            admin = conn.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void close(Connection conn, Admin admin) {
        if (conn != null) {
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (admin != null) {
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static boolean isTableExistNewAPI(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {

        // HBase配置文件
        Configuration conf = HBaseConfiguration.create();

        // 設置zookeeper地址
        conf.set("hbase.zookeeper.quorum", "hadoop-001");
        conf.set("hbase.zookeeper.property.clientPort", "2181");

        // 獲取連接對象,執行
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        boolean tableExists = admin.tableExists(TableName.valueOf(tableName));

        // 關閉資源
        admin.close();

        return tableExists;
    }
    // 創建表
    public static void createTable(String tableName, String... columnFamily) throws IOException {

        if (isTableExistNewAPI(tableName)) {
            System.out.println("表" + tableName + "已存在!");
            return;
        }

        // 創建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));

        // 添加列族
        for (String cf : columnFamily) {
            // 創建列描述器
            HColumnDescriptor HColumnDescriptor = new HColumnDescriptor(cf);
            // 指定列族的版本個數,默認個數是一個
            // HColumnDescriptor.setMaxVersions(5);
            hTableDescriptor.addFamily(HColumnDescriptor);
        }

        // 創建表操作
        admin.createTable(hTableDescriptor);
        System.out.println("表" + tableName + "創建成功!");
    }
    // 刪除表
    public static void deleteTable(String tableName) throws IOException {

        if (isTableExistNewAPI(tableName)) {
            // 刪除表之前先使表不可用(下線)
            admin.disableTable(TableName.valueOf(tableName));
            // 執行刪除操作
            admin.deleteTable(TableName.valueOf(tableName));
            System.out.println("表" + tableName + "刪除成功!");
        } else {
            System.out.println("表" + tableName + "不存在!");
        }
    }

    // 向表中插入數據(或修改)
    public static void putRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {

        // 創建HTable對象
        // 舊API
        // HTable hTable = new HTable(conf, TableName.valueOf(tableName));
        // 獲取Table對象
        // 新API
        Table table = conn.getTable(TableName.valueOf(tableName));

        Put put = new Put(Bytes.toBytes(rowKey));
        // 向Put對象中組裝數據
        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));

        // 執行向表中插入數據的操作
        table.put(put);

        System.out.println("插入數據成功");

        table.close();

        // 批量插入數據提示:1、同一個RowKey下添加不同的列;2、不同的RowKey,可以將RowKey(Put)放到List集合。
    }

    // 刪除多行數據
    public static void deleteData(String tableName, String rowKey, String columnFamily, String column) throws IOException {

        // 獲取Table對象
        // 新API
        Table table = conn.getTable(TableName.valueOf(tableName));

        // 創建Delete對象
        Delete delete = new Delete(Bytes.toBytes(rowKey));

        // 向Delete對象中組裝數據,如果不組裝,則刪除的是行鍵的數據(多行數據)
        // delete.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column)); // 慎用這個方法,刪除某個版本(默認最新版本),保留舊的版本
        // delete.addColumns(Bytes.toBytes(columnFamily), Bytes.toBytes(column)); // 公司推薦使用這個方法,刪除所有版本

        // 執行刪除操作
        table.delete(delete);

        System.out.println("刪除多行數據成功");

        table.close();
    }
    // 獲取所有數據(全表掃描)
    public static void scanTable(String tableName) throws IOException {

        // 獲取Table對象
        // 新API
        Table table = conn.getTable(TableName.valueOf(tableName));

        // 構建掃描器,指定掃描的起始行和結束行,不指定的話,表示掃描全表,還可以指定其他限定
        Scan scan = new Scan();
        // scan.setStartRow(startRow);
        // scan.setStopRow(stopRow);

        // 執行掃描全表操作
        ResultScanner resultScanner = table.getScanner(scan);

        for (Result result : resultScanner) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println("行鍵:" + Bytes.toString(result.getRow())
                        + " 列族:" + Bytes.toString(CellUtil.cloneFamily(cell))
                        + " 列:" + Bytes.toString(CellUtil.cloneQualifier(cell))
                        + " 值:" + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }

        table.close();
    }
    // 獲取某一行數據
    public static void getRowData(String tableName, String rowKey) throws IOException {

        // 獲取Table對象
        // 新API
        Table table = conn.getTable(TableName.valueOf(tableName));

        // 新建一個Get對象
        Get get = new Get(Bytes.toBytes(rowKey));
        // 顯示所有版本
        // get.setMaxVersions();
        // 顯示指定版本
        // get.setMaxVersions(maxVersions);
        // 顯示指定時間戳的版本
        // get.setTimeStamp();

        // 執行獲取某一行數據的操作
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println("行鍵:" + Bytes.toString(result.getRow())
                    + " 列族:" + Bytes.toString(CellUtil.cloneFamily(cell))
                    + " 列:" + Bytes.toString(CellUtil.cloneQualifier(cell))
                    + " 值:" + Bytes.toString(CellUtil.cloneValue(cell))
                    + " 時間戳:" + cell.getTimestamp());
        }

        table.close();
    }
    // 獲取某一行指定“列族:列”的數據
    public static void getRowQualifierData(String tableName, String rowKey, String columnFamily, String column) throws IOException {

        // 獲取Table對象
        // 新API
        Table table = conn.getTable(TableName.valueOf(tableName));

        // 新建一個Get對象
        Get get = new Get(Bytes.toBytes(rowKey));
        // 指定要獲取某一行的“列族:列”
        get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));

        // 執行獲取某一行指定“列族:列”數據的操作
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println("行鍵:" + Bytes.toString(result.getRow())
                    + " 列族:" + Bytes.toString(CellUtil.cloneFamily(cell))
                    + " 列:" + Bytes.toString(CellUtil.cloneQualifier(cell))
                    + " 值:" + Bytes.toString(CellUtil.cloneValue(cell))
                    + " 時間戳:" + cell.getTimestamp());
        }

        table.close();
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM