• 分享知识-快乐自己:HBase编程


    HBase编程:

    一):大数据(hadoop)初始化环境搭建

    二):大数据(hadoop)环境搭建

    三):运行wordcount案例

    四):揭秘HDFS

    五):揭秘MapReduce

    六):揭秘HBase

    七):HBase编程

    -----------------------------------------------------------------

    HBase JavaAPI概述:

      1.HBase使用Java语言编写的,自然支持Java编程

      2.支持CRUD操作:create read update delete

      3.JavaAPI包含了所有HBase的shell,甚至更多

      4.JavaAPI是访问HBase的最快方式

    JavaAPI程序设计:

    1、创建一个Configuration

    Configuration conf = HbaseConfiguration.create();

    Configuration对象包含了连接到HBase服务的信息:zookeeper的位置,连接时间等

    HbaseConfiguration.create()内部逻辑:

      从CLASSPATH下加载hbase-default.xml和hbase-site.xml文件需将hbase-site.xml放入到CLASSPATH下 hbase-site.xml将覆盖hbase-default.xml的同名属性

    自定义配置文件,使用Configuration加载

      Configuration newConf = Configuration.create(existingConf);

      用户自定义的配置文件将在已有配置文件之后加载将覆盖hbase-default.xml和 hbase-site.xml中的配置

    自定义参数值:

    Configuration conf=HbaseConfiguration.create();

    conf.set("hbase.zookeeper.quorum", "admin-01,admin-02"); 

    通常不推荐这么做!

    2、创建HTable句柄

      提供Configuration对象和待访问Table名称 HTable table = new HTable(conf, tableName);

    一个table对应一个HTbale句柄:

      提供了CRUD操作

      设计简单、使用方便 

      提供行级事务

      不支持多行事务或者表级别的事务 

      严格的行一致性 

      并发读、顺序写

    创建HTable句柄代价很大  扫描.META.表等; 

      创建一次,以后尽可能复用;

      如果需要创建多个Htable句柄,使用 HTablePool;

      HTable并非线程安全的  一个线程创建一个即可 

      Htable支持CRUD批处理 

      非线程安全,仅是为了提高性能

    3、执行相应的CRUD操作

      执行put、get、delete、scan等操作

      table.getTableName();

    4、关闭HTable句柄

      将内存数据刷新到磁盘上  释放各种资源

      table.close();

    前期工作:

    将虚拟主机当中的hbase-site.xmlhdfs-site.xml文件复制到项目下的classpath

    修改windows真机的hosts文件,添加三台机器的映射

    导入POM文件:

    <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-common</artifactId>
                <version>2.8.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-hdfs</artifactId>
                <version>2.8.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-client</artifactId>
                <version>2.8.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-client</artifactId>
                <version>1.3.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-server</artifactId>
                <version>1.3.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.hbase</groupId>
                <artifactId>hbase-common</artifactId>
                <version>1.3.1</version>
            </dependency>

    HBase_API 操作:

    package com.gdbd;
    
    import java.io.IOException;
    import java.util.Scanner;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.hbase.Cell;
    import org.apache.hadoop.hbase.CellUtil;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.HColumnDescriptor;
    import org.apache.hadoop.hbase.HTableDescriptor;
    import org.apache.hadoop.hbase.MasterNotRunningException;
    import org.apache.hadoop.hbase.ZooKeeperConnectionException;
    import org.apache.hadoop.hbase.client.Delete;
    import org.apache.hadoop.hbase.client.Get;
    import org.apache.hadoop.hbase.client.HBaseAdmin;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.Put;
    import org.apache.hadoop.hbase.client.Result;
    import org.apache.hadoop.hbase.client.ResultScanner;
    import org.apache.hadoop.hbase.client.Scan;
    import org.apache.hadoop.hbase.util.Bytes;
    
    /**
     * Hello world!
     *
     */
    public class App {
    
        public static void main(String[] args) throws IOException {
    
            do {
                System.out.println("
    
    *********************HBase_API***神码操作平台*********************");
                System.out.println("1、创建表");
                System.out.println("2、向表中添加数据");
                System.out.println("3、查询表中所有数据");
                System.out.println("4、查询表中指定RowKey的所有数据");
                System.out.println("5、删除指定列族的指定列");
                System.out.println("6、删除指定列族");
                System.out.println("7、删除表中指定RowKey的所有数据删除表中指定RowKey的所有数据");
                System.out.println("8、删除表");
                Scanner input = new Scanner(System.in);
                System.out.println("请输入......");
                int num = input.nextInt();
                switch (num) {
                case 1:
                    demo01();
                    break;
                case 2:
                    demo02();
                    break;
                case 3:
                    cat();
                    break;
                case 4:
                    catRows();
                    break;
                case 5:
                    delFamilyColumn();
                    break;
                case 6:
                    delFamily();
                    break;
                case 7:
                    delRow();
                    break;
                case 8:
                    delTable();
                    break;
                }
            } while (true);
        }
    
        /***
         * 创建表
         * 
         * @throws MasterNotRunningException
         * @throws ZooKeeperConnectionException
         * @throws IOException
         */
        private static void demo01() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
            System.out.println("正在 创建表...
    ");
            // 创建一个Configuration对象
            Configuration configuration = HBaseConfiguration.create();
            /**
             * 创建HBaseAdmin对象 此对象 提供了 建表,创建列族,检查表是否存在,修改表和列族结构,删除表等功能 HBaseAdmin实例的生命周期不宜太长
             */
            HBaseAdmin admin = new HBaseAdmin(configuration);
            if (admin.tableExists("hbase_demo")) {
                System.out.println("表已经存在!!!");
            } else { // 证明表不存在
                HTableDescriptor table = new HTableDescriptor("hbase_demo"); // 创建表的描述对象
                // new HColumnDescriptor对象:设置列族的特性
                table.addFamily(new HColumnDescriptor("grade"));
                table.addFamily(new HColumnDescriptor("course"));
                // 定义好了表名和列族 可以创建表
                admin.createTable(table);
                System.out.println("表创建成功!!!");
            }
        }
    
        /***
         * 向表中添加数据
         * 
         * @throws IOException
         */
        private static void demo02() throws IOException {
            System.out.println("正在  向表中添加数据...
    ");
            // 创建一个Configuration对象
            Configuration configuration = HBaseConfiguration.create();
            // 创建HTable对象
            HTable table = new HTable(configuration, "hbase_demo");
            // 创建put对象
            Put put = new Put("xiaohei".getBytes()); // xiaohei是 row key
            put.addColumn("course".getBytes(), "java".getBytes(), "90".getBytes()); // course是列族
            put.addColumn("course".getBytes(), "sql".getBytes(), "99".getBytes()); // java 和sql 都是列
            put.addColumn("grade".getBytes(), "one".getBytes(), "1".getBytes());
            // 向表中增加数据
            table.put(put);
            // 关闭table
            table.close();
            System.out.println("插入成功......");
        }
    
        /***
         * 查询表数据
         * 
         * @throws IOException
         */
        private static void cat() throws IOException {
            System.out.println("正在  查询表数据...
    ");
            // 创建一个Configuration对象
            Configuration configuration = HBaseConfiguration.create();
            // 创建HTable对象
            HTable table = new HTable(configuration, "hbase_demo");
            // 创建ResultScanner对象
            ResultScanner rs = table.getScanner(new Scan());
            for (Result r : rs) {
                System.out.println("***************************Result Start***************************");
                for (Cell cell : r.rawCells()) {
                    System.out.println("=======================start============================");
                    System.out.println("RowKey(行键)===>" + Bytes.toString(r.getRow()));
                    System.out.println("family(列族)===>" + Bytes.toString(CellUtil.cloneFamily(cell)));
                    System.out.println("column(列)===>" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                    System.out.println("value(值)===>" + Bytes.toString(CellUtil.cloneValue(cell)));
                    System.out.println("=========================END==========================");
                }
                System.out.println("****************************Result END****************************");
            }
            // 关闭table
            table.close();
            System.out.println("表数据查询成功...");
        }
    
        /***
         * 查询表中指定RowKey的所有数据
         * 
         * @throws IOException
         */
        private static void catRows() throws IOException {
            System.out.println("正在  查询表中指定RowKey的所有数据...
    ");
            // 创建一个Configuration对象
            Configuration configuration = HBaseConfiguration.create();
            // 创建HTable对象
            HTable table = new HTable(configuration, "hbase_demo");
            // 创建get对象 获取所有rowkey是xiaohei的所有数据
            Get get = new Get(Bytes.toBytes("xiaohei"));
            Result result = table.get(get);
            for (Cell cell : result.rawCells()) {
                System.out.println("=======================start============================");
                System.out.println("RowKey(行键)===>" + Bytes.toString(result.getRow()));
                System.out.println("family(列族)===>" + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("column(列)===>" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("value(值)===>" + Bytes.toString(CellUtil.cloneValue(cell)));
                System.out.println("=========================END==========================
    ");
            }
            // 关闭table
            table.close();
            System.out.println("成功 查询表中指定RowKey的所有数据");
        }
    
        /***
         * 删除表中指定RowKey的所有数据
         * 
         * @throws IOException
         */
        private static void delRow() throws IOException {
            System.out.println("正在删除表中指定RowKey的所有数据...
    ");
            // 创建一个Configuration对象
            Configuration configuration = HBaseConfiguration.create();
            // 创建HTable对象
            HTable table = new HTable(configuration, "hbase_demo");
            // 创建delete对象
            Delete delete = new Delete(Bytes.toBytes("xiaohei"));
            table.delete(delete);
            // 关闭table
            table.close();
            System.out.println("成功删除表中指定RowKey的所有数据...");
        }
    
        /***
         * 删除指定列族的指定列
         * 
         * @throws IOException
         */
        private static void delFamilyColumn() throws IOException {
            System.out.println("正在 删除指定列族的指定列...
    ");
            // 创建一个Configuration对象
            Configuration configuration = HBaseConfiguration.create();
            // 创建HTable对象
            HTable table = new HTable(configuration, "hbase_demo");
            // 创建delete对象
            Delete delete = new Delete(Bytes.toBytes("xiaobaibai"));// 指定的
            delete.addColumn(Bytes.toBytes("course"), Bytes.toBytes("java"));
            table.delete(delete);
            // 关闭table
            table.close();
            System.out.println("成功 删除指定列族的指定列...");
        }
    
        /***
         * 删除指定列族的
         * 
         * @throws IOException
         */
        private static void delFamily() throws IOException {
            System.out.println("正在 删除指定列族...
    ");
            // 创建一个Configuration对象
            Configuration configuration = HBaseConfiguration.create();
            // 创建HTable对象
            HTable table = new HTable(configuration, "hbase_demo");
            // 创建delete对象
            Delete delete = new Delete(Bytes.toBytes("xiaobaibai"));// 指定的
            delete.addFamily("course".getBytes());
            table.delete(delete);
            // 关闭table
            table.close();
            System.out.println("成功 删除指定列族...");
        }
    
        /***
         * 删除表
         * 
         * @throws MasterNotRunningException
         * @throws ZooKeeperConnectionException
         * @throws IOException
         */
        private static void delTable() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
            System.out.println("正在 删除表...
    ");
            // 创建一个Configuration对象
            Configuration configuration = HBaseConfiguration.create();
            // 创建HAdmin对象
            HBaseAdmin admin = new HBaseAdmin(configuration);
            admin.disableTable("hbase_demo");// 禁用表
            admin.deleteTable("hbase_demo");// 删除表
            // 关闭admin对象
            admin.close();
            System.out.println("成功 删除表...");
        }
    
    }
  • 相关阅读:
    Dev XtraTreeList 学习
    DataGridView单元格显示GIF图片
    如何在.NET上处理二维码
    设计模式之UML类图的常见关系(一)
    .NET常用操作小知识
    Sql Server 存储过程使用技巧
    WinForm中如何判断关闭事件来源于用户点击右上角的“关闭”按钮
    WinFrom 安装包制作
    DBHelper数据库操作类(二)
    spring cloud zipkin
  • 原文地址:https://www.cnblogs.com/mlq2017/p/10055170.html
Copyright © 2020-2023  润新知