大数据之HBase API 完整使用 (第四章)

导读:本篇文章讲解 大数据之HBase API 完整使用 (第四章),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一、环境准备

1、新建项目后在 pom.xml 中添加依赖

<dependency>
 <groupId>org.apache.hbase</groupId>
 <artifactId>hbase-server</artifactId>
 <version>1.3.1</version>
</dependency>
<dependency>
 <groupId>org.apache.hbase</groupId>
 <artifactId>hbase-client</artifactId>
 <version>1.3.1</version>
</dependency>

二、HBaseAPI

1、获取 Configuration 对象

public class TestApi {


    private static Connection connection = null;
    private static Admin admin = null;

    static {
        try {
            //获取配置信息
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");
            //创建链接对象
            connection = ConnectionFactory.createConnection(configuration);
            //创建Admin的对象
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

2、判断表是否存在

    //判读表是否存在

    public static boolean isTableExist(String isTable) throws IOException {

        //连接配置
//        HBaseConfiguration configuration = new HBaseConfiguration(); //过时的配置
//        Configuration configuration = HBaseConfiguration.create();
//        configuration.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");

        /**
         * 获取管理员的对象
         * Admin 操作DDL
         * api返回Table的 是操作DML的对象
         */
//        HBaseAdmin baseAdmin = new HBaseAdmin(configuration);//过时的配置
//        Connection connection = ConnectionFactory.createConnection(configuration);
//        Admin admin = connection.getAdmin();
        boolean tableEnabled = admin.tableExists(TableNameValueOf(isTable));
//        boolean tableEnabled = admin.isTableEnabled(isTable);

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

3、创建表

    //1、 TODO、创建表
    public static void createTable(String tableName, String... cfs) throws IOException {
        //1、创建是否存在的列族信息
        if (cfs.length < 0) {
            System.out.println("请设置列族信息!");
            return;
        }

        //2、创建是否存在的表信息
        if (isTableExist(tableName)) {
            System.out.println(tableName + "表已存在!");
            return;
        }

        //3、创建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableNameValueOf(tableName));

        //循环添加列族
        for (String cf : cfs) {
            //5、创建列族描述器
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);

            //6、添加具体的列族信息
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        //创建表
        admin.createTable(hTableDescriptor);
    }

4、删除表

 //2、TODO 删除表
    public static void dropTable(String tableName) throws IOException {

        //1、判断表是否存在
        if (!isTableExist(tableName)) {
            System.out.println(tableName + "表不存在!!");
            return;
        }

        //使表下线
        admin.disableTable(TableNameValueOf(tableName));

        //删除表
        admin.deleteTable(TableNameValueOf(tableName));
    }

5、向表中插入数据


    //5、TODO 向表中插入数据
    public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
        //1、获取表对象
        Table table = connection.getTable(TableNameValueOf(tableName));
        //2、创建put对象
        Put put = new Put(Bytes.toBytes(rowKey));
        //3、给put赋值 列族、列、值
        put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));

        put.addColumn(Bytes.toBytes(cf), Bytes.toBytes("sex"), Bytes.toBytes("male"));
        //4、插入数据
        table.put(put);

        System.out.println("尽管存在,我还是可以走到这。。。。");
        table.close();
    }

6、删除多行数据

    //7、TODO 删除表数据
    public static void deleteData(String tableName, String rowKey, String cf, String cn) throws IOException {
        //1、获取表对象
        Table table = connection.getTable(TableNameValueOf(tableName));

        Delete delete = new Delete(Bytes.toBytes(rowKey));

        //3、获取对象、只传Rowkey的时候就相当于 deleteAll、删除表示为DeleteFamily
        table.delete(delete);

        table.close();
    }

7、获取所有数据

 //6、TODO 扫描表数据
    public static void getsCan(String tableName) throws IOException {
        //1、获取表对象
        Table table = connection.getTable(TableNameValueOf(tableName));

        //2、创建scan对象  左闭右开  右边不包括1005
        Scan scan = new Scan(Bytes.toBytes("1001"), Bytes.toBytes("1003"));

        //3、获取对象
        ResultScanner scanner = table.getScanner(scan);

        for (Result result : scanner) {
            for (Cell cell : result.rawCells()) {
                //打印数据
                System.out.println("Scan CF: " + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("Scan CN: " + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("Scan Value: " + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }


        table.close();
    }

8、获取某一行数据

  //6、TODO 获取表数据
    public static void getData(String tableName, String rowKey, String cf, String cn) throws IOException {
        //1、获取表对象
        Table table = connection.getTable(TableNameValueOf(tableName));

        //2、创建Get对象
        Get get = new Get(Bytes.toBytes(rowKey));

        //2.1指定获取列族
//        get.addFamily(Bytes.toBytes(cf));

        //2.2设置查询的列族和列的条件
        get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));

        //2.3 设置获取数据的版本
        get.setMaxVersions(5);

        //3、获取对象
        Result result = table.get(get);

        //4、解析数据
        for (Cell cell : result.rawCells()) {

            //打印数据
            System.out.println("CF: " + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("CN: " + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("Value: " + Bytes.toString(CellUtil.cloneValue(cell)));
        }

        table.close();
    }

  

9、获取某一行指定“列族:列”的数据

    //6、TODO 获取表数据
    public static void getData(String tableName, String rowKey, String cf, String cn) throws IOException {
        //1、获取表对象
        Table table = connection.getTable(TableNameValueOf(tableName));

        //2、创建Get对象
        Get get = new Get(Bytes.toBytes(rowKey));

        //2.1指定获取列族
//        get.addFamily(Bytes.toBytes(cf));

        //2.2设置查询的列族和列的条件
        get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));

        //2.3 设置获取数据的版本
        get.setMaxVersions(5);

        //3、获取对象
        Result result = table.get(get);

        //4、解析数据
        for (Cell cell : result.rawCells()) {

            //打印数据
            System.out.println("CF: " + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("CN: " + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("Value: " + Bytes.toString(CellUtil.cloneValue(cell)));
        }

        table.close();
    }

10、创建命名空间

   //3、TODO 创建命名空间
    public static void createNameSpace(String ns) {
        //NamespaceDescriptor 源码中已被私有化不可以new
        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();
        try {
            admin.createNamespace(namespaceDescriptor);
        } catch (NamespaceExistException e) {
            System.out.println(ns + "命名空间存在");
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("尽管存在,我还是可以走到这。。。。");

    }

11、关闭资源

   //关闭资源
    public static void close() {
        if (admin != null) {
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    //类型转换
    public static TableName TableNameValueOf(String tableName) {
        return TableName.valueOf(tableName);
    }

三、MapReduce

1、官方 HBase-MapReduce

2、自定义 HBase-MapReduce1

3、自定义 Hbase-MapReduce2

四、与 Hive 的集成

1、HBase 与 Hive 的对比

2、HBase 与 Hive 集成使用

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/15464.html

(0)
小半的头像小半

相关推荐

极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!