ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Hbase 相关

2022-06-09 11:01:40  阅读:104  来源: 互联网

标签:void Bytes next cell new 相关 Hbase table


HBase Java Api 基础操作

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

import java.io.IOException;
import java.util.List;

/**
* http://hbase.org.cn/docs/32.html  官网地址
 * */
/
public class TestHbase {

    //创建变量 方便方法使用
    Connection conn=null;

    /**
     * @author: zcb
     * @description: TODO
     * @date: 2022-06-09 10:35
     * @param
     * @return void
     * 用于连接hbase
     */
    @Before
    public void init() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","hdp1,hdp2,hdp3");
       conn = ConnectionFactory.createConnection(conf);
    }
    /**
     * @author: zcb
     * @description: TODO
     * @date: 2022-06-09 10:35
     * @param
     * @return void
     * 创建表空间,名称为ns0507
     */
    @Test
    public void createNS() throws IOException {
        //获取空间操作权限
        Admin admin = conn.getAdmin();
        admin.createNamespace(NamespaceDescriptor.create("ns0507").build());
        admin.close();
    }
    /**
     * @author: zcb
     * @description: TODO
     * @date: 2022-06-09 10:36
     * @param
     * @return void
     * 创建Hbase表,表名列族自定义
     */
    @Test
    public void createTable() throws IOException {
        Admin admin = conn.getAdmin();
        HTableDescriptor homework0507 = new HTableDescriptor(TableName.valueOf("ns0507:homework0507"));
        //列族名称  可以多个列族
        HColumnDescriptor info = new HColumnDescriptor("info");
        homework0507.addFamily(info);
        admin.createTable(homework0507);
        admin.close();
    }

    /**
     * @author: zcb
     * @description: TODO
     * @date: 2022-06-09 10:37
     * @param
     * @return void
     * 查看指定表中的数据
     */
    @Test
    public void scan() throws IOException {
        //获取表的操作权限
        Table table = conn.getTable(TableName.valueOf("ns0507:homework0507"));
        ResultScanner scanner = table.getScanner(new Scan());
        Result next = scanner.next();
        while(next!=null){
        List<Cell> cells = next.listCells();
        for (Cell cell : cells) {
            byte[] row = CellUtil.cloneRow(cell);
            byte[] family = CellUtil.cloneFamily(cell);
            byte[] qualifier = CellUtil.cloneQualifier(cell);
            byte[] value = CellUtil.cloneValue(cell);
            System.out.println(Bytes.toString(row)+","+Bytes.toString(family)+","+Bytes.toString(qualifier)+","+Bytes.toInt(value));
        }
        next=scanner.next();
        table.close();
        }
    }
    //打印方法  简便查询方法  美观简洁
    public void prints(Scan scan) throws IOException {
        Table table = conn.getTable(TableName.valueOf("ns0507:homework0507"));
        ResultScanner scanner = table.getScanner(scan);
        Result next = scanner.next();
        while(next!=null){
            List<Cell> cells = next.listCells();
            for (Cell cell : cells) {
                byte[] row = CellUtil.cloneRow(cell);
                byte[] family = CellUtil.cloneFamily(cell);
                byte[] qualifier = CellUtil.cloneQualifier(cell);
                byte[] value = CellUtil.cloneValue(cell);
                System.out.println(Bytes.toString(row)+","+Bytes.toString(family)+","+Bytes.toString(qualifier)+","+Bytes.toInt(value));
            }
            next=scanner.next();

        }
        table.close();
    }

    /**
     * @author: zcb
     * @description: TODO
     * @date: 2022-06-09 10:38
     * @param
     * @return void
     * 查询行键为user10的数据
     */
    @Test
    public void get1() throws IOException {
        Table table = conn.getTable(TableName.valueOf("ns0507:homework0507"));
        Scan scan = new Scan();
        //创建行键实体类  进行行键查询
        RowFilter user10 = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(Bytes.toBytes("user10")));
        scan.setFilter(user10);
        ResultScanner scanner = table.getScanner(scan);
        Result next = scanner.next();
        while(next!=null){
            List<Cell> cells = next.listCells();
            for (Cell cell : cells) {
                byte[] row = CellUtil.cloneRow(cell);
                byte[] family = CellUtil.cloneFamily(cell);
                byte[] qualifier = CellUtil.cloneQualifier(cell);
                byte[] value = CellUtil.cloneValue(cell);
                System.out.println(Bytes.toString(row)+","+Bytes.toString(family)+","+Bytes.toString(qualifier)+","+Bytes.toInt(value));
            }
            next=scanner.next();

        }
        table.close();
    }

    /**
     * @author: zcb
     * @description: TODO
     * @date: 2022-06-09 10:40
     * @param
     * @return void
     * 查询姓名包含“小”的数据  条件查询列族为name的数据中包含小的
     */
    @Test
    public void get2() throws IOException {

        Table table = conn.getTable(TableName.valueOf("ns0507:homework0507"));
        Scan scan = new Scan();
        scan.setFilter(new SingleColumnValueFilter(Bytes.toBytes("info"),Bytes.toBytes("name"), CompareFilter.CompareOp.EQUAL,new SubstringComparator("小")));
        ResultScanner scanner = table.getScanner(scan);
        Result next = scanner.next();
        while(next!=null){
            List<Cell> cells = next.listCells();
            for (Cell cell : cells) {
                byte[] row = CellUtil.cloneRow(cell);
                byte[] family = CellUtil.cloneFamily(cell);
                byte[] qualifier = CellUtil.cloneQualifier(cell);
                byte[] value = CellUtil.cloneValue(cell);
                System.out.println(Bytes.toString(row)+","+Bytes.toString(family)+","+Bytes.toString(qualifier)+","+Bytes.toInt(value));
            }
            next=scanner.next();

        }
        table.close();
    }

    /**
     * @author: zcb
     * @description: TODO
     * @date: 2022-06-09 10:42
     * @param
     * @return void
     * 查询年龄大于20的数据  条件查询列族 age中年龄大于20岁的
     */
   @Test
   public void get3() throws IOException {
       Table table = conn.getTable(TableName.valueOf("ns0507:homework0507"));
       Scan scan = new Scan();
       SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("age"), CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes(20)));
       scan.setFilter(singleColumnValueFilter);
       prints(scan);
   }

    /**
     * @author: zcb
     * @description: TODO
     * @date: 2022-06-09 10:43
     * @param
     * @return void
     * 查询年龄大于20且性别为女的数据  多条件查询列族 age大于20 并且 列族sex 为女的
     */
   @Test
    public void get4() throws IOException {
       Table table = conn.getTable(TableName.valueOf("ns0507:homework0507"));
       Scan scan = new Scan();
       SingleColumnValueFilter age = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("age"), CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes(20)));
       age.setFilterIfMissing(true);
       SingleColumnValueFilter sex = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("sex"), CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes("女")));
       sex.setFilterIfMissing(true);
       FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
       filterList.addFilter(age);
       filterList.addFilter(sex);
       scan.setFilter(filterList);
       prints(scan);
       table.close();
   }

    /**
     * @author: zcb
     * @description: TODO
     * @date: 2022-06-09 10:44
     * @param
     * @return void
     * 删除年龄大于30的数据  删除列族age大于30的
     */
    @Test
    public void get5() throws IOException {
        Table table = conn.getTable(TableName.valueOf("ns0507:homework0507"));
        Scan scan = new Scan();
        SingleColumnValueFilter age = new SingleColumnValueFilter(Bytes.toBytes("info"), Bytes.toBytes("age"), CompareFilter.CompareOp.GREATER, new BinaryComparator(Bytes.toBytes(30)));
        scan.setFilter(age);
        ResultScanner scanner = table.getScanner(scan);
        Result next = scanner.next();
        while(next!=null){
            List<Cell> cells = next.listCells();//获取list集合
            for (Cell cell : cells) {//遍历拿值
                byte[] row = CellUtil.cloneRow(cell);//获取行键
                table.delete(new Delete(row));//根据行键删除
            }
            next=scanner.next();

        }
        table.close();
    }
    //10、删除表所有数据
    /**
     * @author: zcb
     * @description: TODO
     * @date: 2022-06-09 10:44
     * @param
     * @return void
     * 删除表中的数据   删除方式不止一种
     */
    @Test
    public void deleteAll() throws IOException {
        Table table = conn.getTable(TableName.valueOf("ns0507:homework0507"));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        Result next = scanner.next();
        while(next!=null){
            List<Cell> cells = next.listCells();//获取list集合
            for (Cell cell : cells) {//遍历拿值
                byte[] row = CellUtil.cloneRow(cell);//获取行键
                table.delete(new Delete(row));//根据行键删除
            }
            next=scanner.next();
        }
        table.close();
    }

    /**
     * @author: zcb
     * @description: TODO
     * @date: 2022-06-09 10:47
     * @param
     * @return void
     * 删除表  需要注意  删除之前需要先进行禁用
     */
    @Test
    public void deleteTable() throws IOException {
        Admin admin = conn.getAdmin();
        admin.disableTable(TableName.valueOf("ns0507:homework0507"));
        admin.deleteTable(TableName.valueOf("ns0507:homework0507"));
        admin.close();
    }
    //删除表空间
    @Test
    public void DeleteNS() throws IOException {
        Admin admin = conn.getAdmin();
        admin.deleteNamespace("ns0507");
        admin.close();
    }
}

 

标签:void,Bytes,next,cell,new,相关,Hbase,table
来源: https://www.cnblogs.com/lenny-z/p/16358492.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有