• Hbase记录-HBase基本操作(一)


    HBase创建表

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

    create ‘<table name>’,’<column family>’

    示例

    下面给出的是一个表名为emp的样本模式。它有两个列族:“personal data”和“professional data”。

    Row keypersonal dataprofessional 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列出表

    list 是用来列出HBase中所有表的命令。下面给出了 list 命令的语法。

    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

  • 相关阅读:
    「CF1380G」 Circular Dungeon
    「CF1208G」 Polygons
    P4827「国家集训队」 Crash 的文明世界
    「CF85E」 Guard Towers
    「BZOJ 2956」模积和
    「HEOI2016/TJOI2016」排序
    CF277E Binary Tree on Plane
    「SDOI2016」数字配对
    HNOI2020「Elegy」
    CSP-S2019「Symphony」
  • 原文地址:https://www.cnblogs.com/xinfang520/p/7717247.html
Copyright © 2020-2023  润新知