HBase common Shell and Java API operations

1. Common shell

namecommand
Create tablecreate 'table name', 'column cluster name 1', 'column cluster name 2'
get dataget 'table name', 'row key', 'column cluster name: column name'
Add dataput 'table name', 'row key', 'column cluster name: column name', 'value'
View all tableslist
View table structuredesc 'table name'
Delete columndelete 'table name', 'row key', 'column cluster name: column name'
Delete a row of datadelete 'table name', 'row key'
Delete tabledrop 'table name' disable the table before deleting the table
Clear table datatruncate 'table name'
Query all datascan 'table name'
Query the first 2 datascan 'table name', {limit = > 2}
Range queryscan 'table name', {startRow = > 'start row key name', endRow = > 'end row key name'}
Statistical table recordcount 'table name', {interval = > how many rows are displayed once, cache = > size taken each time}
Add column clusteralter 'table name', {name = > 'column cluster name'}
Delete column clusteralter 'table name', 'delete' = > 'column cluster name'
Disable tabledisable 'table name'
Enable tableenable 'table name'
Judge whether the table existsexists' table name '

2. Java API operations

2.1 HBaseAdmin

Class: org.apache.hadoop.hbase.client.HBaseAdmin
Function: it provides an interface to manage the table information of HBase database. The methods it provides include: creating a table, deleting a table, listing table items, making the table valid or invalid, and adding or deleting table column family members.

2.2. HBaseConfiguration class

Class: org.apache.hadoop.hbase.HBaseConfiguration
Function: configure HBase

2.3 HTableDescriptor class

Class: org.apache.hadoop.hbase.HTableDescriptor
Function: contains the name of the table and its corresponding column family

2.4 HColumnDescriptor class

Class: org.apache.hadoop.hbase.HColumnDescriptor
Function: maintains information about column families, such as version number, compression settings, etc. It is usually used when creating tables or adding column families to tables. After the column family is created, it cannot be modified directly. It can only be deleted and recreated. When the column family is deleted, the data in the column family will be deleted at the same time.

2.5 HTable class

Class: org.apache.hadoop.hbase.client.Htable
Function: it can be used to communicate directly with HBase table. This method is non thread safe for update operations.

2.6 Put type

Class: org.apache.hadoop.hbase.client.Put
Function: used to add a single row

2.7. Get class

Class: org.apache.hadoop.hbase.client.Get
Function: used to obtain the relevant information of a single row

2.8. Result class

Class: org.apache.hadoop.hbase.client.Result
Function: store the single row value of the table after Get or Scan operation. Using the methods provided by this class, you can directly obtain values or various Map structures (key value pairs)

3. Java API operation code

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Ding
 * @Date: 2021/11/30/21:00
 * @Description: ctrl + alt + f  Promote local variables to global variables
 */
public class APIoperation {

    private Connection conn;
    private Admin admin;

    @Before
    /**
     * Initialize connection
     */
    public void init() throws IOException {
        // Create a configuration
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");
        conn = ConnectionFactory.createConnection(conf);
        admin = conn.getAdmin();
    }


    @Test
    /**
     * Create a table
     */
    public void createTable() throws IOException {
        TableName testAPI = TableName.valueOf("testAPI");
        boolean exists = admin.tableExists(testAPI);
        if (exists) {
            System.out.println(Bytes.toString(testAPI.getName()) + ": Failed to create the table. The table already exists");
            return;
        }

        HTableDescriptor tableDesc = new HTableDescriptor(testAPI);

        HColumnDescriptor info = new HColumnDescriptor("info");
        info.setMaxVersions(3);
        tableDesc.addFamily(info);

        admin.createTable(tableDesc);
        System.out.println(Bytes.toString(testAPI.getName()) + ": Table created successfully!");
    }

    @Test
    /**
     * Delete a table
     */
    public void dropTable() throws IOException {
        TableName testAPI = TableName.valueOf("testAPI");
        boolean exists = admin.tableExists(testAPI);
        if (exists) {
            admin.disableTable(testAPI);
            admin.deleteTable(testAPI);
            System.out.println(Bytes.toString(testAPI.getName()) + "Table deleted successfully!");
            return;
        }
        System.out.println(Bytes.toString(testAPI.getName()) + "The table does not exist");
    }

    @Test
    /**
     * Display all the tables in hbase
     */
    public void list() throws IOException {
        TableName[] tableNames = admin.listTableNames();
        for (TableName tableName : tableNames) {
            System.out.println(tableName);
        }
    }

    @Test
    /**
     * put data into a table
     */
    public void put() throws IOException {
        TableName testAPI = TableName.valueOf("testAPI");
        if (admin.tableExists(testAPI)) {
            Table table = conn.getTable(testAPI);
            Put put = new Put("cf1".getBytes());
            put.addColumn("cf1".getBytes(), "name".getBytes(), "zs".getBytes());

            table.put(put);
        }
    }

    @Test
    /**
     * Get table data
     */
    public void get() throws IOException {
        TableName testAPI = TableName.valueOf("testAPI");
        boolean b = admin.tableExists(testAPI);
        if (b) {
            Table table = conn.getTable(testAPI);
            Get get = new Get("cf1".getBytes());
            Result result = table.get(get);
            byte[] value = result.getValue("cf1".getBytes(), "name".getBytes());

            System.out.println(new String(value));
        }
    }

    @Test
    /**
     * Modify the table structure and add a column cluster
     */
    public void modifyTable() throws IOException {
        //Judge whether the table exists
        TableName testAPI = TableName.valueOf("testAPI");
        boolean exists = admin.tableExists(testAPI);
        if (!exists) {
            System.out.println(Bytes.toString(testAPI.getName()) + "The table does not exist and cannot be modified!");
            return;
        }
        //Gets the descriptor of the original table
        HTableDescriptor tableDesc = admin.getTableDescriptor(testAPI);
        // Add a column cluster
        HColumnDescriptor info2 = new HColumnDescriptor("info2");
        info2.setMaxVersions(3);
        tableDesc.addFamily(info2);
        admin.modifyTable(testAPI, tableDesc);
        System.out.println(Bytes.toString(testAPI.getName()) + "The table has been modified successfullyヾ(≧▽≦*)o");
    }

    @Test
    /**
     * Modify the table structure and delete a column cluster
     */
    public void deleteFamily() throws IOException {
        TableName testAPI = TableName.valueOf("testAPI");
        boolean exists = admin.tableExists(testAPI);
        if (!exists) {
            System.out.println("Sorry"+Bytes.toString(testAPI.getName())+"Table does not exist");
            return;
        }
        HTableDescriptor tableDesc = admin.getTableDescriptor(testAPI);
        boolean isFamily = tableDesc.hasFamily("info2".getBytes());
        if (!isFamily) {
            System.out.println("Sorry"+Bytes.toString(testAPI.getName())+"Not in table"+tableDesc.getNameAsString()+"Column cluster(︶^︶)");
            return;
        }
        tableDesc.removeFamily("info2".getBytes());
        admin.modifyTable(testAPI,tableDesc);
        System.out.println("Columns have been clustered info2 Deleted!");
    }

    @After
    /**
     * close resource
     */
    public void close() throws IOException {
        admin.close();
        conn.close();
    }
}

Tags: Database Big Data HBase nosql

Posted on Wed, 01 Dec 2021 04:31:53 -0500 by modcar