zoukankan      html  css  js  c++  java
  • Hbase深入学习(六) Java操作HBase

    Hbase深入学习() ―― Java操作HBase

    本文讲述如何用hbase shell命令和hbase java api对hbase服务器进行操作。

    先看以下读取一行记录hbase是如何进行工作的,首先hbaseclient端会连接zookeeper qurom,例如hbase_config.set(“hbase.zookeeper.quorum”,”192.168.50.216”)).通过zookeeper组件client能获知哪个server管理root-region。那么client就去访问管理root的server,在meta中记录了hbase中所有表信息,可以使用scan ‘meta’命令列出你创建的所有表的相信信息,从而获取region分布的新。一旦client获取了这一行的位置信息,比如这一行属于哪个region,client将会缓存这个信息并直接访问hregionserver.久而久之client缓存的信息渐渐增多,即使不妨问,meta表也能知道去访问哪个hregionserver.hbase中包含两种基本类型的文件,一种用于存储wal的log,另一种用于存储具体的数据,这些数据都通过dfs client和分布式的文件系统hdfs进行交互实现存储。

    Hbase的一些内存实现原理:

    * HMaster— HBase中仅有一个Master server。
        * HRegionServer—负责多个HRegion使之能向client端提供服务,在HBase cluster中会存在多个HRegionServer。
        * ServerManager—负责管理Region server信息,如每个Region server的HServerInfo(这个对象包含HServerAddress和startCode),已load Region个数,死亡的Region server列表
        * RegionManager—负责将region分配到region server的具体工作,还监视root和meta 这2个系统级的region状态。
        * RootScanner—定期扫描root region,以发现没有分配的meta region。
        * MetaScanner—定期扫描meta region,以发现没有分配的user region。

    HBase基本命令
    下面我们再看看看HBase的一些基本操作命令,我列出了几个常用的HBaseShell命令,如下:

    名称

    命令表达式

    创建表

    create '表名称', '列名称1','列名称2','列名称N'

    添加记录      

    put '表名称', '行名称', '列名称:', '值'

    查看记录

    get '表名称', '行名称'

    查看表中的记录总数

    count  '表名称'

    删除记录

    delete  '表名' ,'行名称' , '列名称'

    删除一张表

    先要屏蔽该表,才能对该表进行删除,第一步 disable '表名称' 第二步  drop '表名称'

    查看所有记录

    scan "表名称"  

    查看某个表某个列中所有数据

    scan "表名称" , ['列名称:']

    更新记录 

    就是重写一遍进行覆盖

     

    可以进入 hbase 的shell 模式中你可以输入 help 命令查看到你可以执行的命令和对该命令的说明,例如对scan这个命令,help中不仅仅提到有这个命令,还详细的说明了scan命令中可以使用的参数和作用,例如,根据列名称查询的方法和带LIMIT 、STARTROW的使用方法:
    scan   Scan a table; pass table name and optionally a dictionary ofscanner specifications.  Scanner specifications may include one or moreof  the following: LIMIT, STARTROW, STOPROW, TIMESTAMP, or COLUMNS. If no columns are specified, all columns will be scanned.  To scan allmembers of a column family, leave the qualifier empty as in 'col_family:'.  Examples:
                hbase>scan '.META.'
                hbase>scan '.META.', {COLUMNS => 'info:regioninfo'}
                hbase>scan 't1', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'}

    使用JavaAPIHBase服务器进行操作

    需要下列jar
         hbase-0.20.6.jar
         hadoop-core-0.20.1.jar
         commons-logging-1.1.1.jar
         zookeeper-3.3.0.jar
         log4j-1.2.91.jar

    import org.apache.hadoop.conf.Configuration;

    import org.apache.hadoop.hbase.HBaseConfiguration;

    importorg.apache.hadoop.hbase.HColumnDescriptor;

    importorg.apache.hadoop.hbase.HTableDescriptor;

    import org.apache.hadoop.hbase.KeyValue;

    importorg.apache.hadoop.hbase.client.HBaseAdmin;

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

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

    importorg.apache.hadoop.hbase.client.ResultScanner;

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

    @SuppressWarnings("deprecation")

    public class HbaseCaseTest {

          static HBaseConfiguration cfg = null;

        static {

            Configuration HBASE_CONFIG = new Configuration();

            HBASE_CONFIG.set("hbase.zookeeper.quorum", "retailvm1d.nam.nsroot.net");

            HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");

            cfg = new HBaseConfiguration (HBASE_CONFIG);

        }

        public static void creatTable(String tablename) throws Exception {

            HBaseAdmin admin = new HBaseAdmin(cfg);

            if (admin.tableExists(tablename)){

                System.out.println("table   Exists!!!");

            }

            else{

                HTableDescriptor tableDesc = new HTableDescriptor(tablename);

                tableDesc.addFamily(new HColumnDescriptor("name:"));

                admin.createTable(tableDesc);

                System.out.println("createtable ok .");

            }

       

             

        }

       

        public static void getAllData (String tablename) throws Exception{

             HTable table = new HTable(cfg, tablename);

             Scan s = new Scan();

             ResultScanner ss =table.getScanner(s);

             for(Result r:ss){

                 for(KeyValue kv:r.raw()){

                    System.out.print(new String(kv.getFamily()));

                    System.out.println(new String(kv.getValue()    ));

                 }

             }

        }

       

        public static void  main (String [] agrs) {

         

            try {

                    String tablename="tablename";

                    HbaseCaseTest.creatTable(tablename);

                    HbaseCaseTest.getAllData(tablename);

                }

            catch (Exception e) {

                e.printStackTrace();

            }

            

        }

    }

  • 相关阅读:
    scrapy中selenium的应用
    Django的锁和事务
    redis
    【leetcode】187. Repeated DNA Sequences
    【leetcode】688. Knight Probability in Chessboard
    【leetcode】576. Out of Boundary Paths
    【leetcode】947. Most Stones Removed with Same Row or Column
    【leetcode】948. Bag of Tokens
    【leetcode】946. Validate Stack Sequences
    【leetcode】945. Minimum Increment to Make Array Unique
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3290025.html
Copyright © 2011-2022 走看看