HBase API 操作范例

Posted

tags:

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

package com.test.hbase.api;


import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.*;

import org.apache.hadoop.hbase.client.*;

import org.apache.hadoop.hbase.util.Bytes;


import java.io.IOException;

import java.util.ArrayList;

import java.util.List;


/**

 * Created by zsq.

 * Date: 2017/6/14

 * desc:Hbase api操作

 */

public class HBaseAPIDemo {


    public static void main(String[] args) throws IOException {

        Configuration conf = HBaseConfiguration.create();

        //设置zk的地址

        conf.set("hbase.zookeeper.quorum", Constants.hostNames);


        //获取链接hbase数据的链接对象

        Connection conn = ConnectionFactory.createConnection(conf);

        //过去时接口

        //  HBaseAdmin admin=new HBaseAdmin(conn);


        //获取操作hbase数据库的对象

        HBaseAdmin ha = (HBaseAdmin) conn.getAdmin();


//        createNamespace(ha);

//        listNamespace(ha);

//        createTables(ha);

//        listNamespaceTables(ha);

//        putDataToTables(ha, conn);

//        putMultilDataToTables(ha, conn);

//        scanTablesData(ha, conn);

//        getTablesData(ha, conn);

        deleteNsAndTable(ha, conn);



    }



    /**

     * shell操作:create_namespace ‘ns2‘

     * 说明:创建命名空间

     *

     * @param ha

     * @throws IOException

     */

    public static void createNamespace(HBaseAdmin ha) throws IOException {


        ha.createNamespace(NamespaceDescriptor.create("ns2").build());

        ha.close();

        System.out.println("-----createNamespace----over");


    }



    /**

     * shell操作:list_namespace

     * 描述: 查看所有命名空间

     *

     * @param ha

     * @throws IOException

     */

    public static void listNamespace(HBaseAdmin ha) throws IOException {


        NamespaceDescriptor[] listns = ha.listNamespaceDescriptors();

        for (NamespaceDescriptor ns : listns) {

            System.out.println(ns.getName());

        }

        ha.close();

        System.out.println("-----listNamespace----over");

    }



    /**

     * 创建表:help ‘create‘

     * shell操作: create ‘ns2:stu‘, ‘cf_info‘, ‘cf_beizhu‘

     *

     * @param ha

     * @throws IOException

     */

    public static void createTables(HBaseAdmin ha) throws IOException {

        //先判断表是否存在

        if (!ha.tableExists("ns2:stu")) {

            HTableDescriptor htable = new HTableDescriptor(TableName.valueOf("ns2:stu"));

            //创建一个表至少需要添加一个列族

            htable.addFamily(new HColumnDescriptor("cf_info"));

            htable.addFamily(new HColumnDescriptor("cf_beizhu"));

            ha.createTable(htable);

        }

        ha.close();

        System.out.println("-----createTables----over");

    }


    /**

     * shell操作:

     * 查看命名空下的表:

     * list_namespace_tables ‘ns2‘

     *

     * @param ha

     * @throws IOException

     */

    public static void listNamespaceTables(HBaseAdmin ha) throws IOException {


        HTableDescriptor[] htables = ha.listTableDescriptorsByNamespace("ns2");

        for (HTableDescriptor tb : htables) {

            System.out.println(tb.getTableName());

        }

        ha.close();

        System.out.println("-----createTables----over");

    }



    /**

     * 添加数据到表:help ‘put‘

     * shell操作:put ‘ns1:t1‘, ‘r1‘, ‘c1‘, ‘value‘

     *

     * @param ha

     * @param conn

     * @throws IOException

     */

    public static void putDataToTables(HBaseAdmin ha, Connection conn) throws IOException {


        //判断表是否存在

        if (ha.tableExists("ns2:stu")) {

            Table mTable = conn.getTable(TableName.valueOf("ns2:stu"));

            //创建Put对象并且添加rowkey

            Put put = new Put("soft_20170101".getBytes());

            //第一个参数是列族,第二个参数是列名,第三个参数是列的值

            put.addColumn("cf_info".getBytes(), "name".getBytes(), "laowang".getBytes());

            put.addColumn("cf_info".getBytes(), "age".getBytes(), "20".getBytes());

            put.addColumn("cf_info".getBytes(), "sex".getBytes(), "nan".getBytes());

            put.addColumn("cf_beizhu".getBytes(), "address".getBytes(), "fangshan".getBytes());

            mTable.put(put);

        }


        ha.close();

        System.out.println("-----putDataToTables----over");

    }


    /**

     * 批量添加数据到Hbase表

     *

     * @param ha

     * @param conn

     * @throws IOException

     */

    public static void putMultilDataToTables(HBaseAdmin ha, Connection conn) throws IOException {


        //判断表是否存在

        if (ha.tableExists("ns2:stu")) {

            Table mTable = conn.getTable(TableName.valueOf("ns2:stu"));


            List<Put> listput = new ArrayList<Put>();

            for (int i = 0; i < 100; i++) {

                //创建Put对象并且添加rowkey

                Put put = new Put(("soft_20170101" + i).getBytes());

                //第一个参数是列族,第二个参数是列名,第三个参数是列的值

                put.addColumn("cf_info".getBytes(), "name".getBytes(), ("laowang" + i).getBytes());

                put.addColumn("cf_info".getBytes(), "age".getBytes(), "20".getBytes());

                if (i % 2 == 1) {

                    put.addColumn("cf_info".getBytes(), "sex".getBytes(), "nv".getBytes());

                } else {

                    put.addColumn("cf_info".getBytes(), "sex".getBytes(), "nan".getBytes());

                }

                put.addColumn("cf_beizhu".getBytes(), "address".getBytes(), ("fangshan" + i).getBytes());

                listput.add(put);

            }


            //批量插入

            mTable.put(listput);

        }


        ha.close();

        System.out.println("-----putMultilDataToTables----over");

    }



    /**

     * 查看表数据:

     * scan ‘ns2:stu‘

     * 查看一个列族

     * scan ‘ns2:stu‘,{COLUMNS => ‘cf_info‘}

     * 查看多个列族

     * scan ‘ns2:stu‘,{COLUMNS => [‘cf_info‘,‘cf_beizhu‘]}

     * 查看某个列的数据:

     * scan ‘ns2:stu‘,{COLUMNS => ‘cf_info:name‘}

     *

     * @param ha

     * @param conn

     * @throws IOException

     */

    public static void scanTablesData(HBaseAdmin ha, Connection conn) throws IOException {


        //判断表是否存在

        if (ha.tableExists("ns2:stu")) {

            Table mTable = conn.getTable(TableName.valueOf("ns2:stu"));


            //扫描整张表

            Scan scan = new Scan();

            //scan.addFamily("cf_info".getBytes());

//            scan.addColumn("cf_info".getBytes(), "name".getBytes());

            ResultScanner rs = mTable.getScanner(scan);

            for (Result result : rs) {

                System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));

                System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));

                System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));

                System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));

            }

        }


        ha.close();

        System.out.println("-----scanTablesData----over");

    }


    /**

     * 查看某一行数据:help ‘get‘

     * <p>

     * get ‘ns1:stu‘,‘data_20170102‘

     * <p>

     * 查看某行的其中的一个列族的数据:

     * get ‘ns1:stu‘,‘data_20170102‘,{COLUMN => ‘cf_info‘}

     * <p>

     * 查看某行的其中的多个列族的数据:

     * get ‘ns1:stu‘,‘data_20170102‘,{COLUMN => [‘cf_info‘,‘cf_beizhu‘]}

     * <p>

     * 获取某一个列的值:

     * get ‘ns1:stu‘,‘data_20170102‘,‘cf_info:name‘

     *

     * @param ha

     * @param conn

     * @throws IOException

     */


    public static void getTablesData(HBaseAdmin ha, Connection conn) throws IOException {


        //判断表是否存在

        if (ha.tableExists("ns2:stu")) {

            Table mTable = conn.getTable(TableName.valueOf("ns2:stu"));


            //获取整行数据

            Get get = new Get("soft_2017010196".getBytes());

//            get.addFamily("cf_info".getBytes());

            get.addColumn("cf_info".getBytes(), "name".getBytes());

            Result result = mTable.get(get);


            System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));

            System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));

            System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));

            System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));

        }


        ha.close();

        System.out.println("-----getTablesData----over");

    }


    /**

     * 操作:删除命名空间和命名空间所有表

     *

     * @param ha

     * @param conn

     * @throws IOException

     */

    public static void deleteNsAndTable(HBaseAdmin ha, Connection conn) throws IOException {


        //遍历命名空间下所有表

        HTableDescriptor[] htables = ha.listTableDescriptorsByNamespace("ns1");

        for (HTableDescriptor tb : htables) {

            System.out.println(tb.getTableName());

            if (ha.isTableEnabled(tb.getTableName())) {

                ha.disableTable(tb.getTableName());

            }

            ha.deleteTable(tb.getTableName());

        }

        ha.deleteNamespace("ns1");

        ha.close();

        System.out.println("-----deleteNsAndTable----over");

    }


    


}


        // HBase 的过滤器操作


public class HbaseAPIFilter {


    public static void main(String[] args) throws Exception {

//        singleColumnValueFilter();


//        qualifierFilter();

        familyFilter();

    }



    /**

     * 1、单个列值过滤   SingleColumnValueFilter

     *

     * @throws IOException

     */

    public static void singleColumnValueFilter() throws IOException {

        Table mTable = TableUtils.getTable(Constants.tableName);



        //如果匹配,那么可用获取行数据

        //单个列值的匹配:比较基类为BinaryComparator   BinaryComparator 匹配完整字节数组

        // SingleColumnValueFilter scvf = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "laowang88".getBytes());

        SingleColumnValueFilter scvf = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator("laowang88".getBytes()));


        //BinaryPrefixComparator 匹配开始的部分字节数组 name的值开始部分为"xiao"的行都过滤出来

        SingleColumnValueFilter scvf2 = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryPrefixComparator("xiao".getBytes()));


//        Only EQUAL or NOT_EQUAL comparisons are valid with this comparator.

//        所以对于RegexStringComparator基类只能用EQUAL或者NOT_EQUAL

        SingleColumnValueFilter scvf3 = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^[x].*$"));



//        Only EQUAL or NOT_EQUAL tests are valid with this comparator.

//        所以对于SubstringComparator基类只能用EQUAL或者NOT_EQUAL

        SingleColumnValueFilter scvf4 = new SingleColumnValueFilter("cf_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, new SubstringComparator("bai"));



        Scan scan = new Scan();

        scan.setFilter(scvf4);

        ResultScanner rs = mTable.getScanner(scan);

        for (Result result : rs) {

            System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));

            System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));

            System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));

            System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));

        }

    }



    /**

     * 2. 列过滤器(列过滤器表中存在该列的数据都出来)

     * QualifierFilter

     *

     * @throws IOException

     */

    public static void qualifierFilter() throws IOException {

        Table mTable = TableUtils.getTable(Constants.tableName);


        //列名过滤:

        //比较基类为BinaryComparator   BinaryComparator 匹配完整字节数组

        QualifierFilter columnsNameFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator("name".getBytes()));

        QualifierFilter columnsNameFilter2 = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator("age".getBytes()));


//        Only EQUAL or NOT_EQUAL tests are valid with this comparator.

//        所以对于SubstringComparator基类只能用EQUAL或者NOT_EQUAL

        QualifierFilter columnsNameFilter3 = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("a"));



        

        //BinaryPrefixComparator 匹配开始的部分字节数组, (可能用)

        // RegexStringComparator, 正则表达式匹配(可能用)


        Scan scan = new Scan();

        scan.setFilter(columnsNameFilter3);

        ResultScanner rs = mTable.getScanner(scan);

        for (Result result : rs) {

            System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));

            System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));

            System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));

            System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));

        }


    }



    /**

     * 定义列族过滤器(表中存在该列族的数据都出来)

     * FamilyFilter

     *

     * @throws IOException

     */

    public static void familyFilter() throws IOException {

        Table mTable = TableUtils.getTable(Constants.tableName);


        //因为是根据列族的名称过滤,所以传入列族名称

        FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, new BinaryPrefixComparator("cf_i".getBytes()));


        //        Only EQUAL or NOT_EQUAL tests are valid with this comparator.

//        所以对于RegexStringComparator基类只能用EQUAL或者NOT_EQUAL

        FamilyFilter familyFilter2 = new FamilyFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^[a-z_]{3}[^b].+$"));



//        BinaryComparator 匹配完整字节数组, (可能用)

//        SubstringComparator 比配子串、大小写不敏感(可能用)


        Scan scan = new Scan();

        scan.setFilter(familyFilter2);

        ResultScanner rs = mTable.getScanner(scan);

        for (Result result : rs) {

            System.out.println("name:" + Bytes.toString(result.getValue("cf_info".getBytes(), "name".getBytes())));

            System.out.println("age:" + Bytes.toString(result.getValue("cf_info".getBytes(), "age".getBytes())));

            System.out.println("sex:" + Bytes.toString(result.getValue("cf_info".getBytes(), "sex".getBytes())));

            System.out.println("address:" + Bytes.toString(result.getValue("cf_beizhu".getBytes(), "address".getBytes())));

        }


    }


}


注意: 前提是保证集群正常运行和HBase集群正常工作。

本文出自 “星月情缘” 博客,请务必保留此出处http://xuegodxingyue.blog.51cto.com/5989753/1937250

以上是关于HBase API 操作范例的主要内容,如果未能解决你的问题,请参考以下文章

大数据之API操作HBase

如何使用Java API操作Hbase

通过Python操作hbase api

HBase 6用Phoenix Java api操作HBase

如何使用Java API操作Hbase

hadoop2-HBase的Java API操作