码迷,mamicode.com
首页 > 编程语言 > 详细

Hbase2.0的Java API

时间:2021-03-15 11:32:26      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:循环   com   cell   操作   binary   yar   cells   hba   ant   

以表的操作和单条记录的增删改查为基础。

批量操作,需要进行进一步封装。

技术图片
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.security.UserGroupInformation;

import java.io.IOException;


public class HBaseUtils {
    public static void main(String[] args) {

    }
    // 判断表是否存在
    public boolean tableExists(String tableName) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","ambari-1,ambari-2");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        boolean test = admin.tableExists(TableName.valueOf("test"));
        admin.close();
        connection.close();
        System.out.println(test);
        return test;
    }
    //创建表,,表明,,和做个列族
    public void  creatTable(String tableName, String... cfs) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","ambari-1,ambari-2");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //指定用户名为hbase的用户去访问hbase服务
        UserGroupInformation userGroupInformation = UserGroupInformation.createRemoteUser("hbase");
        Connection connection = ConnectionFactory.createConnection(conf, User.create(userGroupInformation));
        // 不指定用户
        // Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        admin.disableTable(TableName.valueOf(tableName));
        admin.deleteTable(TableName.valueOf(tableName));
        admin.close();
        connection.close();
    }
    //删除表
    public void deleteTable(String tableName) throws IOException {
        if(this.tableExists(tableName)){
            return;
        }
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","ambari-1,ambari-2");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //指定用户名为hbase的用户去访问hbase服务
        UserGroupInformation userGroupInformation = UserGroupInformation.createRemoteUser("hbase");
        Connection connection = ConnectionFactory.createConnection(conf, User.create(userGroupInformation));
        // 不指定用户
        // Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        admin.disableTable(TableName.valueOf(tableName));
        admin.deleteTable(TableName.valueOf(tableName));
        admin.close();
        connection.close();
    }
    //增加数据,, 增加一行数据,修改同此操作
    public void putData(String tableName,String rowKey, String cf, String columName, String value) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","ambari-1,ambari-2");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //指定用户名为hbase的用户去访问hbase服务
        UserGroupInformation userGroupInformation = UserGroupInformation.createRemoteUser("hbase");
        Connection connection = ConnectionFactory.createConnection(conf,User.create(userGroupInformation));

        Table table=connection.getTable(TableName.valueOf(tableName));
        Put put=new Put(rowKey.getBytes());
        put.addColumn(cf.getBytes(), Bytes.toBytes(columName),value.getBytes());
        table.put(put);

        connection.close();
    }
    // 删除单条记录
    public void deleteRow(String tableName,String rowkey,String cf1, String column) throws IOException{
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","ambari-1,ambari-2");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //指定用户名为hbase的用户去访问hbase服务
        UserGroupInformation userGroupInformation = UserGroupInformation.createRemoteUser("hbase");
        Connection connection = ConnectionFactory.createConnection(conf,User.create(userGroupInformation));

        Table table=connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(rowkey.getBytes());
        // 可以作循环
        // 删除最近一个版本,上个版本依然有效存在仍会进行查询
        delete.addColumn(cf1.getBytes(),column.getBytes());
        // 删除所有版本
        delete.addColumns(cf1.getBytes(),column.getBytes());
        table.delete(delete);
        connection.close();
    }
    // 全表扫描
    public void scanTable(String tableName) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","ambari-1,ambari-2");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //指定用户名为hbase的用户去访问hbase服务
        UserGroupInformation userGroupInformation = UserGroupInformation.createRemoteUser("hbase");
        Connection connection = ConnectionFactory.createConnection(conf,User.create(userGroupInformation));

        Table table=connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            String rowKey = new String(result.getRow());
            System.out.println(rowKey);
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                // 方式一
                System.out.println("RowKey: "+Bytes.toString(CellUtil.cloneRow(cell))
                        +",CF: "+Bytes.toString(CellUtil.cloneFamily(cell))
                        +",CN: "+Bytes.toString(CellUtil.cloneQualifier(cell))
                        +",Value: "+Bytes.toString(CellUtil.cloneValue(cell)));
                //方式二
                String family=Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
                String qualifier=Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                String row=Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                String value1=Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                System.out.println(row+","+family+","+qualifier+","+value1+"\n");
            }
        }
        connection.close();
    }
    // 全表扫描,指定列族和列名
    public void scanTableWithCF(String tableName, String cf1, String column) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","ambari-1,ambari-2");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //指定用户名为hbase的用户去访问hbase服务
        UserGroupInformation userGroupInformation = UserGroupInformation.createRemoteUser("hbase");
        Connection connection = ConnectionFactory.createConnection(conf,User.create(userGroupInformation));

        Table table=connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        scan.addColumn(cf1.getBytes(),column.getBytes());
        scan.setFilter(new RowFilter(CompareOperator.EQUAL,new BinaryComparator("aa".getBytes())));
        ResultScanner scanner = table.getScanner(scan);

//        ResultScanner scanner = table.getScanner(cf1.getBytes(),column.getBytes());
        for (Result result : scanner) {
            String rowKey = new String(result.getRow());
            System.out.println(rowKey);
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                // 方式一
                System.out.println("RowKey: "+Bytes.toString(CellUtil.cloneRow(cell))
                        +",CF: "+Bytes.toString(CellUtil.cloneFamily(cell))
                        +",CN: "+Bytes.toString(CellUtil.cloneQualifier(cell))
                        +",Value: "+Bytes.toString(CellUtil.cloneValue(cell)));
                //方式二
                String family=Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
                String qualifier=Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                String row=Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                String value1=Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                System.out.println(row+","+family+","+qualifier+","+value1+"\n");
            }
        }
        connection.close();
    }
    // get方式
    public void getData(String tableName,String rowKey, String cf, String column) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","ambari-1,ambari-2");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        //指定用户名为hbase的用户去访问hbase服务
        UserGroupInformation userGroupInformation = UserGroupInformation.createRemoteUser("hbase");
        Connection connection = ConnectionFactory.createConnection(conf,User.create(userGroupInformation));

        Table table=connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(cf.getBytes(),column.getBytes());
        Result result = table.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println("RowKey: "+Bytes.toString(CellUtil.cloneRow(cell))
                    +",CF: "+Bytes.toString(CellUtil.cloneFamily(cell))
                    +",CN: "+Bytes.toString(CellUtil.cloneQualifier(cell))
                    +",Value: "+Bytes.toString(CellUtil.cloneValue(cell)));


            //方式二
            String family=Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
            String qualifier=Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
            String row=Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
            String value1=Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
            System.out.println(row+","+family+","+qualifier+","+value1+"\n");
        }

        connection.close();
    }

    //filter 参考:https://www.cnblogs.com/xiguage119/p/10605721.html
}
View Code

 

Hbase2.0的Java API

标签:循环   com   cell   操作   binary   yar   cells   hba   ant   

原文地址:https://www.cnblogs.com/wind-man/p/14530649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!