Hbase操作表和Java API

Hbasel列出表
使用list命令可列出所有表

hbase(main):001:0 > list

使用Java API列出表

下面給出的是使用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());
   }

   }
 }

HBase禁用表
hbase禁用表使用 disable命令:

disable '表名'

使用is_disabled命令查看錶是否被禁用,返回true或者false

hbase> is_disabled 'table name'

禁用所有表:disable_all

hbase> disable_all 'r.*'  禁用所有和 r.* 匹配的表

輸出:

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

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

   }
}

HBase啓用表
啓用表:enable

hbase(main):005:0> enable 'emp'
0 row(s) in 0.4580 seconds

判斷表是否被啓用:is_enabled

hbase(main):031:0> is_enabled 'emp'
true
0 row(s) in 0.0440 seconds

使用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 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表描述和修改

hbase(main):002:0> describe 'coupon:coupon_member'
Table coupon:coupon_member is ENABLED                                                                                                                                                                                                                                         
coupon:coupon_member                                                                                                                                                                                                                                                          
COLUMN FAMILIES DESCRIPTION                                                                                                                                                                                                                                                   
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'
}                                                                                                                                                                                                                                                                             
1 row(s) in 0.1830 seconds

修改


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添加一列族

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 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"); 
   }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章