HBase命令行和Java实现

来源:互联网 发布:mac市场增长率 编辑:程序博客网 时间:2024/06/05 08:15

1.先搭建Hadoop HA集群(包含ZooKeeper搭建),请看这:Hadoop HA高可用集群搭建(2.7.2)

2.我只有五台机器,所以是这么规划的:drguo1/2 HMaster drguo3/4/5 HRegionServer

3.先在drguo1上安装配置好之后,再拷给2/3/4/5。

4.正式开始

4.1去官网下载HBase,最低要0.96版本,之前版本不支持Hadoop HA

4.2加压后配置环境变量

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. guo@drguo1:~$ sudo gedit /etc/profile  
我把我的profile文件贴出来给大家看看

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. # /etc/profile: system-wide .profile file for the Bourne shell (sh(1))  
  2. # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).  
  3.   
  4. if [ "$PS1" ]; then  
  5.   if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then  
  6.     # The file bash.bashrc already sets the default PS1.  
  7.     # PS1='\h:\w\$ '  
  8.     if [ -f /etc/bash.bashrc ]; then  
  9.       . /etc/bash.bashrc  
  10.     fi  
  11.   else  
  12.     if [ "`id -u`" -eq 0 ]; then  
  13.       PS1='# '  
  14.     else  
  15.       PS1='$ '  
  16.     fi  
  17.   fi  
  18. fi  
  19.   
  20. # The default umask is now handled by pam_umask.  
  21. # See pam_umask(8) and /etc/login.defs.  
  22.   
  23. if [ -d /etc/profile.d ]; then  
  24.   for i in /etc/profile.d/*.sh; do  
  25.     if [ -r $i ]; then  
  26.       . $i  
  27.     fi  
  28.   done  
  29.   unset i  
  30. fi  
  31. #java  
  32. export JAVA_HOME=/opt/Java/jdk1.8.0_73  
  33. export JRE_HOME=/opt/Java/jdk1.8.0_73/jre  
  34. export CLASSPATH=$JAVA_HOME/lib  
  35. export PATH=:$PATH:$JAVA_HOME/bin:$JRE_HOME/bin  
  36. #hadoop  
  37. export HADOOP_HOME=/opt/Hadoop/hadoop-2.7.2  
  38. export PATH=$PATH:$HADOOP_HOME/sbin  
  39. export PATH=$PATH:$HADOOP_HOME/bin  
  40. #zookeeper  
  41. export ZOOKEEPER_HOME=/opt/zookeeper-3.4.8  
  42. export PATH=$PATH:$ZOOKEEPER_HOME/bin  
  43. #hive  
  44. export HIVE_HOME=/opt/apache-hive-2.0.0-bin  
  45. export PATH=$PATH:$HIVE_HOME/bin  
  46. #hbase  
  47. export HIVE_HOME=/opt/hbase  
  48. export PATH=$PATH:$HIVE_HOME/bin  
4.3修改/opt/hbase/conf/下的hbase-env.sh

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. guo@drguo1:/opt/hbase/conf$ gedit hbase-env.sh  
hbase-env.sh(修改两处)

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. # The java implementation to use.  Java 1.6 required.  
  2. export JAVA_HOME=/opt/Java/jdk1.8.0_73  
  3. # Tell HBase whether it should manage it's own instance of Zookeeper or not.不让HBase管理zookeeper,修改为false  
  4. export HBASE_MANAGES_ZK=false  

4.4修改/opt/hbase/conf/下的hbase-site.xml

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. guo@drguo1:/opt/hbase/conf$ gedit hbase-site.xml  
hbase-site.xml

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0"?>  
  2. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>  
  3.   
  4. <configuration>  
  5. <property>  
  6.         <name>hbase.rootdir</name>  
  7.         <value>hdfs://ns1/hbase</value>  
  8. </property>  
  9. <property>  
  10.         <name>hbase.cluster.distributed</name>  
  11.         <value>true</value>  
  12. </property>  
  13. <property>  
  14.         <name>hbase.zookeeper.quorum</name>  
  15.         <value>drguo3:2181,drguo4:2181,drguo5:2181</value>  
  16. </property>  
  17. </configuration>  

4.5将/opt/Hadoop/hadoop-2.7.2/etc/hadoop下的core-site.xml hdfs-site.xml拷到/opt/hbase/conf/下

4.6将hbase整个文件夹拷给drguo2/3/4/5(拷之前可以把文档删了,这样会快点,好像是hbase下的doc目录,我已经删了,记不清了)

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. guo@drguo1:~$ scp -r /opt/hbase/ drguo2:/opt/  
  2. guo@drguo1:~$ scp -r /opt/hbase/ drguo3:/opt/  
  3. guo@drguo1:~$ scp -r /opt/hbase/ drguo4:/opt/  
  4. guo@drguo1:~$ scp -r /opt/hbase/ drguo5:/opt/  
4.7在启动完zookeeper集群和hadoop集群后启动hbase集群

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. guo@drguo1:~$ start-hbase.sh   
  2. starting master, logging to /opt/hbase/bin/../logs/hbase-guo-master-drguo1.out  
  3. drguo5: starting regionserver, logging to /opt/hbase/bin/../logs/hbase-guo-regionserver-drguo5.out  
  4. drguo3: starting regionserver, logging to /opt/hbase/bin/../logs/hbase-guo-regionserver-drguo3.out  
  5. drguo4: starting regionserver, logging to /opt/hbase/bin/../logs/hbase-guo-regionserver-drguo4.out  
  6. guo@drguo1:~$ jps  
  7. 3184 DFSZKFailoverController  
  8. 4211 Jps  
  9. 4100 HMaster  
  10. 2796 NameNode  
  11. 3292 ResourceManager  
4.8在drguo2需要单独启动HMaster,当然你也要再配一遍环境变量

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. guo@drguo2:~$ hbase-daemon.sh start master  
4.9查看所有节点的jps,看是否正常启动,也可打开浏览器输入drguo1:60010查看

5.大功告成!


6.HBase Shell简单使用演示(真不是人用的)

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. guo@drguo1:~$ hbase shell  
  2. 2016-03-27 18:07:31,663 INFO  [main] Configuration.deprecation: hadoop.native.lib is deprecated. Instead, use io.native.lib.available  
  3. HBase Shell; enter 'help<RETURN>' for list of supported commands.  
  4. Type "exit<RETURN>" to leave the HBase Shell  
  5. Version 0.96.2-hadoop2, r1581096, Mon Mar 24 16:03:18 PDT 2014  
  6.   
  7. hbase(main):001:0> help  
  8. HBase Shell, version 0.96.2-hadoop2, r1581096, Mon Mar 24 16:03:18 PDT 2014  
  9. Type 'help "COMMAND"', (e.g. 'help "get"' -- the quotes are necessary) for help on a specific command.  
  10. Commands are grouped. Type 'help "COMMAND_GROUP"', (e.g. 'help "general"') for help on a command group.  
  11.   
  12. COMMAND GROUPS:  
  13.   Group name: general  
  14.   Commands: status, table_help, version, whoami  
  15.   
  16.   Group name: ddl  
  17.   Commands: alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list, show_filters  
  18.   
  19.   Group name: namespace  
  20.   Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables  
  21.   
  22.   Group name: dml  
  23.   Commands: count, delete, deleteall, get, get_counter, incr, put, scan, truncate, truncate_preserve  
  24.   
  25.   Group name: tools  
  26.   Commands: assign, balance_switch, balancer, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, close_region, compact, flush, hlog_roll, major_compact, merge_region, move, split, trace, unassign, zk_dump  
  27.   
  28.   Group name: replication  
  29.   Commands: add_peer, disable_peer, enable_peer, list_peers, list_replicated_tables, remove_peer  
  30.   
  31.   Group name: snapshot  
  32.   Commands: clone_snapshot, delete_snapshot, list_snapshots, rename_snapshot, restore_snapshot, snapshot  
  33.   
  34.   Group name: security  
  35.   Commands: grant, revoke, user_permission  
  36.   
  37. SHELL USAGE:  
  38. Quote all names in HBase Shell such as table and column names.  Commas delimit  
  39. command parameters.  Type <RETURN> after entering a command to run it.  
  40. Dictionaries of configuration used in the creation and alteration of tables are  
  41. Ruby Hashes. They look like this:  
  42.   
  43.   {'key1' => 'value1', 'key2' => 'value2', ...}  
  44.   
  45. and are opened and closed with curley-braces.  Key/values are delimited by the  
  46. '=>' character combination.  Usually keys are predefined constants such as  
  47. NAME, VERSIONS, COMPRESSION, etc.  Constants do not need to be quoted.  Type  
  48. 'Object.constants' to see a (messy) list of all constants in the environment.  
  49.   
  50. If you are using binary keys or values and need to enter them in the shell, use  
  51. double-quote'd hexadecimal representation. For example:  
  52.   
  53.   hbase> get 't1', "key\x03\x3f\xcd"  
  54.   hbase> get 't1', "key\003\023\011"  
  55.   hbase> put 't1', "test\xef\xff", 'f1:', "\x01\x33\x40"  
  56.   
  57. The HBase shell is the (J)Ruby IRB with the above HBase-specific commands added.  
  58. For more on the HBase Shell, see http://hbase.apache.org/docs/current/book.html  
建表(必须按它给的范例写!该大写大写!)

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. hbase(main):007:0> create 'phone', {NAME => 'base_info', VERSIONS => 5}, {NAME => 'extra_info'}  
  2. 0 row(s) in 8.2020 seconds  
  3.   
  4. => Hbase::Table - phone  
  5. hbase(main):008:0> describe 'phone'  
  6. DESCRIPTION                                               ENABLED                         
  7.  'phone', {NAME => 'base_info', BLOOMFILTER => 'ROW', VER true                            
  8.  SIONS => '5', IN_MEMORY => 'false', KEEP_DELETED_CELLS =                                 
  9.  > 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => '214748                                 
  10.  3647', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCK                                 
  11.  CACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE                                 
  12.   => '0'}, {NAME => 'extra_info', BLOOMFILTER => 'ROW', V                                 
  13.  ERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS                                 
  14.   => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => '2147                                 
  15.  483647', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLO                                 
  16.  CKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCO                                 
  17.  PE => '0'}                                                                               
  18. 1 row(s) in 0.3660 seconds  
传数据

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. hbase(main):009:0> put 'phone','001','base_info:brand','oneplus'   
  2. 0 row(s) in 0.3230 seconds  
  3.   
  4. hbase(main):010:0> put 'phone','001','base_info:brand','iphone6'  
  5. 0 row(s) in 0.0250 seconds  
查看数据(只显示最新的版本)

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. hbase(main):011:0> get 'phone','001'  
  2. COLUMN                  CELL                                                              
  3.  base_info:brand        timestamp=1459074136116, value=iphone6                            
  4. 1 row(s) in 0.0650 seconds  
显示base_info列族2个版本数据

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. hbase(main):012:0> get 'phone','001',{COLUMN => 'base_info:brand',VERSIONS => 2}  
  2. COLUMN                  CELL                                                              
  3.  base_info:brand        timestamp=1459074136116, value=iphone6                            
  4.  base_info:brand        timestamp=1459074094569, value=oneplus                            
  5. 2 row(s) in 0.0680 seconds  
再传几条数据

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. hbase(main):013:0> put 'phone','001','base_info:brand','iphone7'  
  2. 0 row(s) in 0.0400 seconds  
  3.   
  4. hbase(main):014:0> put 'phone','001','base_info:brand','iphone8'  
  5. 0 row(s) in 0.0100 seconds  
  6.   
  7. hbase(main):015:0> put 'phone','001','base_info:brand','iphone9'  
  8. 0 row(s) in 0.0090 seconds  
  9.   
  10. hbase(main):016:0> put 'phone','001','base_info:brand','iphone10'  
  11. 0 row(s) in 0.0130 seconds  
按理说一共有六条了,但因为之前设置的版本数为5,所以只显示最新的5条

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. hbase(main):017:0> get 'phone','001',{COLUMN => 'base_info:brand',VERSIONS => 6}  
  2. COLUMN                  CELL                                                              
  3.  base_info:brand        timestamp=1459074536842, value=iphone10                           
  4.  base_info:brand        timestamp=1459074518748, value=iphone9                            
  5.  base_info:brand        timestamp=1459074515847, value=iphone8                            
  6.  base_info:brand        timestamp=1459074512440, value=iphone7                            
  7.  base_info:brand        timestamp=1459074136116, value=iphone6                            
  8. 5 row(s) in 0.0690 seconds  
用scan可以显示6条,因为过时的那条还没有被及时删除

[plain] view plain copy
在CODE上查看代码片派生到我的代码片
  1. hbase(main):018:0> scan 'phone',{RAW =>true, VERSIONS => 6}  
  2. ROW                     COLUMN+CELL                                                       
  3.  001                    column=base_info:brand, timestamp=1459074536842, value=iphone10   
  4.  001                    column=base_info:brand, timestamp=1459074518748, value=iphone9    
  5.  001                    column=base_info:brand, timestamp=1459074515847, value=iphone8    
  6.  001                    column=base_info:brand, timestamp=1459074512440, value=iphone7    
  7.  001                    column=base_info:brand, timestamp=1459074136116, value=iphone6    
  8.  001                    column=base_info:brand, timestamp=1459074094569, value=oneplus    
  9. 1 row(s) in 0.1970 seconds  

总结:不要用这个,反人类!用HBase的Java API!如下:

[java] view plain copy
在CODE上查看代码片派生到我的代码片
  1. package club.drguo.hbase;  
  2.   
  3. import java.util.ArrayList;  
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.hbase.HBaseConfiguration;  
  6. import org.apache.hadoop.hbase.HColumnDescriptor;  
  7. import org.apache.hadoop.hbase.HTableDescriptor;  
  8. import org.apache.hadoop.hbase.KeyValue;  
  9. import org.apache.hadoop.hbase.TableName;  
  10. import org.apache.hadoop.hbase.client.Get;  
  11. import org.apache.hadoop.hbase.client.HBaseAdmin;  
  12. import org.apache.hadoop.hbase.client.HTable;  
  13. import org.apache.hadoop.hbase.client.Put;  
  14. import org.apache.hadoop.hbase.client.Result;  
  15. import org.apache.hadoop.hbase.util.Bytes;  
  16. import org.junit.Before;  
  17. import org.junit.Test;  
  18.   
  19. public class HBaseDao {  
  20.     private Configuration conf = null;  
  21.   
  22.     @Before  
  23.     public void init() {  
  24.         conf = HBaseConfiguration.create();  
  25.         conf.set("hbase.zookeeper.quorum""drguo3:2181,drguo4:2181,drguo5:2181");  
  26.     }  
  27.   
  28.     // 插入数据  
  29.     @Test  
  30.     public void putTest() throws Exception {  
  31.         HTable phone1 = new HTable(conf, "phone1");  
  32.         Put brand = new Put(Bytes.toBytes("001"));  
  33.         brand.add(Bytes.toBytes("base_info"), Bytes.toBytes("brand"), Bytes.toBytes("oneplus"));  
  34.         Put ram = new Put(Bytes.toBytes("001"));  
  35.         ram.add(Bytes.toBytes("base_info"), Bytes.toBytes("ram"), Bytes.toBytes("3GB"));  
  36.         ArrayList<Put> puts = new ArrayList<>();  
  37.         puts.add(brand);  
  38.         puts.add(ram);  
  39.         phone1.put(puts);  
  40.         phone1.close();  
  41.     }  
  42.   
  43.     // 查询数据  
  44.     @SuppressWarnings("deprecation")  
  45.     @Test  
  46.     public void testGet() throws Exception {  
  47.         HTable table = new HTable(conf, "phone1");  
  48.         Get get = new Get(Bytes.toBytes("001"));  
  49.         get.setMaxVersions(5);  
  50.         Result result = table.get(get);  
  51. //      List<Cell> cells = result.listCells();  
  52.   
  53.         // result.getValue(family, qualifier); 可以从result中直接取出一个特定的value  
  54.   
  55.         // 遍历出result中所有的键值对  
  56.         for (KeyValue kv : result.list()) {  
  57.             System.out.println(new String(kv.getFamily()));  
  58.             System.out.println(new String(kv.getQualifier()));  
  59.             System.out.println(new String(kv.getValue()));  
  60.   
  61.         }  
  62.         table.close();  
  63.     }  
  64.   
  65.     // 建表  
  66.     @SuppressWarnings("resource")  
  67.     public static void main(String[] args) throws Exception {  
  68.         Configuration conf = HBaseConfiguration.create();  
  69.         conf.set("hbase.zookeeper.quorum""drguo3:2181,drguo4:2181,drguo5:2181");  
  70.         HBaseAdmin admin = new HBaseAdmin(conf);  
  71.         // 指定table名  
  72.         TableName tableName = TableName.valueOf("phone1");  
  73.         HTableDescriptor tableDescriptordescriptor = new HTableDescriptor(tableName);  
  74.         // 指定列族名  
  75.         HColumnDescriptor base_info = new HColumnDescriptor("base_info");  
  76.         HColumnDescriptor extra_info = new HColumnDescriptor("extra_info");  
  77.         // 给base_info设定版本数,不设就是默认数  
  78.         base_info.setMaxVersions(5);  
  79.         // 加入列族  
  80.         tableDescriptordescriptor.addFamily(base_info);  
  81.         tableDescriptordescriptor.addFamily(extra_info);  
  82.         // 创建表  
  83.         admin.createTable(tableDescriptordescriptor);  
  84.     }  
  85. }  
转载:http://blog.csdn.net/dr_guo/article/details/50992518
0 0
原创粉丝点击