Hbase的API案例实战

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hbase的API案例实战相关的知识,希望对你有一定的参考价值。

1、创建maven工程

技术图片

  • 自动导包(需要从cloudera仓库下载,耗时较长,耐心等待

技术图片
技术图片
如下内容作为maven工程中pom.xml的repositories的内容

    <repositories>
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.0-mr1-cdh5.14.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.0-cdh5.14.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.2.0-cdh5.14.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*/RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2、创建表

  • 创建myuser表,此表有两个列族f1和f2
    /**
     * 操作数据库  第一步:获取连接  第二步:获取客户端对象   第三步:操作数据库  第四步:关闭
     * 创建myuser表,有两个列族 f1 f2
     * @throws IOException
     */
    @Test
    public void createTable() throws IOException {
        //获得连接
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");

        //创建连接对象
        Connection connection = ConnectionFactory.createConnection(configuration);

        //操作:建表、删除表、修改表 -> 管理员;创建管理员对象
        Admin admin = connection.getAdmin();

        //添加了表名信息
        HTableDescriptor myuser = new HTableDescriptor(TableName.valueOf("myuser"));
        //给表添加列族
        myuser.addFamily(new HColumnDescriptor("f1"));
        myuser.addFamily(new HColumnDescriptor("f2"));

        //创建表
        admin.createTable(myuser);

        //关闭连接
        admin.close();
        connection.close();
    }

技术图片

3、添加数据

    private Connection connection;
    private Table table;

    //建立连接
    @BeforeTest
    public void init() throws IOException {
        //获得连接
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
        //configuration.set("zookeeper.znode.parent", "/HBase");
        connection = ConnectionFactory.createConnection(configuration);
        //获得表
        table = connection.getTable(TableName.valueOf("myuser"));
    }

    @AfterTest
    public void close() throws IOException {
        table.close();
        connection.close();
    }

    //向表中添加数据
    @Test
    public void putData() throws IOException {
        Put put = new Put("0001".getBytes());//创建Put对象,并指定rowkey值
        //添加cell值:列族名称+列名+值
        put.addColumn("f1".getBytes(), "name".getBytes(), "zhangsan".getBytes());
        put.addColumn("f1".getBytes(), "age".getBytes(), Bytes.toBytes(18));
        put.addColumn("f1".getBytes(),"id".getBytes(), Bytes.toBytes(25));
        put.addColumn("f1".getBytes(),"address".getBytes(), Bytes.toBytes("地球人"));

        table.put(put);
    }

4、查询数据

  • 初始化一批数据到HBase表当中,用于查询
    @Test
    public void batchInsert() throws IOException {
        //创建put对象,并指定rowkey
        Put put = new Put("0002".getBytes());
        //向f1列族添加数据
        put.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(1));
        put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("曹操"));
        put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(30));
        //向f2列族添加数据
        put.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("沛国谯县"));
        put.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("16888888888"));
        put.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("helloworld"));

        Put put2 = new Put("0003".getBytes());
        put2.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(2));
        put2.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("刘备"));
        put2.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(32));
        put2.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put2.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("幽州涿郡涿县"));
        put2.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("17888888888"));
        put2.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("talk is cheap , show me the code"));

        Put put3 = new Put("0004".getBytes());
        put3.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(3));
        put3.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("孙权"));
        put3.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(35));
        put3.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put3.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("下邳"));
        put3.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("12888888888"));
        put3.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("what are you 弄啥嘞!"));

        Put put4 = new Put("0005".getBytes());
        put4.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(4));
        put4.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("诸葛亮"));
        put4.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
        put4.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put4.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("四川隆中"));
        put4.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("14888888888"));
        put4.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("出师表你背了嘛"));

        Put put5 = new Put("0006".getBytes());
        put5.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
        put5.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("司马懿"));
        put5.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(27));
        put5.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put5.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("哪里人有待考究"));
        put5.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15888888888"));
        put5.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("跟诸葛亮死掐"));

        Put put6 = new Put("0007".getBytes());
        put6.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
        put6.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("xiaobubu—吕布"));
        put6.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
        put6.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put6.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("内蒙人"));
        put6.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15788888888"));
        put6.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("貂蝉去哪了"));

        List<Put> listPut = new ArrayList<Put>();
        listPut.add(put);
        listPut.add(put2);
        listPut.add(put3);
        listPut.add(put4);
        listPut.add(put5);
        listPut.add(put6);

        table.put(listPut);
    }

技术图片

4.1 Get查询

  • 按照rowkey进行查询,获取所有列的所有值
  • 查询主键rowkey为0003的人
    //查询rowkey为0003的数据
    @Test
    public void getDataByRowkey() throws IOException {
        //通过get对象,指定rowkey
        Get get = new Get("0003".getBytes());

        //获取某列族
        get.addFamily("f1".getBytes());//限定查询f1列族下面所有列的值
        get.addColumn("f2".getBytes(), "say".getBytes());//查询f2列族say列的值

        //通过get查询,返回0003行,f1列族、f2:say列的所有cell的值,封装到一个Result对象
        Result result = table.get(get);
        //获得Result中的所有Cell
        List<Cell> cells = result.listCells();

        //遍历Cell
        for(Cell cell: cells) {
            //cell单元格
            //获得rowkey
            byte[] rowkey_bytes = CellUtil.cloneRow(cell);
            //获得列族
            byte[] family_bytes = CellUtil.cloneFamily(cell);
            //获得列
            byte[] qualifier_bytes = CellUtil.cloneQualifier(cell);
            //获得值
            byte[] cell_bytes = CellUtil.cloneValue(cell);

            if("age".equals(Bytes.toString(qualifier_bytes)) || "id".equals(Bytes.toString(qualifier_bytes))) {
                System.out.println(Bytes.toString(rowkey_bytes));
                System.out.println(Bytes.toString(family_bytes));
                System.out.println(Bytes.toString(qualifier_bytes));
                System.out.println(Bytes.toInt(cell_bytes));//age或id的值是int类型,得用Bytes.toInt
            } else {
                System.out.println(Bytes.toString(rowkey_bytes));
                System.out.println(Bytes.toString(family_bytes));
                System.out.println(Bytes.toString(qualifier_bytes));
                System.out.println(Bytes.toString(cell_bytes));
            }
        }
    }

4.2 Scan查询

    /**
     * 不知道rowkey的具体值,我想查询rowkey范围值是0003  到0006
     * select * from myuser  where age > 30  and id < 8  and name like ‘zhangsan‘
     */
    @Test
    public void scanData() throws IOException {
        Scan scan = new Scan();//若没有指定startRow以及stopRow,则全表扫描
        //扫描f1列族
        scan.addFamily("f1".getBytes());
        //扫描 f2列族 phone列
        scan.addColumn("f2".getBytes(),"phone".getBytes());
        //设置起始结束rowkey,前闭后开
        scan.setStartRow("0003".getBytes());
        scan.setStopRow("0007".getBytes());

        //设置每批次返回客户端的数据条数
        scan.setBatch(20);
        //从cacheBlock中读取数据
        scan.setCacheBlocks(true);
        scan.setMaxResultSize(4);
        scan.setMaxVersions(2);//获取历史2个版本

        //通过getScanner查询获取到了表里面所有的数据,是多条数据
        ResultScanner scanner = table.getScanner(scan);
        //遍历ResultScanner 得到每一条数据,每一条数据都是封装在result对象里面了
        for (Result result : scanner) {
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                byte[] family_name = CellUtil.cloneFamily(cell);
                byte[] qualifier_name = CellUtil.cloneQualifier(cell);
                byte[] rowkey = CellUtil.cloneRow(cell);
                byte[] value = CellUtil.cloneValue(cell);
                //判断id和age字段,这两个字段是整形值
                if("age".equals(Bytes.toString(qualifier_name))  || "id".equals(Bytes.toString(qualifier_name))){
                    System.out.println("数据的rowkey为" +  Bytes.toString(rowkey)   +"======数据的列族为" +  Bytes.toString(family_name)+"======数据的列名为" +  Bytes.toString(qualifier_name) + "==========数据的值为" +Bytes.toInt(value));
                }else{
                    System.out.println("数据的rowkey为" +  Bytes.toString(rowkey)   +"======数据的列族为" +  Bytes.toString(family_name)+"======数据的列名为" +  Bytes.toString(qualifier_name) + "==========数据的值为" +Bytes.toString(value));
                }
            }
        }
    }

5、HBase的删除操作

5.1、根据rowkey删除数据

  • 删除rowkey为003的数据
    /**
     * 删除数据
     */
    @Test
    public  void  deleteData() throws IOException {
        Delete delete = new Delete("0003".getBytes());
        delete.addFamily("f1".getBytes());
        delete.addColumn("f2".getBytes(), "phone".getBytes());
        table.delete(delete);
    }

5.2、删除表操作

    /**
     * 删除表
     */
    @Test
    public void deleteTable() throws IOException {
        //获取管理员对象,用于表的删除
        Admin admin = connection.getAdmin();
        //删除一张表之前,需要先禁用表
        admin.disableTable(TableName.valueOf("myuser"));
        admin.deleteTable(TableName.valueOf("myuser"));
    }

e(delete);
}


## 5.2、删除表操作

```java
    /**
     * 删除表
     */
    @Test
    public void deleteTable() throws IOException {
        //获取管理员对象,用于表的删除
        Admin admin = connection.getAdmin();
        //删除一张表之前,需要先禁用表
        admin.disableTable(TableName.valueOf("myuser"));
        admin.deleteTable(TableName.valueOf("myuser"));
    }

以上是关于Hbase的API案例实战的主要内容,如果未能解决你的问题,请参考以下文章

HBase新版本Java API编程实战及基本操作方法封装

Hbase 设计与开发实战

hbase-api

hbase-api

hbase之java api实战一

hbase实战 hbase权威指南哪本好书