设为首页 收藏本站
查看: 1852|回复: 0

[经验分享] Hbase 基础

[复制链接]

尚未签到

发表于 2015-12-4 02:51:31 | 显示全部楼层 |阅读模式
  版权说明:  本文章版权归本人及博客园共同所有,转载请标明原文出处(http://www.cnblogs.com/mikevictor07/),以下内容为个人理解,仅供参考。
  
  一、简介
  Hbase是在HDFS上开发的面向列的分布式数据库,适用于随机读/写超大规模的数据集(通常这种数据压力传统RDBMS很难承受),可以在廉价的硬件上构成的集群上管理超大规模的稀疏表,并且可以水平扩展。
  
  二、基础概念
  1、Hbase把数据存放在表中,表由行列组成,表中的行是排序的(根据ASCII顺序),行键作为表的主键,对表的数据访问需要通过主键或者主键Range,故行键的设计很重要
  2、列由“列族”组成(即对列分类),不同列族的数据通常放在不同的文件夹里,列族不宜过多,Hbase启动时就打开数据文件,并且一直保持打开状态(Linux 默认一个进程打开最大文件数为1024),不合理的设计将导致异常。定义表时必须定义一个可用的列族,用户可根据需要增加或删除列族,但是必须先disable。
  3、Hbase为master/slave结构,依赖于zookeeper,master 管理着多个regionServer。
  
  三、安装(standalone)
  1、必须安装Java 1.6 或者更高版本。
  2、可用修改~/.base_profile,export JAVA_HOME指向JAVA安装路径,也可修改conf/hbase-env.sh 中 export JAVA_HOME=/usr/java/jdk1.6.0/
  3、默认情况下,hbase会使用/tmp/hbase-$USERID作为数据存储目录,有些系统重启会清空/tmp目录,可用通过更改hbase-site.xml来配置数据存储目录,如:



<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:///opt/hbase_data</value>
</property>
</configuration>
  4、修改bin下sh文件执行权限,通过 ./bin/start-hbase.sh即可启动hbase,通过tail -f 监听./log/hbase-root-master-hbase-xx.log来查看启动信息。
  
  四、Hbase shell
  使用 ./bin/hbase shell即可进入管理hbase,使用secureCRT会导致无法删除键入的错误命令(backspace delete键无法使用),笔者使用putty(v0.62)可正常使用,下面是一些示例:
  下面以创建一个stations的表,这表的行键是stationid,  列包含info.name(名称)、info.countryCode(站点所属国家代号)
  1、创建一个表与显示所有表



hbase> create 'stations','info'   --创建一个带有info列族的stations表
hbase> list   --显示当前所有表
  2、录入数据(如果录入同一行同一列则代表更新)



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'
  3、读取、删除数据



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列
  4、增加/删除列族



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列族,列族下面的列将被全部删除
  5、删除表



hbase> disable 'stations'   --需要把表disable
hbase> drop 'stations'
  通过http://hbase-master:60010/ 可查看hbase状态信息
  
  五、Java 客户端
  基本表的管理与访问,下面方法依赖一个静态变量:



private static String host = "192.168.70.41"; --这里是Master 的地址
  下面各段代码中有重复部分,关键的在try{}中,可举一反三。
  1、创建表



/**
* 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();
}
}
  
  2、列出所有表名



/**
* 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();
}
}
  
  3、录入数据



/**
* 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();
}
}
  
  4、获取数据



/**
* 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();
}
}
  
  5、删除数据



/**
* 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();
}
}
  6、扫描全表



/**
* 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();
}
}
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-146951-1-1.html 上篇帖子: [Python]判断素数 下篇帖子: set
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表