HBase——命令操作

一、Hbase shell

通用命令

status: 提供HBase的状态,例如,服务器的数量。
version: 提供正在使用HBase版本。
table_help: 表引用命令提供帮助。
whoami: 提供有关用户的信息。

数据定义语言

这些是关于HBase在表中操作的命令。

create: 创建一个表。
list: 列出HBase的所有表。
disable: 禁用表。
is_disabled: 验证表是否被禁用。
enable: 启用一个表。
is_enabled: 验证表是否已启用。
describe: 提供了一个表的描述。
alter: 改变一个表。
exists: 验证表是否存在。
drop: 从HBase中删除表。
drop_all: 丢弃在命令中给出匹配“regex”的表。
Java Admin API: 在此之前所有的上述命令,Java提供了一个通过API编程来管理实现DDL功能。在这个org.apache.hadoop.hbase.client包中有HBaseAdmin和HTableDescriptor 这两个重要的类提供DDL功能。

数据操纵语言

put: 把指定列在指定的行中单元格的值在一个特定的表。
get: 取行或单元格的内容。
delete: 删除表中的单元格值。
deleteall: 删除给定行的所有单元格。
scan: 扫描并返回表数据。
count: 计数并返回表中的行的数目。
truncate: 禁用,删除和重新创建一个指定的表。
Java client API: 在此之前所有上述命令,Java提供了一个客户端API来实现DML功能,CRUD(创建检索更新删除)操作更多的是通过编程,在org.apache.hadoop.hbase.client包下。 在此包HTable 的 Put和Get是重要的类。

启动 HBase Shell

要访问HBase shell,必须导航进入到HBase的主文件夹。

cd /usr/localhost/
cd Hbase

可以使用“hbase shell”命令来启动HBase的交互shell,如下图所示。

./bin/hbase shell

如果已成功在系统中安装HBase,那么它会给出 HBase shell 提示符,如下图所示。

HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.94.23, rf42302b28aceaab773b15f234aa8718fff7eea3c, Wed Aug 27
00:54:09 UTC 2014

hbase(main):001:0>

要退出交互shell命令,在任何时候键入 exit 或使用<Ctrl + C>。进一步处理检查shell功能之前,使用 list 命令用于列出所有可用命令。list是用来获取所有HBase 表的列表。首先,验证安装HBase在系统中使用如下所示。

hbase(main):001:0> list

当输入这个命令,它给出下面的输出。

hbase(main):001:0> list

二、HBase Admin API

HBase是用Java编写的,因此它提供Java API和HBase通信。 Java API是与HBase通信的最快方法。下面给出的是引用Java API管理,涵盖用于管理表的任务。

HBaseAdmin类

HBaseAdmin是一个类表示管理。这个类属于org.apache.hadoop.hbase.client包。使用这个类,可以执行管理员任务。使用Connection.getAdmin()方法来获取管理员的实例。
在这里插入图片描述

Descriptor类

这个类包含一个HBase表,如详细信息:

  • 所有列族的描述,
  • 如果表是目录表,
  • 如果表是只读的,
  • 存储的最大尺寸,
  • 当区域分割发生,

构造函数

HTableDescriptor(TableName name)
构造一个表描述符指定TableName对象。

方法及说明

HTableDescriptor addFamily(HColumnDescriptor family)
列家族给定的描述符

三、表操作

创建表

创建一个表,在这里必须指定表名和列族名。在HBase shell中创建表的语法如下所示。

create ‘<table name>’,’<column family>’ 
hbase(main):002:0> create 'emp', 'personal data', ’professional data’

可以验证是否已经创建,使用 list 命令如下所示。在这里,可以看到创建的emp表。

hbase(main):002:0> list

使用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

列出表

hbase(main):001:0 > list

Java API
在类HBaseAdmin中有一个方法叫 listTables(),列出HBase中所有的表的列表。这个方法返回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());
   }
   
   }
 }

禁用表

要删除表或改变其设置,首先需要使用 disable 命令关闭表。使用 enable 命令,可以重新启用它。

hbase(main):025:0> disable 'emp'

is_disabled 这个命令是用来查看表是否被禁用。它的语法如下。

hbase> is_disabled 'table name'

disable_all 此命令用于禁用所有匹配给定正则表达式的表。disable_all命令的语法如下。

hbase> disable_all 'r.*'
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");
   }

   }
}

启用表

启用表的语法:

enable ‘emp’

is_enabled 此命令用于查找表是否被启用。它的语法如下:

hbase> is_enabled 'table name'
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");
   }
   
   }
}

表描述与修改

该命令返回表的说明。它的语法如下:

hbase> describe 'table name'

alter用于更改现有表的命令。使用此命令可以更改列族的单元,设定最大数量和删除表范围运算符,并从表中删除列家族。
下面给出的语法来改变列家族单元的最大数目。

hbase> alter 't1', NAME => 'f1', VERSIONS => 5

在下面的例子中,单元的最大数目设置为5。

hbase(main):003:0> alter 'emp', NAME => 'personal data', VERSIONS => 5

表范围运算符
使用alter,可以设置和删除表范围,运算符,如MAX_FILESIZE,READONLY,MEMSTORE_FLUSHSIZE,DEFERRED_LOG_FLUSH等。
设置只读
下面给出的是语法,是用以设置表为只读。

hbase>alter 't1', READONLY(option)

在下面的例子中,我们已经设置表emp为只读。

hbase(main):006:0> alter 'emp', READONLY

删除表范围运算符
也可以删除表范围运算。下面给出的是语法,从emp表中删除“MAX_FILESIZE”。
hbase> alter 't1', METHOD => 'table_att_unset', NAME => 'MAX_FILESIZE'

删除列族
使用alter,也可以删除列族。下面给出的是使用alter删除列族的语法。
hbase> alter ‘ table name ’, ‘delete’ => ‘ column family ’
使用alter命令删除指定的 professional 列族。

hbase(main):007:0> alter 'employee','delete'=>'professional'

使用Java API添加一列族

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");
   }
}

使用Java API删除列族

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"); 
   }
}

HBase Exists

可以使用exists命令验证表的存在。下面的示例演示了如何使用这个命令。

hbase(main):024:0> exists 'emp'
import java.io.IOException;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class TableExists{

   public static void main(String args[])throws IOException{

   // Instantiating configuration class
   Configuration conf = HBaseConfiguration.create();

   // Instantiating HBaseAdmin class
   HBaseAdmin admin = new HBaseAdmin(conf);

   // Verifying the existance of the table
   boolean bool = admin.tableExists("emp");
   System.out.println( bool);
   }
} 

删除表

用drop命令可以删除表。在删除一个表之前必须先将其禁用。

hbase(main):018:0> disable 'emp'

hbase(main):019:0> drop 'emp'

drop_all这个命令是用来在给出删除匹配“regex”表。它的语法如下:

hbase> drop_all ‘t.*’ 

注意:要删除表,则必须先将其禁用。

import java.io.IOException;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class DeleteTable {

   public static void main(String[] args) throws IOException {

      // Instantiating configuration class
      Configuration conf = HBaseConfiguration.create();

      // Instantiating HBaseAdmin class
      HBaseAdmin admin = new HBaseAdmin(conf);

      // disabling table named emp
      admin.disableTable("emp12");

      // Deleting emp
      admin.deleteTable("emp12");
      System.out.println("Table deleted");
   }
}

Hbase关闭

exit
可以通过键入exit命令退出shell。
hbase(main):021:0> exit
停止HBase
要停止HBase,浏览进入到HBase主文件夹,然后键入以下命令。

./bin/stop-hbase.sh

使用Java API停止HBase

import java.io.IOException;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class ShutDownHbase{

   public static void main(String args[])throws IOException {

      // Instantiating configuration class
      Configuration conf = HBaseConfiguration.create();

      // Instantiating HBaseAdmin class
      HBaseAdmin admin = new HBaseAdmin(conf);

      // Shutting down HBase
      System.out.println("Shutting down hbase");
      admin.shutdown();
   }
}

四、数据操作

HBase客户端API

HBaseConfiguration类

添加 HBase 的配置到配置文件。这个类属于org.apache.hadoop.hbase包。
方法及说明

static org.apache.hadoop.conf.Configuration create()

此方法创建使用HBase的资源配置

HTable类

HTable表示HBase表中HBase的内部类。它用于实现单个HBase表进行通信。这个类属于org.apache.hadoop.hbase.client类。
构造函数

HTable()
HTable(TableName tableName, ClusterConnection connection, ExecutorService pool)

使用此构造方法,可以创建一个对象来访问HBase表。
方法说明
在这里插入图片描述

Put类

此类用于为单个行执行PUT操作。它属于org.apache.hadoop.hbase.client包。
构造函数
在这里插入图片描述
方法
在这里插入图片描述

Get类

此类用于对单行执行get操作。这个类属于org.apache.hadoop.hbase.client包。
构造方法
在这里插入图片描述
方法
在这里插入图片描述

Delete类

这个类用于对单行执行删除操作。要删除整行,实例化一个Delete对象用于删除行。这个类属于org.apache.hadoop.hbase.client包。
构造方法
在这里插入图片描述
方法
在这里插入图片描述

Result类

这个类是用来获取Get或扫描查询的单行结果
构造函数

Result()

使用此构造方法,可以创建无Key Value的有效负载空的结果;如果调用Cells()返回null。

方法
在这里插入图片描述

HBase创建数据

  • put 命令,
  • add() - Put类的方法
  • put() - HTable 类的方法.
    在这里插入图片描述
put ’<table name>’,’row1’,’<colfamily:colname>’,’<value>’
hbase(main):005:0> put 'emp','1','personal data:name','raju'

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class InsertData{

   public static void main(String[] args) throws IOException {

      // Instantiating Configuration class
      Configuration config = HBaseConfiguration.create();

      // Instantiating HTable class
      HTable hTable = new HTable(config, "emp");

      // Instantiating Put class
      // accepts a row name.
      Put p = new Put(Bytes.toBytes("row1")); 

      // adding values using add() method
      // accepts column family name, qualifier/row name ,value
      p.add(Bytes.toBytes("personal"),Bytes.toBytes("name"),Bytes.toBytes("raju"));

      p.add(Bytes.toBytes("personal"),Bytes.toBytes("city"),Bytes.toBytes("hyderabad"));

      p.add(Bytes.toBytes("professional"),Bytes.toBytes("designation"),Bytes.toBytes("manager"));

      p.add(Bytes.toBytes("professional"),Bytes.toBytes("salary"),Bytes.toBytes("50000"));
      
      // Saving the put Instance to the HTable.
      hTable.put(p);
      System.out.println("data inserted");
      
      // closing HTable
      hTable.close();
   }
}

HBase更新数据

可以使用put命令更新现有的单元格值。按照下面的语法,并注明新值,如下图所示。
put ‘table name’,’row ’,'Column family:column name',’new value’
java代码同创建数据

HBase读取数据

get命令和HTable类的get()方法用于从HBase表中读取数据。使用 get 命令,可以同时获取一行数据。它的语法如下:

get ’<table name>’,’row1’
hbase(main):012:0> get 'emp', '1'

读取指定列
下面给出的是语法,使用get方法读取指定列。

hbase>get 'table name', ‘rowid’, {COLUMN => ‘column family:column name ’}
hbase(main):015:0> get 'emp', 'row1', {COLUMN=>'personal:name'}

使用Java API读取数据
从一个HBase表中读取数据,要使用HTable类的get()方法。这种方法需要Get类的一个实例。按照下面从HBase表中检索数据给出的步骤。

  • 第1步:实例化Configuration类
    Configuration类增加了HBase的配置文件到它的对象。使用HbaseConfiguration类的create()方法,如下图所示的配置对象。

    Configuration conf = HbaseConfiguration.create();

  • 第2步:实例化HTable类
    有一类叫HTable,实现在HBase中的Table类。此类用于单个HBase的表进行通信。在这个类实例,它接受配置对象和表名作为参数。实例化HTable类,如下图所示。

    HTable hTable = new HTable(conf, tableName);

  • 第3步:实例化获得类
    可以从HBase表使用HTable类的get()方法检索数据。此方法提取从一个给定的行的单元格。它需要一个 Get 类对象作为参数。创建如下图所示。

    Get get = new Get(toBytes(“row1”));

  • 第4步:读取数据
    当检索数据,可以通过ID得到一个单列,或得到一组行一组行ID,或者扫描整个表或行的子集。
    可以使用Get类的add方法变种检索HBase表中的数据。
    要得到一个特定的列族的所有列,使用下面的方法。

    get.addFamily(personal)

从特定的列族获取指定的列,使用下面的方法。

get.addColumn(personal, name) 
  • 第5步:获取结果
    获取结果通过Get类实例的HTable类的get方法。此方法返回Result类对象,其中保存所请求的结果。下面给出的是get()方法的使用。

    Result result = table.get(get);

  • 第6步:从Result实例读值
    Result 类提供getValue()方法从它的实例读出值。如下图所示,使用它从Result 实例读出值。

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

public class RetriveData{

   public static void main(String[] args) throws IOException, Exception{
   
      // Instantiating Configuration class
      Configuration config = HBaseConfiguration.create();

      // Instantiating HTable class
      HTable table = new HTable(config, "emp");

      // Instantiating Get class
      Get g = new Get(Bytes.toBytes("row1"));

      // Reading the data
      Result result = table.get(g);

      // Reading values from Result class object
      byte [] value = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("name"));

      byte [] value1 = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("city"));

      // Printing the values
      String name = Bytes.toString(value);
      String city = Bytes.toString(value1);
      
      System.out.println("name: " + name + " city: " + city);
   }
}

HBase删除数据

从表删除特定单元格
使用 delete 命令,可以在一个表中删除特定单元格。 delete 命令的语法如下:

delete ‘<table name>’, ‘<row>’, ‘<column name >’, ‘<time stamp>’

下面是一个删除特定单元格和例子。在这里,我们删除salary

hbase(main):006:0> delete 'emp', '1', 'personal data:city'

删除表的所有单元格
使用“deleteall”命令,可以删除一行中所有单元格。下面给出是 deleteall 命令的语法。

deleteall ‘<table name>’, ‘<row>’

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;

public class DeleteData {

   public static void main(String[] args) throws IOException {

      // Instantiating Configuration class
      Configuration conf = HBaseConfiguration.create();

      // Instantiating HTable class
      HTable table = new HTable(conf, "employee");

      // Instantiating Delete class
      Delete delete = new Delete(Bytes.toBytes("row1"));
      delete.deleteColumn(Bytes.toBytes("personal"), Bytes.toBytes("name"));
      delete.deleteFamily(Bytes.toBytes("professional"));

      // deleting the data
      table.delete(delete);

      // closing the HTable object
      table.close();
      System.out.println("data deleted.....");
   }
}

参考:https://www.yiibai.com/hbase/hbase_shell.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值