Hbase常用JAVA API

<dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
            <version>2.1.5</version>
</dependency>
public class HbaseApi {
    private static Connection conn;
    private static Admin admin;
    
    public static void init(String quorum, String clientPort) throws IOException {
        Configuration config = HBaseConfiguration.create();
        System.getProperties().setProperty("HADOOP_USER_NAME", "hadoop");
        config.set("hbase.zookeeper.quorum", quorum);
        config.set("hbase.zookeeper.clientPort", clientPort);
        config.set("hbase.client.operation.timeout", "60000");
        config.set("hbase.rpc.timeout", "60000");
        conn = ConnectionFactory.createConnection(config);
        admin = conn.getAdmin();
    }


    //  ***********************  3.  获取一条数据   *******************************

    public static void getRow(String tableName, String rowKey) throws IOException {

        HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowKey));   //  通过rowkey创建一个 get 对象
        Result result = table.get(get);         //  输出结果
        for (Cell cell : result.rawCells()) {
            System.out.println(
                    "\u884c\u952e:" + new String(CellUtil.cloneRow(cell)) + "\t" +
                            "\u5217\u65cf:" + new String(CellUtil.cloneFamily(cell)) + "\t" +
                            "\u5217\u540d:" + new String(CellUtil.cloneQualifier(cell)) + "\t" +
                            "\u503c:" + new String(CellUtil.cloneValue(cell)) + "\t" +
                            "\u65f6\u95f4\u6233:" + cell.getTimestamp());
        }
        table.close();
        conn.close();
    }


    //**************************** 2.  添加一条数据  *******************************

    public static void addRow(String tableName, String rowKey, String columnFamily,
                              String column, String value) throws IOException {

        HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
        Put put = new Put(Bytes.toBytes(rowKey));  //   通过rowkey创建一个 put 对象
        //  在 put 对象中设置 列族、列、值
        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
        table.put(put);     //  插入数据,可通过 put(List<Put>) 批量插入
        table.close();
        conn.close();
    }


    //  ***************************  4. 全表扫描  ****************************

    public static void scanTable(String tableName) throws IOException {

        HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();  //  创建扫描对象
        ResultScanner results = table.getScanner(scan);   //  全表的输出结果
        for (Result result : results) {
            for (Cell cell : result.rawCells()) {
                System.out.println(
                        "\u884c\u952e:" + new String(CellUtil.cloneRow(cell)) + "\t" +
                                "\u5217\u65cf:" + new String(CellUtil.cloneFamily(cell)) + "\t" +
                                "\u5217\u540d:" + new String(CellUtil.cloneQualifier(cell)) + "\t" +
                                "\u503c:" + new String(CellUtil.cloneValue(cell)) + "\t" +
                                "\u65f6\u95f4\u6233:" + cell.getTimestamp());
            }
        }
        results.close();
        table.close();
        conn.close();
    }

    //**************************** 5. 删除一条数据  *****************************
    public static void delRow(String tableName, String rowKey) throws IOException {

        HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        table.delete(delete);
        table.close();
        conn.close();
    }

    //************************   6. 删除多条数据  *****************************
    public static void delRows(String tableName, String[] rows) throws IOException {

        HTable table = (HTable) conn.getTable(TableName.valueOf(tableName));
        List<Delete> list = new ArrayList<Delete>();
        for (String row : rows) {
            Delete delete = new Delete(Bytes.toBytes(row));
            list.add(delete);
        }
        table.delete(list);
        table.close();
        conn.close();
    }

    //获取列簇信息
    public static List<String> getColumnFamilyList(String tableName) throws Exception {


        Table table = conn.getTable(TableName.valueOf(tableName));
        List<String> columnFamilyList = new ArrayList<>();
        HTableDescriptor hTableDescriptor = table.getTableDescriptor();
        for (HColumnDescriptor descriptor : hTableDescriptor.getColumnFamilies()) {
            String name = descriptor.getNameAsString();

            columnFamilyList.add(descriptor.getNameAsString());

        }
        System.out.println("--------------" + tableName + ":ColumnFamilyInfo---------------");
        columnFamilyList.forEach(s -> System.out.println(s));

        return columnFamilyList;
    }

    //判断列簇是否存在
    public static boolean isExistColumnFamily(String tableName, String cf) throws IOException {
        Table table = conn.getTable(TableName.valueOf(tableName));
        TableDescriptor tableDescriptor = table.getDescriptor();
        ColumnFamilyDescriptor descriptor = tableDescriptor.getColumnFamily(Bytes.toBytes(cf));
        return descriptor == null ? false : true;
    }

    //动态新增列簇
    public static synchronized void insertColumnFamilyList(String tableName, List<String> columnFamilyList) throws Exception {
        if (columnFamilyList == null || columnFamilyList.size() <= 0) {
            return;
        }
        TableName table = TableName.valueOf(tableName);

        admin.disableTable(table);
        columnFamilyList.forEach(c -> {
            try {
                admin.addColumn(table, new HColumnDescriptor(c));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        admin.enableTable(table);
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值