查看hbase表結構的命令,hbase常用命令及使用方法

首先通過docker安裝Hbase

docker search hbase docker pull harisekhon/hbase

啟動hbase鏡像

docker run -d -p 2181:2181 -p 8080:8080 -p 8085:8085 -p 9090:9090 -p 9095:9095 -p 16000:16000 -p 16010:16010 -p 16201:16201 -p 16301:16301 -p 16030:16030 -p 16020:16020 --name hbase001 harisekhon/hbase

訪問hbase

訪問localhost:16010
docker安裝Hbase以及命令行和api的crud操作

hbase命令行操作

docker exec -it hbase001 bash hbase shell

如上命令操作之後我們就進入了hbase的命令行操作模式:

下面,我們創建一個namespace為default,表名為test,列族column family為cf的表: create ‘test’,’cf’ 通過list命令可以查看所有的表:list

docker安裝Hbase以及命令行和api的crud操作
  • 刪除表:先disable,再drop
disable 'test' 
drop 'test'
  • 新增數據操作
put 'test','1','cf:name','flume'
put 'test','1','cf:age','18'
put 'test','2','cf:name','hbase'
put 'test','2','cf:age','20'
put 'test','3','cf:name','hadoop'
put 'test','3','cf:age','22'
put 'test','11','cf:name','spark'
put 'test','11','cf:age','24'
put 'test','21','cf:name','hive'
put 'test','21','cf:age','30'
  • 刪除數據操作
deleteall 'test','3';   -- 刪除該rowkey下的所有數據
delete 'test','3','cf:name' -- 刪除某一cell的數據
  • 查看數據
HBase中有兩個用於查看數據的命令:
1. scan 命令,用於查看某個表的全部數據;
2. get 命令,用於查看錶的某一行數據
  • 修改數據同樣也是put操作
比如將行鍵為 2015003 的學生 age 改為 25:put 'student','2015003','age','25'

如上是命令行的一些基本操作,詳細操作可以查看官網:

http://abloz.com/hbase/book.html

下面是通過java api的方式操作hbase

  • 添加maven依賴
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>2.1.3</version>
</dependency>

測試工具欄如下:

package com.example.redisiondemo.utils;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HbaseTest {
    private static Admin admin;

    private static final String COLUMNS_FAMILY_1 = "cf1";
    private static final String COLUMNS_FAMILY_2 = "cf2";

    public static Connection initHbase() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "127.0.0.1");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        configuration.set("hbase.master", "127.0.0.1:16010");
        Connection connection = ConnectionFactory.createConnection(configuration);
        return connection;
    }
//創建表 create
    public static void createTable(TableName tableName, String[] cols) throws IOException {
        admin = initHbase().getAdmin();
        if (admin.tableExists(tableName)) {
            System.out.println("Table Already Exists!");
        } else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for (String col : cols) {
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
            System.out.println("Table Create Successful");
        }
    }

    public static TableName getTbName(String tableName) {
        return TableName.valueOf(tableName);
    }
    // 刪除表 drop
    public static void deleteTable(TableName tableName) throws IOException {
        admin = initHbase().getAdmin();
        if (admin.tableExists(tableName)) {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
            System.out.println("Table Delete Successful");
        } else {
            System.out.println("Table does not exist!");
        }

    }
 //put 插入數據
    public static void insertData(TableName tableName, Student student) throws IOException {
        Put put = new Put(Bytes.toBytes(student.getId()));
        put.addColumn(Bytes.toBytes(COLUMNS_FAMILY_1), Bytes.toBytes("name"), Bytes.toBytes(student.getName()));
        put.addColumn(Bytes.toBytes(COLUMNS_FAMILY_1), Bytes.toBytes("age"), Bytes.toBytes(student.getAge()));
        initHbase().getTable(tableName).put(put);
        System.out.println("Data insert success:" + student.toString());
    }

    // delete 刪除數據
    public static void deleteData(TableName tableName, String rowKey) throws IOException {
        Delete delete = new Delete(Bytes.toBytes(rowKey));      // 指定rowKey
//        delete = delete.addColumn(Bytes.toBytes(COLUMNS_FAMILY_1), Bytes.toBytes("name"));  // 指定column,也可以不指定,刪除該rowKey的所有column
        initHbase().getTable(tableName).delete(delete);
        System.out.println("Delete Success");
    }

    // scan數據
    public static List<Student> allScan(TableName tableName) throws IOException {
        ResultScanner results = initHbase().getTable(tableName).getScanner(new Scan().addFamily(Bytes.toBytes("cf1")));
        List<String> list = new ArrayList<>();
        for (Result result : results) {
            Student student = new Student();
            for (Cell cell : result.rawCells()) {
                String colName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
            }

        }
        return null;
    }
    // 根據rowkey get數據
    public static Student singleGet(TableName tableName, String rowKey) throws IOException {
        Student student = new Student();
        student.setId(rowKey);
        Get get = new Get(Bytes.toBytes(rowKey));
        if (!get.isCheckExistenceOnly()) {
            Result result = initHbase().getTable(tableName).get(get);
            for (Cell cell : result.rawCells()) {
                String colName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                switch (colName) {
                    case "name":
                        student.setName(value);
                        break;
                    case "age":
                        student.setAge(value);
                        break;
                    default:
                        System.out.println("unknown columns");
                }

            }

        }
        System.out.println(student.toString());
        return student;
    }
    // 查詢指定Cell數據
    public static String getCell(TableName tableName, String rowKey, String cf, String column) throws IOException {
        Get get = new Get(Bytes.toBytes(rowKey));
        String rst = null;
        if (!get.isCheckExistenceOnly()) {
            get = get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(column));
            try {

                Result result = initHbase().getTable(tableName).get(get);
                byte[] resByte = result.getValue(Bytes.toBytes(cf), Bytes.toBytes(column));
                rst = Bytes.toString(resByte);
            } catch (Exception exception) {
                System.out.printf("columnFamily or column does not exists");
            }

        }
        System.out.println("Value is: " + rst);
        return rst;
    }

    public static void main(String[] args) throws IOException{
        Student student = new Student();
        student.setId("1");
        student.setName("hzp");
        student.setAge("18");
        String table = "student";

      //  createTable(getTbName(table), new String[]{COLUMNS_FAMILY_1, COLUMNS_FAMILY_2});
    //   deleteTable(getTbName(table));
          insertData(getTbName(table), student);
//       deleteData(getTbName(table), "1");
      //  singleGet(getTbName(table), "2");
    //    getCell(getTbName(table), "1", "cf1", "name");
    }
}
student實體
package com.example.redisiondemo.utils;

public class Student {
    private String id;
    private String name;
    private String age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + ''' +
                ", name='" + name + ''' +
                ", age='" + age + ''' +
                '}';
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/230064.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-10 13:17
下一篇 2024-12-10 13:17

相關推薦

發表回復

登錄後才能評論