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"); 
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值