Hbase记录-HBase基本操作(一)
HBase创建表
可以使用命令创建一个表,在这里必须指定表名和列族名。在HBase shell中创建表的语法如下所示。
- create ‘<table name>’,’<column family>’
示例
下面给出的是一个表名为emp的样本模式。它有两个列族:“personal data”和“professional data”。
Row key | personal data | professional data |
---|---|---|
在HBase shell创建该表如下所示。
- hbase(main):002:0> create 'emp', 'personal data', ’professional data’
它会给下面的输出。
- 0 row(s) in 1.1300 seconds
- => Hbase::Table - emp
验证创建
可以验证是否已经创建,使用 list 命令如下所示。在这里,可以看到创建的emp表。
- hbase(main):002:0> list
- TABLE
- emp
- 2 row(s) in 0.0340 seconds
使用Java API创建一个表
可以使用HBaseAdmin类的createTable()方法创建表在HBase中。这个类属于org.apache.hadoop.hbase.client 包。下面给出的步骤是来使用Java API创建表在HBase中。
第1步:实例化HBaseAdmin
这个类需要配置对象作为参数,因此初始实例配置类传递此实例给HBaseAdmin。
- Configuration conf = HBaseConfiguration.create();
- HBaseAdmin admin = new HBaseAdmin(conf);
第2步:创建TableDescriptor
HTableDescriptor类是属于org.apache.hadoop.hbase。这个类就像表名和列族的容器一样。
- //creating table descriptor
- HTableDescriptor table = new HTableDescriptor(toBytes("Table name"));
- //creating column family descriptor
- HColumnDescriptor family = new HColumnDescriptor(toBytes("column family"));
- //adding coloumn family to HTable
- table.addFamily(family);
第3步:通过执行管理
使用HBaseAdmin类的createTable()方法,可以在管理模式执行创建的表。
- admin.createTable(table);
下面给出的是完整的程序,通过管理员创建一个表。
- import java.io.IOException;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.HColumnDescriptor;
- import org.apache.hadoop.hbase.HTableDescriptor;
- import org.apache.hadoop.hbase.client.HBaseAdmin;
- import org.apache.hadoop.hbase.TableName;
- import org.apache.hadoop.conf.Configuration;
- public class CreateTable {
- public static void main(String[] args) throws IOException {
- // Instantiating configuration class
- Configuration con = HBaseConfiguration.create();
- // Instantiating HbaseAdmin class
- HBaseAdmin admin = new HBaseAdmin(con);
- // Instantiating table descriptor class
- HTableDescriptor tableDescriptor = new
- TableDescriptor(TableName.valueOf("emp"));
- // Adding column families to table descriptor
- tableDescriptor.addFamily(new HColumnDescriptor("personal"));
- tableDescriptor.addFamily(new HColumnDescriptor("professional"));
- // Execute the table through admin
- admin.createTable(tableDescriptor);
- System.out.println(" Table created ");
- }
- }
编译和执行上述程序如下所示。
- $javac CreateTable.java
- $java CreateTable
下面列出的是输出:
- Table created
HBase列出表
- hbase(main):001:0 > list
当输入这个命令,并在HBase提示符下执行,它会显示HBase中的所有表的列表,如下图所示。
- hbase(main):001:0> list
- TABLE
- emp
在这里,可以看到一个名为表emp。
使用Java API列出表
按照下面给出的步骤来使用Java API从HBase获得表的列表。
第1步
在类HBaseAdmin中有一个方法叫 listTables(),列出HBase中所有的表的列表。这个方法返回HTableDescriptor对象的数组。
- //creating a configuration object
- Configuration conf = HBaseConfiguration.create();
- //Creating HBaseAdmin object
- HBaseAdmin admin = new HBaseAdmin(conf);
- //Getting all the list of tables using HBaseAdmin object
- HTableDescriptor[] tableDescriptor =admin.listTables();
第1步
就可以得到使用HTableDescriptor类长度可变的HTableDescriptor[]数组的长度。从该对象使用getNameAsString()方法获得表的名称。运行'for'循环而获得HBase表的列表。
下面给出的是使用Java API程序列出所有HBase中表的列表。
- import java.io.IOException;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.HTableDescriptor;
- import org.apache.hadoop.hbase.MasterNotRunningException;
- import org.apache.hadoop.hbase.client.HBaseAdmin;
- public class ListTables {
- public static void main(String args[])throws MasterNotRunningException, IOException{
- // Instantiating a configuration class
- Configuration conf = HBaseConfiguration.create();
- // Instantiating HBaseAdmin class
- HBaseAdmin admin = new HBaseAdmin(conf);
- // Getting all the list of tables using HBaseAdmin object
- HTableDescriptor[] tableDescriptor =admin.listTables();
- // printing all the table names.
- for (int i=0; i<tableDescriptor.length;i++ ){
- System.out.println(tableDescriptor[i].getNameAsString());
- }
- }
- }
编译和执行上述程序如下所示。
- $javac ListTables.java
- $java ListTables
下面列出的是输出:
- User
- emp
HBase禁用表
要删除表或改变其设置,首先需要使用 disable 命令关闭表。使用 enable 命令,可以重新启用它。
下面给出的语法是用来禁用一个表:
- disable ‘emp’
下面给出的是一个例子,说明如何禁用表。
- hbase(main):025:0> disable 'emp'
- 0 row(s) in 1.2760 seconds
验证
禁用表之后,仍然可以通过 list 和exists命令查看到。无法扫描到它存在,它会给下面的错误。
- hbase(main):028:0> scan 'emp'
- ROW COLUMN+CELL
- ERROR: emp is disabled.
is_disabled
这个命令是用来查看表是否被禁用。它的语法如下。
- hbase> is_disabled 'table name'
下面的例子验证表名为emp是否被禁用。如果禁用,它会返回true,如果没有,它会返回false。
- hbase(main):031:0> is_disabled 'emp'
- true
- 0 row(s) in 0.0440 seconds
disable_all
此命令用于禁用所有匹配给定正则表达式的表。disable_all命令的语法如下。
- hbase> disable_all 'r.*'
假设有5个表在HBase,即raja, rajani, rajendra, rajesh 和 raju。下面的代码将禁用所有以 raj 开始的表。
- hbase(main):002:0> disable_all 'raj.*'
- raja
- rajani
- rajendra
- rajesh
- raju
- Disable the above 5 tables (y/n)?
- y
- 5 tables successfully disabled
禁用表使用Java API
要验证一个表是否被禁用,使用isTableDisabled()方法和disableTable()方法禁用一个表。这些方法属于HBaseAdmin类。按照下面给出禁用表中的步骤。
第1步
HBaseAdmin类的实例如下所示。
- // Creating configuration object
- Configuration conf = HBaseConfiguration.create();
- // Creating HBaseAdmin object
- HBaseAdmin admin = new HBaseAdmin(conf);
第2步
使用isTableDisabled()方法验证表是否被禁用,如下图所示。
- Boolean b = admin.isTableDisabled("emp");
第3步
如果表未禁用,禁用它,如下图所示。
- if(!b){
- admin.disableTable("emp");
- System.out.println("Table disabled");
- }
下面给出的是完整的程序,以验证表是否被禁用;如果没有,那么如何禁用它?
- import java.io.IOException;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.MasterNotRunningException;
- import org.apache.hadoop.hbase.client.HBaseAdmin;
- public class DisableTable{
- public static void main(String args[]) throws MasterNotRunningException, IOException{
- // Instantiating configuration class
- Configuration conf = HBaseConfiguration.create();
- // Instantiating HBaseAdmin class
- HBaseAdmin admin = new HBaseAdmin(conf);
- // Verifying weather the table is disabled
- Boolean bool = admin.isTableDisabled("emp");
- System.out.println(bool);
- // Disabling the table using HBaseAdmin object
- if(!bool){
- admin.disableTable("emp");
- System.out.println("Table disabled");
- }
- }
- }
编译和执行上述程序如下所示。
- $javac DisableTable.java
- $java DsiableTable
下面列出的是输出:
- false
- Table disabled
HBase启用表
启用表的语法:
- enable ‘emp’
给出下面是一个例子,使一个表启用。
- hbase(main):005:0> enable 'emp'
- 0 row(s) in 0.4580 seconds
验证
启用表之后,扫描。如果能看到的模式,那么证明表已成功启用。
- hbase(main):006:0> scan 'emp'
- ROW COLUMN+CELL
- 1 column=personal data:city, timestamp=1417516501, value=hyderabad
- 1 column=personal data:name, timestamp=1417525058, value=ramu
- 1 column=professional data:designation, timestamp=1417532601, value=manager
- 1 column=professional data:salary, timestamp=1417524244109, value=50000
- 2 column=personal data:city, timestamp=1417524574905, value=chennai
- 2 column=personal data:name, timestamp=1417524556125, value=ravi
- 2 column=professional data:designation, timestamp=14175292204, value=sr:engg
- 2 column=professional data:salary, timestamp=1417524604221, value=30000
- 3 column=personal data:city, timestamp=1417524681780, value=delhi
- 3 column=personal data:name, timestamp=1417524672067, value=rajesh
- 3 column=professional data:designation, timestamp=14175246987, value=jr:engg
- 3 column=professional data:salary, timestamp=1417524702514, value=25000
- 3 row(s) in 0.0400 seconds
is_enabled
此命令用于查找表是否被启用。它的语法如下:
- hbase> is_enabled 'table name'
下面的代码验证表emp是否启用。如果启用,它将返回true,如果没有,它会返回false。
- hbase(main):031:0> is_enabled 'emp'
- true
- 0 row(s) in 0.0440 seconds
使用Java API启用表
要验证一个表是否被启用,使用isTableEnabled()方法;并且使用enableTable()方法使一个表启用。这些方法属于HBaseAdmin类。按照下面给出启用表的步骤。
第1步
HBaseAdmin类的实例如下所示。
- // Creating configuration object
- Configuration conf = HBaseConfiguration.create();
- // Creating HBaseAdmin object
- HBaseAdmin admin = new HBaseAdmin(conf);
第2步
使用isTableEnabled()方法验证表是否被启用,如下所示。
- Boolean bool=admin.isTableEnabled("emp");
第3步
如果表未禁用,那么禁用它,如下图所示
- if(!bool){
- admin.enableTable("emp");
- System.out.println("Table enabled");
- }
下面给出的是完整的程序,以验证表是否已启用,如果它不是,那么启用它。
- import java.io.IOException;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.MasterNotRunningException;
- import org.apache.hadoop.hbase.client.HBaseAdmin;
- public class EnableTable{
- public static void main(String args[]) throws MasterNotRunningException, IOException{
- // Instantiating configuration class
- Configuration conf = HBaseConfiguration.create();
- // Instantiating HBaseAdmin class
- HBaseAdmin admin = new HBaseAdmin(conf);
- // Verifying weather the table is disabled
- Boolean bool = admin.isTableEnabled("emp");
- System.out.println(bool);
- // Disabling the table using HBaseAdmin object
- if(!bool){
- admin.enableTable("emp");
- System.out.println("Table Enabled");
- }
- }
- }
编译和执行上述程序如下所示。
- $javac EnableTable.java
- $java EnableTable
下面列出的是输出:
- false
- Table Enabled
HBase表描述和修改
描述
该命令返回表的说明。它的语法如下:
- hbase> describe 'table name'
下面给出的是对emp表的 describe 命令的输出。
- hbase(main):006:0> describe 'emp'
- DESCRIPTION
- ENABLED
- 'emp', {NAME => 'READONLY', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER
- => 'ROW', REPLICATION_SCOPE => '0', COMPRESSION => 'NONE', VERSIONS =>
- '1', TTL true
- => 'FOREVER', MIN_VERSIONS => '0', KEEP_DELETED_CELLS => 'false',
- BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}, {NAME
- => 'personal
- data', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW',
- REPLICATION_SCOPE => '0', VERSIONS => '5', COMPRESSION => 'NONE',
- MIN_VERSIONS => '0', TTL
- => 'FOREVER', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536',
- IN_MEMORY => 'false', BLOCKCACHE => 'true'}, {NAME => 'professional
- data', DATA_BLO
- CK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0',
- VERSIONS => '1', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL =>
- 'FOREVER', K
- EEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY =>
- 'false', BLOCKCACHE => 'true'}, {NAME => 'table_att_unset',
- DATA_BLOCK_ENCODING => 'NO
- NE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', COMPRESSION =>
- 'NONE', VERSIONS => '1', TTL => 'FOREVER', MIN_VERSIONS => '0',
- KEEP_DELETED_CELLS
- => 'false', BLOCKSIZE => '6
修改
alter用于更改现有表的命令。使用此命令可以更改列族的单元,设定最大数量和删除表范围运算符,并从表中删除列家族。
更改列族单元格的最大数目
下面给出的语法来改变列家族单元的最大数目。
- hbase> alter 't1', NAME => 'f1', VERSIONS => 5
在下面的例子中,单元的最大数目设置为5。
- hbase(main):003:0> alter 'emp', NAME => 'personal data', VERSIONS => 5
- Updating all regions with the new schema...
- 0/1 regions updated.
- 1/1 regions updated.
- Done.
- 0 row(s) in 2.3050 seconds
表范围运算符
使用alter,可以设置和删除表范围,运算符,如MAX_FILESIZE,READONLY,MEMSTORE_FLUSHSIZE,DEFERRED_LOG_FLUSH等。
设置只读
下面给出的是语法,是用以设置表为只读。
- hbase>alter 't1', READONLY(option)
在下面的例子中,我们已经设置表emp为只读。
- hbase(main):006:0> alter 'emp', READONLY
- Updating all regions with the new schema...
- 0/1 regions updated.
- 1/1 regions updated.
- Done.
- 0 row(s) in 2.2140 seconds
删除表范围运算符
也可以删除表范围运算。下面给出的是语法,从emp表中删除“MAX_FILESIZE”。
- hbase> alter 't1', METHOD => 'table_att_unset', NAME => 'MAX_FILESIZE'
删除列族
使用alter,也可以删除列族。下面给出的是使用alter删除列族的语法。
- hbase> alter ‘ table name ’, ‘delete’ => ‘ column family ’
下面给出的是一个例子,从“emp”表中删除列族。
假设在HBase中有一个employee表。它包含以下数据:
- hbase(main):006:0> scan 'employee'
- ROW COLUMN+CELL
- row1 column=personal:city, timestamp=1418193767, value=hyderabad
- row1 column=personal:name, timestamp=1418193806767, value=raju
- row1 column=professional:designation, timestamp=1418193767, value=manager
- row1 column=professional:salary, timestamp=1418193806767, value=50000
- 1 row(s) in 0.0160 seconds
现在使用alter命令删除指定的 professional 列族。
- hbase(main):007:0> alter 'employee','delete'=>'professional'
- Updating all regions with the new schema...
- 0/1 regions updated.
- 1/1 regions updated.
- Done.
- 0 row(s) in 2.2380 seconds
现在验证该表中变更后的数据。观察列族“professional”也没有了,因为前面已经被删除了。
- hbase(main):003:0> scan 'employee'
- ROW COLUMN+CELL
- row1 column=personal:city, timestamp=14181936767, value=hyderabad
- row1 column=personal:name, timestamp=1418193806767, value=raju
- 1 row(s) in 0.0830 seconds
使用Java API添加一列族
可以使用HBAseAdmin类的addColumn方法添加一列家族的表。按照下面给出的步骤将一个列族添加到表中。
第1步
实例化HBaseAdmin类。
- // Instantiating configuration object
- Configuration conf = HBaseConfiguration.create();
- // Instantiating HBaseAdmin class
- HBaseAdmin admin = new HBaseAdmin(conf);
第2步
addColumn()方法需要一个表名和一个HColumnDescriptorclass对象。因此需要实例化HColumnDescriptor类。 HColumnDescriptor依次构造函数需要一个列族名称用于添加。在这里加入了一个名为“contactDetails”到“employee”表的列族。
- // Instantiating columnDescriptor object
- HColumnDescriptor columnDescriptor = new
- HColumnDescriptor("contactDetails");
第3步
使用addColumn方法添加列族。通过表名和HColumnDescriptor类对象作为这个方法的参数。
- // Adding column family
- admin.addColumn("employee", new HColumnDescriptor("columnDescriptor"));
下面给出的是一个完整的程序,用于添加一列族到现有的表。
- import java.io.IOException;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.HColumnDescriptor;
- import org.apache.hadoop.hbase.MasterNotRunningException;
- import org.apache.hadoop.hbase.client.HBaseAdmin;
- public class AddColoumn{
- public static void main(String args[]) throws MasterNotRunningException, IOException{
- // Instantiating configuration class.
- Configuration conf = HBaseConfiguration.create();
- // Instantiating HBaseAdmin class.
- HBaseAdmin admin = new HBaseAdmin(conf);
- // Instantiating columnDescriptor class
- HColumnDescriptor columnDescriptor = new HColumnDescriptor("contactDetails");
- // Adding column family
- admin.addColumn("employee", columnDescriptor);
- System.out.println("coloumn added");
- }
- }
编译和执行上述程序,如下所示
- $javac AddColumn.java
- $java AddColumn
上述编译只有已经设置“.bashrc”中的类路径。如果还没有,请按照下面编译给出.java文件的程序。
- //if "/home/home/hadoop/hbase " is your Hbase home folder then.
- $javac -cp /home/hadoop/hbase/lib/*: Demo.java
如果一切顺利,它会生成以下的输出:
- column added
使用Java API删除列族
可以使用HBAseAdmin类的deleteColumn()方法删除列族。按照下面给出的步骤添加一个列族到表中。
第1步
实例化HBaseAdmin类。
- // Instantiating configuration object
- Configuration conf = HBaseConfiguration.create();
- // Instantiating HBaseAdmin class
- HBaseAdmin admin = new HBaseAdmin(conf);
第2步
使用deleteColumn()方法添加列族。传递表名和列族名作为这个方法的参数。
- // Deleting column family
- admin.deleteColumn("employee", "contactDetails");
下面给出的是从现有表中删除列族的完整的程序。
- import java.io.IOException;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.MasterNotRunningException;
- import org.apache.hadoop.hbase.client.HBaseAdmin;
- public class DeleteColoumn{
- public static void main(String args[]) throws MasterNotRunningException, IOException{
- // Instantiating configuration class.
- Configuration conf = HBaseConfiguration.create();
- // Instantiating HBaseAdmin class.
- HBaseAdmin admin = new HBaseAdmin(conf);
- // Deleting a column family
- admin.deleteColumn("employee","contactDetails");
- System.out.println("coloumn deleted");
- }
- }
编译和执行上述程序如下所示。
- $javac DeleteColumn.java
- $java DeleteColumn
下面列出的是输出:
- column deleted
Hbase记录-HBase基本操作(一)的更多相关文章
- Hbase记录-Hbase基础概念
HBase是什么? HBase是建立在Hadoop文件系统之上的分布式面向列的数据库.它是一个开源项目,是横向扩展的. HBase是一个数据模型,类似于谷歌的大表设计,可以提供快速随机访问海量结构化数 ...
- Hbase记录-Hbase shell使用
HBase Shell HBase包含可以与HBase进行通信的Shell. HBase使用Hadoop文件系统来存储数据.它拥有一个主服务器和区域服务器.数据存储将在区域(表)的形式.这些区域被分割 ...
- Hbase记录-HBase基本操作(二)
HBase Exists 可以使用exists命令验证表的存在.下面的示例演示了如何使用这个命令. hbase(main):024:0> exists 'emp' Table emp doe ...
- Hbase记录-HBase性能优化指南
垃圾回收优化当region服务器处理大量的写入负载时,繁重的任务会迫使JRE默认的内存分配策略无法保证程序的稳定性 所以我们可能需要对region服务器的垃圾回收机制进行一些参数调整(因为master ...
- Hbase记录-Hbase介绍
Hbase是什么 HBase是一种构建在HDFS之上的分布式.面向列的存储系统,适用于实时读写.随机访问超大规模数据的集群. HBase的特点 大:一个表可以有上亿行,上百万列. 面向列:面向列表(簇 ...
- Hbase记录-Hbase shell使用命令
1.进入hbase shell 执行./bin/hbase shell 2.进入后,help 帮助信息,如可以使用help 'create' 3.创建表:create 'test','cf' 表 ...
- Hbase记录-hbase部署
#版本支持 #官网下载二进制包,解压到/usr/app下,配置/etc/profile: export HBASE_HOME=/usr/app/hbase export PATH=$HBASE_HOM ...
- Hbase记录-Hbase配置项
hbase.tmp.dir:本地文件系统的临时目录,默认是java.io.tmpdir/hbase−java.io.tmpdir/hbase−{user.name}: hbase.rootdir:hb ...
- Hbase记录-HBase扫描/计数/权限
HBase扫描 scan 命令用于查看HTable数据.使用 scan 命令可以得到表中的数据.它的语法如下: scan ‘<table name>’ 下面的示例演示了如何使用scan ...
随机推荐
- 配置wbepack
proxyTable:{ //反向代理 先建立连接 '/sexLady':{ target:url//请求地址 暗号:'/sexLady ' changeOrigin:true ,//类似baseUr ...
- Oracle的一般监听问题解决
1. 无监听的解决办法: Windows的情况下重启之后或者是一些异常状态时会造成服务没有正常启动起来, 解决办法: 打开服务 方法1 任务管理器-服务界面 或者是 运行-services.msc 打 ...
- U9财务体系
- python threading模块使用 以及python多线程操作的实践(使用Queue队列模块)
今天花了近乎一天的时间研究python关于多线程的问题,查看了大量源码 自己也实践了一个生产消费者模型,所以把一天的收获总结一下. 由于GIL(Global Interpreter Lock)锁的关系 ...
- codeforces445A
DZY Loves Chessboard CodeForces - 445A DZY 喜欢棋盘,他很享受棋盘上的游戏. 他有一个 n 行和 m 列的棋盘.棋盘上的某些单元格是坏的位置,其他的是好的位置 ...
- android studio marvin 配置
buildscript { repositories { maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} } } ...
- Ubuntu 16.04安装Tomcat 8
此篇为http://www.cnblogs.com/EasonJim/p/7139275.html的分支页. 前提:必须正确安装JDK. 一.通过二进制包(tar.gz)安装 下载: https:// ...
- ajax 提交字符串到后台 反序列化
MVC后台 或者 Webapi 都可以使用此方式 前台 @using (Html.BeginForm("Test","Test")) { <input t ...
- Hibernate事务以及一级缓存02
一. Hibernate中的事务 1. 事务的回顾 1.1 什么是事务(Transaction)(面试重点) 是并发控制的单元,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的 ...
- MT【10】和三次有关的一个因式分解
解答: 评:1此处因式分解也可以看成关于$a$的函数$f(a)$利用多项式有理根的有关知识得到 2.此处我们可以得到关于$\Delta ABC$的余弦的一个不等式$cosA+cosB+cosC> ...