Hbase操作与编程使用

1.任务:

  • 列出HBase所有的表的相关信息,例如表名

  • 在终端打印出指定的表的所有记录数据

  • 向已经创建好的表添加和删除指定的列族或列

  • 清空指定的表的所有记录数据

  • 统计表的行数

 

 

2.关系型数据库中的表和数据(教材P92上),要求将其转换为适合于HBase存储的表并插入数据。


 

 

3. 编程完成指定功能(教材P92下)

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 java.io.IOException;

public class hw {
    public static Configuration configuration; //管理HBase的配置信息
    public static Connection connection; //管理HBase连接
    public static Admin admin; //管理HBase数据库的表信息

    public static void main(String[] args) throws IOException{
        init();
        close();
    }

    public static void init(){
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void close(){
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();;
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    
    /** 1 创建表
     * @param tableName 表名
     * @param fields 列族数组
     * @throws IOException
     */
    public static void createTable(String tableName,String[] fields) throws IOException {
        TableName table_Name = TableName.valueOf(tableName);
        if(admin.tableExists(table_Name)){
            System.out.println("表已存在,准备删除原表并重建!");
            admin.disableTable(table_Name);
            admin.deleteTable(table_Name); //删除原来的表
        }else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for (String str:fields){
                HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
                hTableDescriptor.addFamily(hColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
            System.out.println("表创建成功!");
            close();
        }
    }

    /** 2 添加数据
     * 
     * @param tableName
     * @param row
     * @param fields
     * @param values
     * @throws IOException
     */
    public static void addRecord(String tableName,String row,String[] fields,String[] values) throws IOException{
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(row.getBytes());
        if(fields.length == 0)
            return;
        for (int i = 0; i < fields.length; i++){
            addColumn(put, fields[i], values[i]);
        }
        table.put(put);
        table.close();
    }

    private static void addColumn(Put put, String field, String value){
        String[] Col = field.split(":");
        String colFamily = Col[0];
        String col = Col[1];
        addColumn(put,colFamily,col,value);
    }

    private static void addColumn(Put put, String colFamily, String col, String value){
        put.addColumn(colFamily.getBytes(),col.getBytes(),value.getBytes());
    }

    /** 3 浏览数据
     *
     * @param tableName
     * @param column
     * @throws IOException
     */
    public static void scanColumn(String tableName, String column)throws IOException{
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        scan.addFamily(Bytes.toBytes(column));
        ResultScanner scanner = table.getScanner(scan);
        for (Result result = scanner.next(); result != null; result = scanner.next()){
            showCell(result);
        }
        table.close();
    }

    private static void showCell(Result result) {
        for (KeyValue kv : result.raw()) {
            System.out.println(Bytes.toString(kv.getRowArray()));
            System.out.println(Bytes.toString(kv.getFamilyArray()));
            System.out.println(Bytes.toString(kv.getQualifierArray()));
            System.out.println(Bytes.toString(kv.getValueArray()));
        }
    }

    /** 4 修改数据
     *
     * @param tableName
     * @param row
     * @param column
     * @param value
     * @throws IOException
     */
    public static void modifyData(String tableName, String row, String column, String value) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(row.getBytes());
        addColumn(put,column,value);
        table.put(put);
        table.close();
    }

    /** 5 删除指定行
     *
     * @param tableName
     * @param row
     * @throws IOException
     */
    public static void deleteRow(String tableName, String row) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(row.getBytes());
        table.delete(delete);
        table.close();
    }
}

 

posted @ 2020-11-22 16:35  Aolemon  阅读(182)  评论(0)    收藏  举报