码迷,mamicode.com
首页 > 编程语言 > 详细

HBase JavaAPI

时间:2018-12-22 18:40:13      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:方便   qualifier   main   comm   esc   xml文件   resources   cell   eva   

一.概念

  1.对HBase JavaAPI的概述:

    01.hbase使用java语言编写,自然支持java编程

    02.支持CRUD操作

    03.JavaAPI包含了所有的hbase的shell,甚至比这个还要多

    04.JavaAPI是访问hbase的最快的方式

  2.api

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

    02.HbaseConfiguration.create():从classPath下加载hbase-default.xml和hbase-sitl.xml文件,所以需要将hbase-site.xml放入到classPath下。hbase-sitl.xml将覆盖hbase-default.xml的同名属性

    03.HTable句柄:为Configuration提供对象和访问table的名称

    

HTable table = new HTable(con, tableName);

  一个table对应一个句柄:

    001.提供了CRUD操作,且支持批处理

    002.设计简单,使用方便

    003.提供行级事务

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

    005.并发读,顺序写

    006.严格的行一致性

    007.创建句柄代价很大,创建一次后尽可能复用

    008.如果需要创建多个句柄,使用HtablePoll

    009.HTable并非线程安全的,一个线程创建一个即可,但性能较高

二.代码环节

  1.需要下面的pom节点

<dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.8.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.8.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.8.5</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>

  2.将虚拟机中的hdfs-sitl.xml和hbase-sitl.xml文件复制到项目中的resources文件下

  3.修改window下的hosts文件,路径是:C:\Windows\System32\drivers\etc\hosts

技术分享图片

技术分享图片

  4.代码

public class HBaseJavaAPI {
    public static void main(String[]args) throws Exception{
        //createTable();
        //addData();
        //getAllData();
        //deleteByRowKey();
        //getByRowKey();
        //deleteQualifier();
        //deleteTable();
    }
    
     /**
      * 1.创建表
      */
    public static void createTable() throws Exception {
        //1.创建一个Configuration对象
        Configuration con = HBaseConfiguration.create();
        
        /**
         * 2.创建HBaseAdmin对象,此对象包含了创建表,创建列族,检索表是否存在,修改表和列族结构,删除表等功能
         * HBaseAdmin对象的生命周期不宜太长
         */
        HBaseAdmin hBaseAdmin=new HBaseAdmin(con);
        //3.判断表是否存在
        if(hBaseAdmin.tableExists("hbase_demo_api")) {
            System.out.println("表已存在,不许重复创建");
        }else {
            //4.创建表的描述对象
            HTableDescriptor tableDescriptor=new HTableDescriptor("hbase_demo_api");
            tableDescriptor.addFamily(new HColumnDescriptor("grade"));
            tableDescriptor.addFamily(new HColumnDescriptor("course"));
            hBaseAdmin.createTable(tableDescriptor);
            System.out.println("表创建成功!");
        }
        hBaseAdmin.close();
    }
    
    /**
      * 2.新增数据
      */
    public static void addData() throws Exception {
        //1.创建一个Configuration对象
        Configuration con = HBaseConfiguration.create();
        //2.创建HTable句柄
        HTable htable=new HTable(con, "hbase_demo_api");
        //3.创建put对象
        Put put=new Put("class1".getBytes());    //class1就是rowkey
        put.addColumn("course".getBytes(), "sql".getBytes(), "90".getBytes());
        put.addColumn("course".getBytes(), "java".getBytes(), "89".getBytes());
        put.addColumn("grade".getBytes(), "".getBytes(), "c1".getBytes());
        //4.向表中插入数据
        htable.put(put);
        System.out.println("插入成功!");
        //5.关闭HTable对象
        htable.close();
    }
    /**
      * 3.查询全部数据
      */
    public static void getAllData() throws Exception {
        //1.创建一个Configuration对象
        Configuration con = HBaseConfiguration.create();
        //2.创建HTable句柄
        HTable htable=new HTable(con, "hbase_demo_api");
        //3.使用扫描器去扫描表
        ResultScanner scanner = htable.getScanner(new Scan());
        for (Result result: scanner) {
            for (Cell cell: result.rawCells()) {
                System.out.println("RowKey-------"+Bytes.toString(result.getRow()));
                System.out.println("column(列族)-------"+Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("col(列)-------"+Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("value(值)-------"+Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
        //4.关闭HTable对象
        htable.close();
    }
    /**
      * 4.删除表中指定RowKey的数据
      */
    public static void deleteByRowKey() throws Exception {
        //1.创建一个Configuration对象
        Configuration con = HBaseConfiguration.create();
        //2.创建HTable句柄
        HTable htable=new HTable(con, "hbase_demo_api");
        //3.创建delete对象
        Delete delete =new Delete(Bytes.toBytes("class1"));
        htable.delete(delete);
        System.out.println("删除成功!");
        //4.关闭HTable对象
        htable.close();
    }
    
    /**
      * 5.获取指定RowKey的数据
      */
    public static void getByRowKey() throws Exception {
        //1.创建一个Configuration对象
        Configuration con = HBaseConfiguration.create();
        //2.创建HTable句柄
        HTable htable=new HTable(con, "hbase_demo_api");
        //3.创建get对象
        Get get=new Get(Bytes.toBytes("class1"));
        Result result = htable.get(get);
        for (Cell cell : result.rawCells()) {
            System.out.println("RowKey-------"+Bytes.toString(result.getRow()));
            System.out.println("column(列族)-------"+Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("value(值)-------"+Bytes.toString(CellUtil.cloneValue(cell)));

        }
        htable.close();
        
    }
    /**
      * 6.获取指定RowKey的数据
      */
    public static void deleteQualifier() throws Exception {
        //1.创建一个Configuration对象
        Configuration con = HBaseConfiguration.create();
        //2.创建HTable句柄
        HTable htable=new HTable(con, "hbase_demo_api");
        //3.创建delete对象
        Delete delete=new Delete(Bytes.toBytes("class1"));
        delete.addColumn(Bytes.toBytes("course"), Bytes.toBytes("java"));
        
        htable.delete(delete);
        System.out.println("删除成功!");
        htable.close();
    }
    
    /**
      * 7.删除表
      */
    public static void deleteTable() throws Exception {
        //1.创建一个Configuration对象
        Configuration con = HBaseConfiguration.create();
        //2.创建HAdmin对象
        HBaseAdmin admin=new HBaseAdmin(con);
        //3.先将表禁用
        admin.disableTable("hbase_demo_api");
        //4.删除指定表
        admin.deleteTable("hbase_demo_api");
        System.out.println("删除成功!");
        //5.关闭资源
        admin.close();
    }

  

 

HBase JavaAPI

标签:方便   qualifier   main   comm   esc   xml文件   resources   cell   eva   

原文地址:https://www.cnblogs.com/wang2386033566/p/10161765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!