HBase API(新版本)

以下API均为HBase API 3.0版本

API3.0与API2.0对比

Put、Delete、Scan类的API没有发生太多改变

HTableDescriptor类被TableDescriptorBuilder替代,HColumnDescriptor被ColumnFamilyDescriptor替代,CellUtil类被淘汰。

连接HBase

public static Configuration configuration;
public static Connection connection;
public static Admin admin;
    //建立连接
    public static void init(){
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.master", "master:60000");
        configuration.set("hbase.zookeeper.quorum", "master");
        configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

创建表

public static void createTable(String tableName,String[] fields) throws IOException {

        init();
        TableName tablename = TableName.valueOf(tableName);

        if(admin.tableExists(tablename)){
            System.out.println("table is exists!");
            admin.disableTable(tablename);
            admin.deleteTable(tablename);//删除原来的表
        }

        TableDescriptorBulider tableDescriptor = TableDescriptorBuilder.newBuilder(tablename);
        for(String str : fields){
            tableDescriptor.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build());
        admin.createTable(tableDescriptor.build());

添加数据Put

public static void putData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
        init();
        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        //创建Put对象
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
        table.put(put);
        table.close();
        close();
    }

删除操作Delete

public static void deleteData(String tableName,String rowKey,String colFamily,String col) throws IOException {
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        //删除指定列族
        delete.addFamily(Bytes.toBytes(colFamily));
        //删除指定列
        delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
        table.delete(delete);
        table.close();
        close();
    }

全表扫描

scan可以扫描多条记录。

public static void scanData(String tableName)throws  IOException{
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);//获取行的遍历器
        for (Result result:scanner){
            printRecoder(result);
        }
        close();
    }

Cell输出

通过Result.rawCells()可以得到Cell,一个RowKey有多个单元格,一个单元格一个Cell。

public  static void printRecoder(Result result)throws IOException{
        for(Cell cell:result.rawCells()){
            System.out.print("行健: "+new String(Bytes.toString(cell.getRowArray(),cell.getRowOffset(), cell.getRowLength())));
            System.out.print("列簇: "+new String( Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(), cell.getFamilyLength()) ));
            System.out.print(" 列: "+new String(Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(), cell.getQualifierLength())));
            System.out.print(" 值: "+new String(Bytes.toString(cell.getValueArray(),cell.getValueOffset(), cell.getValueLength())));
            System.out.println("时间戳: "+cell.getTimestamp());
            /*
            CellUtil在HBase2.0被弃用,在HBase3.0被移除,因此它的cloneRow,cloneFamily等都被淘汰
            System.out.print("行健: "+new String(CellUtil.cloneRow(cell)));
            System.out.print("列簇: "+new String(CellUtil.cloneFamily(cell)));
            System.out.print(" 列: "+new String(CellUtil.cloneQualifier(cell)));
            System.out.print(" 值: "+new String(CellUtil.cloneValue(cell)));
            System.out.println("时间戳: "+cell.getTimestamp());
            */
        }
    }

指定列族的信息

get获取一个单元格的多个版本信息

public static void getData(String tableName)throws  IOException{
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        //创建一个Get对象
        Get get = new Get(Bytes.toBytes(rowKey));
        //获取数据的操作
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell:cells){
            print(cell);
        }
        close();
    }

Admin接口

Admin接口用于管理HBase数据库的表信息,包括创建删除表,列出表等。
返回值 | 方法 | 介绍
-|-|-
void | createTable(TableDescriptor tabledescriptor) | 创建表
void | deleteTable(TableName tableName) | 删除表
void | disableTable(TableName tableName) | 使表无效
boolean | tableExists(TableName tableName) | 检查表是否存在
TableDescriptor | listTableDescriptors() | 列出所有表

TableDescriptorBuilder接口

该接口用于构建TableDescriptorBuilder类,主要方法如下:
返回值 | 方法 | 介绍
-|-|-
TableDescriptor | build() | 构建TableDescriptor
TableDescriptorBuilder | newBuilder(byte[] name) | 构建TableDescriptorBuilder
TableDescriptorBuilder | setColumnFamily(ColumnFamilyDescriptor family) | 设置某个列族
TableDescriptorBuilder | removeColumnFamily(byte[] name) | 删除某个列族

ColumnFamilyDescriptor接口

该接口用于构建ColumnFamilyDescriptor,其主要方法如下:
返回值 | 方法 | 介绍
-|-|-
ColumnFamilyDescriptor | build() | 构建ColumnFamilyDescriptor
ColumnFamilyDescriptorBuilder | newBuilder(byte[] name) | 构建ColumnFamilyDescriptorBuilder

HBase API(新版本)

上一篇:Struts2--API,结果页面


下一篇:【WPF学习】第五十八章 理解逻辑树和可视化树