Hbase 基础 - shell 与 客户端
版权说明: 本文章版权归本人及博客园共同所有,转载请标明原文出处(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', '', '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的所有列
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();
}
}
Hbase 基础 - shell 与 客户端的更多相关文章
- HBASE 基础命令总结
HBASE基础命令总结 一,概述 本文中介绍了hbase的基础命令,作者既有记录总结hbase基础命令的目的还有本着分享的精神,和广大读者一起进步.本文的hbase版本是:HBase 1.2.0-cd ...
- HBASE基础知识总结
HBASE基础知识总结 一,概要说明 文章首先回顾HBase 的数据模型和数据层级结构,对数据的每个层级的作用和架构进行了详细阐述:随后介绍了数据写入和读取的详细流程.先把架构图和流程图来坐镇. 架构 ...
- 大数据存储利器 - Hbase 基础图解
由于疫情原因在家办公,导致很长一段时间没有更新内容,这次终于带来一篇干货,是一篇关于 Hbase架构原理 的分享. Hbase 作为实时存储框架在大数据业务下承担着举足轻重的地位,可以说目前绝大多数大 ...
- HBase基本shell命令
HBase基本shell命令 以下shell命令都是经过测试,正常展示,若有不足,还望指点! 1.创建表 create ‘表名称’,‘列族名称1’,‘列族名称1’create 'test_M_01', ...
- HBASE基础知识
HBASE的集群的搭建HBASE的表设计HBASE的底层存储模型 HBase 是一个高可靠.高性能.面向列.可伸缩的分布式缓存系统.利用HBase 技术可在廉价PC Server上搭建起大规模结构化存 ...
- hbase运行shell时ERROR:org.apache.hadoop.hbase.PleaseHoldException: Master is initializing 的解决办法
这个问题困扰了我一天多的时间,百度搜索的前几条的答案也是很扯淡的,说什么把/etc/hosts文件下的127.0.1.1改成127.0.0.1就行了,我也只能呵呵了.今天早上起得很晚,中午迪哥请我们去 ...
- HBase的shell命令行界面按退格键(Backspace)无法删除问题
在HBase的shell命令行界面输入错误项按"退格键"删除,却怎么也删除不了: 解决办法: 第一步,修改SecureCRT的设置参数: 第二步,按"Ctrl+退格键(B ...
- HBase的Shell命令
1.HBase提供了一个shell的终端给用户交互 2.HBase Shell的DDL操作 (1)先进入HBase的 Shell命令行,即HBASE_HOME/bin/hbase shell …… & ...
- HBase之Table.put客户端流程(续)
上篇博文中已经谈到,有两个流程没有讲到.一个是MetaTableAccessor.getRegionLocations,另外一个是ConnectionImplementation.cacheLocat ...
随机推荐
- 使用 bufferedreader 的好处
简单的说,一次IO操作,读取一个字节也是读取,读取8k个字节也是读取,两者花费时间相差不多.而一次IO的来回操作却要耗费大量时间.好比是一辆大型汽车(设装100人),要去车站接人到公司,接一个人也是接 ...
- 数据结构之数据的next和nextval
KMP算法是模式匹配专用算法. 它是在已知模式串的next或nextval数组的基础上执行的.如果不知道它们二者之一,就没法使用KMP算法,因此我们需要计算它们. KMP算法由两部分组成: 第一部分, ...
- Objective-C 编码风格指南
本文转自:[Objective-C 编码风格指南 | www.samirchen.com][2] ## 背景 保证自己的代码遵循团队统一的编码规范是一个码农的基本节操,能够进入一个有统一编码规范的团队 ...
- 《HelloGitHub》第 15 期
公告 这段时间没怎么写文章,跑去写 https://hellogithub.com 这个网站了,现在已经顺利上线,功能后面会持续迭代. 最后,这个 https://hellogithub.com 网站 ...
- 查询sql表列名
--查询sql 查询表列名Select Name FROM SysColumns Where id=Object_Id('Tab') --查询sql数据库表列名称select name from sy ...
- Sqlserver2005 破解版下载地址
Sqlserver2005 破解版下载地址:http://www.xiaidown.com/soft/from/1583.html
- for循环操作数组
if(!empty($data)){ $arr_length=count($data); //数组的步长 $array_keys_list=array_values($data); //数组的键值 f ...
- 1.如何安装matlab2016a
下载:见网盘 安装教程: 解压安装文件,安装文件为 iso 格式,但是不能通过虚拟光驱安装,需要将 iso 文件用解压软件解压.注意,R2016b_win64_dvd1.iso 和 R2016b_wi ...
- 超好用的memcache管理及可视化监控工具,真方便!
memcache做为主流的缓存数据库之一,广泛在各互联网平台使用,但是大家使用中都知道memcache目前没有一个比较好用的可视化客户端工具,每次都要输入命令进行操作,十分不方便. 而另一款主流缓存 ...
- MyEclipse2014web工程项目直接复制不能访问报错处理方案
在学习web servlet项目中,做一个项目时 , 完成到了某一阶段 实现了部分功能,有必要保存这一项目,当复制这个项目之后发现发布会报错或者不能访问. 其实复制项目主要是为了在以后的学习中如果能顺 ...