您好,欢迎访问一九零五行业门户网

HBase入门篇2

本篇文章讲述用hbase shell命令 和 hbase java api 对hbase 服务器 进行操作。在此之前需要对hbase的总体上有个大概的了解。比如说hbase服务器内部由哪些主要部件构成?hbase的内部工作原理是什么?我想学习任何一项知识、技术的态度不能只是知道如何使用,
本篇文章讲述用hbase shell命令 和 hbase java api 对hbase 服务器 进行操作。在此之前需要对hbase的总体上有个大概的了解。比如说hbase服务器内部由哪些主要部件构成?hbase的内部工作原理是什么?我想学习任何一项知识、技术的态度不能只是知道如何使用,对产品的内部构建一点都不去关心,那样出了问题,很难让你很快的找到答案,甚至我们希望最后能对该项技术的领悟出自己的心得,为我所用,借鉴该项技术其中的设计思想创造出自己的解决方案,更灵活的去应对多变的计算场景与架构设计。以我目前的对hbase的了解还不够深入,将来不断的学习,我会把我所知道的点滴分享到这个blog上。
     先来看一下读取一行记录hbase是如何进行工作的,首先hbase client端会连接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的一些基本操作命令,我列出了几个常用的hbase shell命令,如下:
名称
命令表达式
创建表 create '表名称', '列名称1','列名称2','列名称n'
添加记录       put '表名称', '行名称', '列名称:', '值'
查看记录 get '表名称', '行名称'
查看表中的记录总数 count  '表名称'
删除记录 delete  '表名' ,'行名称' , '列名称'
删除一张表 先要屏蔽该表,才能对该表进行删除,第一步 disable '表名称' 第二步  drop '表名称'
查看所有记录 scan 表名称  
查看某个表某个列中所有数据 scan 表名称 , ['列名称:']
更新记录  就是重写一遍进行覆盖
如果你是一个新手队hbase的一些命令还不算非常熟悉的话,你可以进入 hbase 的shell 模式中你可以输入 help 命令查看到你可以执行的命令和对该命令的说明,例如对scan这个命令,help中不仅仅提到有这个命令,还详细的说明了scan命令中可以使用的参数和作用,例如,根据列名称查询的方法和带limit 、startrow的使用方法:
scan   scan a table; pass table name and optionally a dictionary of scanner specifications.  scanner specifications may include one or more of  the following: limit, startrow, stoprow, timestamp, or columns.  if no columns are specified, all columns will be scanned.  to scan all members 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'}
使用java api对hbase服务器进行操作
需要下列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;
import org.apache.hadoop.hbase.hcolumndescriptor;
import org.apache.hadoop.hbase.htabledescriptor;
import org.apache.hadoop.hbase.keyvalue;
import org.apache.hadoop.hbase.client.hbaseadmin;
import org.apache.hadoop.hbase.client.htable;
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.io.batchupdate;
@suppresswarnings(deprecation)
public class hbasetestcase {
static hbaseconfiguration cfg = null;
    static {
        configuration hbase_config = new configuration();
        hbase_config.set(hbase.zookeeper.quorum, 192.168.50.216);
        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(create table ok .);
        }
}
/**
     * 添加一条数据
     */
    public static void adddata (string tablename) throws exception{
         htable table = new htable(cfg, tablename);
             batchupdate update = new batchupdate(huangyi);  
             update.put(name:java, http://www.javabloger.com.getbytes());  
             table.commit(update);  
         system.out.println(add data 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.getcolumn()));
                system.out.println(new string(kv.getvalue()    ));
             }
         }
    }
public static void  main (string [] agrs) {
        try {
                string tablename=tablename;
                hbasetestcase.creattable(tablename);
                hbasetestcase.adddata(tablename);
                hbasetestcase.getalldata(tablename);
            } 
        catch (exception e) {
            e.printstacktrace();
        }
}
}
其它类似信息

推荐信息