HBase的JavaAPI使用--基础篇

2021-01-26 10:55:30 浏览数 (1)

本篇博客小菌为大家带来的是关于HBase的JavaAPI使用。

创建项目,导入pom

代码语言:javascript复制
 <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.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.0-cdh5.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.2.0-cdh5.14.0</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>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>RELEASE</version>
            <scope>compile</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>
                    <!-- <verbal>true</verbal>-->
                </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>

然后任意创建一个类,例如: HBaseAPI 接下来正式开始书写Java代码

创建一个数据表

代码语言:javascript复制
    /**
     * 创建一个数据表
     * @throws IOException
     */
    @Test
    public void createTable() throws IOException {

        //实例化配置文件对象
        Configuration conf = new Configuration();

        //设置连接zookeeper的节点
        conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
        Connection connection = ConnectionFactory.createConnection(conf);


        //设置一个管理员
        Admin admin = connection.getAdmin();

        //设置表名称
        TableName tableName = TableName.valueOf("mytest1");    //"myuser" 是字符串

        //设置表的列族
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);

        HColumnDescriptor hColumnDescriptor1 = new HColumnDescriptor("f1");
        HColumnDescriptor hColumnDescriptor2= new HColumnDescriptor("f2");
        HColumnDescriptor hColumnDescriptor3 = new HColumnDescriptor("f3");


        //为表添加列族
        hTableDescriptor.addFamily(hColumnDescriptor1);
        hTableDescriptor.addFamily(hColumnDescriptor2);
        hTableDescriptor.addFamily(hColumnDescriptor3);

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

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

    }

添加数据[逐行添加]

代码语言:javascript复制
    /**
     * 添加数据【逐条添加】
     * @throws IOException
     */
    @Test
    public void putdata1() throws IOException {


        //连接数据库
        Configuration conf = new Configuration();


        //设置连接zookeeper的节点
        conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");

        Connection connection = ConnectionFactory.createConnection(conf);

        //读取数据
        Table mytest1 = connection.getTable(TableName.valueOf("mytest1"));


        //创建数据
        Put put = new Put("0001".getBytes());

        put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("zhangsan"));
        put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(18));
        put.addColumn("f1".getBytes(),"sex".getBytes(),Bytes.toBytes(1));
        put.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes("0001"));


        //将数据写入数据表
        mytest1.put(put);

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

    }

添加数据[多行添加]

代码语言:javascript复制
/**
     * 添加数据【多条问题】
     * @throws IOException
     */
    @Test
    public void putdata2() throws IOException {

        //连接数据库
        Configuration conf = new Configuration();


        //设置连接zookeeper的节点

        conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");

        Connection connection = ConnectionFactory.createConnection(conf);

        //读取数据
        Table mytest1 = connection.getTable(TableName.valueOf("mytest1"));


        //创建put对象,并指定rowkey
        Put put = new Put("0002".getBytes());
        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));
        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("0005".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("0006".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);

        mytest1.put(listPut);

        connection.close();

    }

查询数据[一行]

代码语言:javascript复制
    /**
     * 查询 一行 的 数据
     * @throws IOException
     */
    @Test
    public void searchData() throws IOException {

        //连接数据库
        Configuration conf = new Configuration();


        //设置连接zookeeper的节点

        conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");

        Connection connection = ConnectionFactory.createConnection(conf);

        //读取数据
        Table mytest1 = connection.getTable(TableName.valueOf("mytest1"));


        // 获取到 rowkey 为0002的数据
        Get get = new Get("0002".getBytes());

        //result为返回的一行数据
        Result result = mytest1.get(get);

        //遍历一行内的所有列
        Cell[] cells = result.rawCells();

        //遍历每一个cell
        for (Cell cell : cells) {

            // 若 cell的 列名 为 'id' 或 'age'
            if (Bytes.toString(CellUtil.cloneQualifier(cell)).equals("id") || Bytes.toString(CellUtil.cloneQualifier(cell)).equals("age")){

                // 获取列族名
                System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));

                // 获取 列名
                System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));

                // 获取行键 rowkey
                System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));

                // 获取 cell的值 【因为age 和 id 列的数据在添加的时候设置为int类型的,故获取也需要用Bytes.toiInt】
                System.out.println(Bytes.toInt(CellUtil.cloneValue(cell)));


            }else{

                System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));

                System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));

                System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));

                System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));

            }

        }

        //关闭连接
        mytest1.close();
        connection.close();

    }

限定条件查询数据[一行]

代码语言:javascript复制
/**
     *  限定条件 查询一行 的 数据
     * @throws IOException
     */
    @Test
    public void searchData2() throws IOException {

        // 连接数据库
        Configuration conf = new Configuration();

        conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");

        Connection connection = ConnectionFactory.createConnection(conf);

        // 读取表
        Table mytest1 = connection.getTable(TableName.valueOf("mytest1"));

        // 获取rowkey为0002的行数据
        Get get = new Get("0002".getBytes());

        // 限定查询的列族
        get.addFamily("f1".getBytes());   // 列族为'f1'

        //限定查询的列
        get.addColumn("f1".getBytes(),"name".getBytes());   // 列名为'name'

        //result 是一行数据
        Result result = mytest1.get(get);

        System.out.println("rowkey : "  Bytes.toString(result.getRow()));

        // 遍历一行内的所有列
        Cell[] cells = result.rawCells();

        //遍历每一个cell
        for (Cell cell : cells) {

            //若cell的列是id , age 就将数据转换成int

            //先获取到cell的列名
            String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));

            if (qualifier.equals("id")||qualifier.equals("age")){

                // 获取到列族名
                System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));

                // 获取到列名
                System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));

                // 获取到值
                System.out.println(Bytes.toInt(CellUtil.cloneValue(cell)));

            }else {

                // 获取到列族名
                System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));

                // 获取到列名
                System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));

                // 获取到值
                System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));

            }

        }

        // 关闭连接
        mytest1.close();
        connection.close();



    }

通过 startKey 和 endKey 进行限制【全表扫描】

代码语言:javascript复制
    /**
     * 通过 startKey 和 endKey 进行【全表扫描】
     * @throws Exception
     */
    @Test
    public void searchData3() throws Exception {

        // 连接 数据库
        Configuration conf = new Configuration();

        conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");

        Connection connection = ConnectionFactory.createConnection(conf);

        //读取表
        Table mytest1 = connection.getTable(TableName.valueOf("mytest1"));

        // Scan 全表扫描
        Scan scan = new Scan();
        //设置startKey 和 endKey
        scan.setStartRow("0002".getBytes());
        scan.setStopRow("0006".getBytes());

        ResultScanner scanner = mytest1.getScanner(scan);

        //进行遍历打印
        for (Result result : scanner) {

            // 获取rowkey
            System.out.println("rowkey:" Bytes.toString(result.getRow()));

            // 获取列族为 f1, 列名为 id 的 value 值
            System.out.println(Bytes.toInt(result.getValue("f1".getBytes(),"id".getBytes())));

            // 获取列族为 f1, 列名为 name 的 value 值
            System.out.println(Bytes.toString(result.getValue("f1".getBytes(),"name".getBytes())));

            // 获取列族为 f1, 列名为 age 的value 值
            System.out.println(Bytes.toInt(result.getValue("f1".getBytes(),"age".getBytes())));

        }
    }

全表扫描

代码语言:javascript复制
    /**
     * 全表扫描数据
     * @throws Exception
     */
    @Test
    public void searchData4() throws Exception {

        // 连接 数据库
        Configuration conf = new Configuration();

        conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");

        Connection connection = ConnectionFactory.createConnection(conf);

        //读取表
        Table mytest1 = connection.getTable(TableName.valueOf("mytest1"));

        // Scan 全表扫描
        Scan scan = new Scan();

        //scan 为 Hbase 行数据的集合
        ResultScanner scanner = mytest1.getScanner(scan);

        for (Result result : scanner) {

            System.out.println("rowkey:" Bytes.toString(result.getRow()));

            // 获取f1列族下的id列
            System.out.println(Bytes.toInt(result.getValue("f1".getBytes(),"id".getBytes())));

            // 获取f1列族下的name列
            System.out.println(Bytes.toString(result.getValue("f1".getBytes(),"name".getBytes())));

            // 获取f1列族下的age列
            //System.out.println(Bytes.toInt(result.getValue("f1".getBytes(),"age".getBytes())));
            System.out.println(Bytes.toInt(result.getValue("f1".getBytes(),"age".getBytes())));


        }

    }

本次的分享到这里就结束了,受益的小伙伴或对大数据技术感兴趣的朋友记得点赞关注小菌哟(^U^)ノ~YO后续小菌会为大家带来HBase的JavaAPI使用的升级篇,敬请期待!

0 人点赞