版权说明: 本文章版权归本人及博客园共同全部,转载请标明原文出处(http://www.cnblogs.com/mikevictor07/),如下内容为我的理解,仅供参考。java
1、简介shell
Hbase是在HDFS上开发的面向列的分布式数据库,适用于随机读/写超大规模的数据集(一般这种数据压力传统RDBMS很难承受),能够在廉价的硬件上构成的集群上管理超大规模的稀疏表,而且能够水平扩展。数据库
2、基础概念分布式
一、Hbase把数据存放在表中,表由行列组成,表中的行是排序的(根据ASCII顺序),行键做为表的主键,对表的数据访问须要经过主键或者主键Range,故行键的设计很重要。spa
二、列由“列族”组成(即对列分类),不一样列族的数据一般放在不一样的文件夹里,列族不宜过多,Hbase启动时就打开数据文件,而且一直保持打开状态(Linux 默认一个进程打开最大文件数为1024),不合理的设计将致使异常。定义表时必须定义一个可用的列族,用户可根据须要增长或删除列族,可是必须先disable。设计
三、Hbase为master/slave结构,依赖于zookeeper,master 管理着多个regionServer。code
3、安装(standalone)xml
一、必须安装Java 1.6 或者更高版本。blog
二、可用修改~/.base_profile,export JAVA_HOME指向JAVA安装路径,也可修改conf/hbase-env.sh 中 export JAVA_HOME=/usr/java/jdk1.6.0/排序
三、默认状况下,hbase会使用/tmp/hbase-$USERID做为数据存储目录,有些系统重启会清空/tmp目录,可用经过更改hbase-site.xml来配置数据存储目录,如:
<configuration> <property> <name>hbase.rootdir</name> <value>file:///opt/hbase_data</value> </property> </configuration>
四、修改bin下sh文件执行权限,经过 ./bin/start-hbase.sh便可启动hbase,经过tail -f 监听./log/hbase-root-master-hbase-xx.log来查看启动信息。
4、Hbase shell
使用 ./bin/hbase shell便可进入管理hbase,使用secureCRT会致使没法删除键入的错误命令(backspace delete键没法使用),笔者使用putty(v0.62)可正常使用,下面是一些示例:
下面以建立一个stations的表,这表的行键是stationid, 列包含info.name(名称)、info.countryCode(站点所属国家代号)
一、建立一个表与显示全部表
hbase> create 'stations','info' --建立一个带有info列族的stations表
hbase> list --显示当前全部表
二、录入数据(若是录入同一行同一列则表明更新)
hbase> put 'stations', '1001', 'info:name', 'HAILAR' --录入1001为行键、HAILAR为站点名称的记录
hbase> put 'stations', '1001', 'info:countryCode', 'CH' --CH表明china
hbase> put 'stations', '1002', 'info:name', 'NENJIANG'
hbase> put 'stations', '1002', 'info:countryCode', 'CH'
三、读取、删除数据
hbase> scan 'stations' --读取表中全部数据 hbase> get 'stations','1001' --获得行键为1001的全部列
hbase> get 'stations','1002','info:name' --获得行键为1002的info:name列
hbase> delete 'stations','1001','info:countryCode' --删除1001行的info:countryCode列
四、增长/删除列族
hbase> disable 'stations' hbase> alter 'stations', {NAME=>'data'} --增长data列族,能够录入以data:做为prefix的列 hbase> enable 'stations'
hbase> describe 'stations' --列出表结构
---删除列族
hbase> disable 'stations'
hbase> alter 'stations',{NAME=>'data', METHOD=>'delete'} --删除stations里面的data列族,列族下面的列将被所有删除
五、删除表
hbase> disable 'stations' --须要把表disable hbase> drop 'stations'
经过http://hbase-master:60010/ 可查看hbase状态信息
5、Java 客户端
基本表的管理与访问,下面方法依赖一个静态变量:
private static String host = "192.168.70.41"; --这里是Master 的地址
下面各段代码中有重复部分,关键的在try{}中,可触类旁通。
一、建立表
/** * create 'tableName','colFamily' */ public static void createTable(String tableName, String colFamily) throws Exception{ Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", host); HBaseAdmin hadmin = null; try { hadmin = new HBaseAdmin(config); HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(Bytes.toBytes(tableName))); HColumnDescriptor hcd = new HColumnDescriptor(colFamily); htd.addFamily(hcd); hadmin.createTable(htd); } catch (Exception e) { e.printStackTrace(); } finally { if (hadmin != null) hadmin.close(); } }
二、列出全部表名
/** * list */ public static void list() throws Exception{ Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", host); HBaseAdmin hadmin = null; try { hadmin = new HBaseAdmin(config); HTableDescriptor[] tables = hadmin.listTables(); for (HTableDescriptor table : tables) { System.out.println(new String(table.getName())); } } catch (Exception e) { e.printStackTrace(); } finally { if (hadmin != null) hadmin.close(); } }
三、录入数据
/** * put 'tableName','row','colFamily:qualifier','value' */ public static void put(String tableName,String row, String colFamily, String qualifier, String value) throws Exception { Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", host); HBaseAdmin hadmin = null; HTable table = null; try { table = new HTable(config, tableName); Put put = new Put(Bytes.toBytes(row)); put.add(Bytes.toBytes(colFamily), Bytes.toBytes(qualifier), Bytes.toBytes(value)); table.put(put); } catch (Exception e) { e.printStackTrace(); } finally { if (table != null) table.close(); if (hadmin != null) hadmin.close(); } }
四、获取数据
/** * get 'tableName', 'row', 'colFamily:qualifier' */ public static void get(String tableName,String row, String colFamily, String qualifier) throws Exception { Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", host); HBaseAdmin hadmin = null; HTable table = null; try { table = new HTable(config, tableName); Get get = new Get(Bytes.toBytes(row)); get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(qualifier)); Result result = table.get(get); String value = Bytes.toString(result.getValue(Bytes.toBytes(colFamily), Bytes.toBytes(qualifier))); System.out.println(value); } catch (Exception e) { e.printStackTrace(); } finally { if (table != null) table.close(); if (hadmin != null) hadmin.close(); } }
五、删除数据
/** * delete 'tableName', 'row', 'colFamily:qualifier' */ public static void delete(String tableName,String row, String colFamily, String qualifier) throws Exception { Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", host); HBaseAdmin hadmin = null; HTable table = null; try { table = new HTable(config, tableName); Delete delete = new Delete(Bytes.toBytes(row)); delete.deleteColumn(Bytes.toBytes(colFamily), Bytes.toBytes(qualifier)); table.delete(delete); System.out.println("delete successful"); } catch (Exception e) { e.printStackTrace(); } finally { if (table != null) table.close(); if (hadmin != null) hadmin.close(); } }
六、扫描全表
/** * scan 'tableName' */ public static void scan(String tableName) throws Exception { Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", host); HBaseAdmin hadmin = null; HTable table = null; try { table = new HTable(config, tableName); Scan scan = new Scan(); ResultScanner rc = table.getScanner(scan); for (Result result : rc) { System.out.println(result); } } catch (Exception e) { e.printStackTrace(); } finally { if (table != null) table.close(); if (hadmin != null) hadmin.close(); } }