1.version
2.table_help
3.whoami

创建表

  1. import java.io.IOException;
  2. import org.apache.hadoop.hbase.HBaseConfiguration;
  3. import org.apache.hadoop.hbase.HColumnDescriptor;
  4. import org.apache.hadoop.hbase.HTableDescriptor;
  5. import org.apache.hadoop.hbase.client.HBaseAdmin;
  6. import org.apache.hadoop.hbase.TableName;
  7. import org.apache.hadoop.conf.Configuration;
  8. public class CreateTable {
  9. public static void main(String[] args) throws IOException {
  10. // Instantiating configuration class
  11. Configuration con = HBaseConfiguration.create();
  12. // Instantiating HbaseAdmin class
  13. HBaseAdmin admin = new HBaseAdmin(con);
  14. // Instantiating table descriptor class
  15. HTableDescriptor tableDescriptor = new
  16. TableDescriptor(TableName.valueOf("emp"));
  17. // Adding column families to table descriptor
  18. tableDescriptor.addFamily(new HColumnDescriptor("personal"));
  19. tableDescriptor.addFamily(new HColumnDescriptor("professional"));
  20. // Execute the table through admin
  21. admin.createTable(tableDescriptor);
  22. System.out.println(" Table created ");
  23. }
  24. }

HBase列出表

list

  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.HTableDescriptor;
  5. import org.apache.hadoop.hbase.MasterNotRunningException;
  6. import org.apache.hadoop.hbase.client.HBaseAdmin;
  7. public class ListTables {
  8. public static void main(String args[])throws MasterNotRunningException, IOException{
  9. // Instantiating a configuration class
  10. Configuration conf = HBaseConfiguration.create();
  11. // Instantiating HBaseAdmin class
  12. HBaseAdmin admin = new HBaseAdmin(conf);
  13. // Getting all the list of tables using HBaseAdmin object
  14. HTableDescriptor[] tableDescriptor =admin.listTables();
  15. // printing all the table names.
  16. for (int i=0; i<tableDescriptor.length;i++ ){
  17. System.out.println(tableDescriptor[i].getNameAsString());
  18. }
  19. }
  20. }

HBase禁用表

is_disabled
is_disabled ‘table name’
disable_all ‘r.*’

  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.MasterNotRunningException;
  5. import org.apache.hadoop.hbase.client.HBaseAdmin;
  6. public class DisableTable{
  7. public static void main(String args[]) throws MasterNotRunningException, IOException{
  8. // Instantiating configuration class
  9. Configuration conf = HBaseConfiguration.create();
  10. // Instantiating HBaseAdmin class
  11. HBaseAdmin admin = new HBaseAdmin(conf);
  12. // Verifying weather the table is disabled
  13. Boolean bool = admin.isTableDisabled("emp");
  14. System.out.println(bool);
  15. // Disabling the table using HBaseAdmin object
  16. if(!bool){
  17. admin.disableTable("emp");
  18. System.out.println("Table disabled");
  19. }
  20. }
  21. }

HBase启用表

enable ‘emp’
scan ‘emp’

is_enabled ‘table name’
is_enabled ‘emp’

  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.MasterNotRunningException;
  5. import org.apache.hadoop.hbase.client.HBaseAdmin;
  6. public class EnableTable{
  7. public static void main(String args[]) throws MasterNotRunningException, IOException{
  8. // Instantiating configuration class
  9. Configuration conf = HBaseConfiguration.create();
  10. // Instantiating HBaseAdmin class
  11. HBaseAdmin admin = new HBaseAdmin(conf);
  12. // Verifying weather the table is disabled
  13. Boolean bool = admin.isTableEnabled("emp");
  14. System.out.println(bool);
  15. // Disabling the table using HBaseAdmin object
  16. if(!bool){
  17. admin.enableTable("emp");
  18. System.out.println("Table Enabled");
  19. }
  20. }
  21. }

HBase表描述和修改
describe ‘emp’

更改列族单元格的最大数目
单元的最大数目设置为5
alter ‘emp’, NAME => ‘personal data’, VERSIONS => 5

设置只读
alter ‘emp’, READONLY

删除表范围运算符
alter ‘t1’, METHOD => ‘table_att_unset’, NAME => ‘MAX_FILESIZE’

删除列族
使用alter删除列族的语法
alter ‘ table name ’, ‘delete’ => ‘ column family ’

  1. alter 'employee','delete'=>'professional'

使用Java API添加一列族

  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.HColumnDescriptor;
  5. import org.apache.hadoop.hbase.MasterNotRunningException;
  6. import org.apache.hadoop.hbase.client.HBaseAdmin;
  7. public class AddColoumn{
  8. public static void main(String args[]) throws MasterNotRunningException, IOException{
  9. // Instantiating configuration class.
  10. Configuration conf = HBaseConfiguration.create();
  11. // Instantiating HBaseAdmin class.
  12. HBaseAdmin admin = new HBaseAdmin(conf);
  13. // Instantiating columnDescriptor class
  14. HColumnDescriptor columnDescriptor = new HColumnDescriptor("contactDetails");
  15. // Adding column family
  16. admin.addColumn("employee", columnDescriptor);
  17. System.out.println("coloumn added");
  18. }
  19. }

使用Java API删除列族

  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.MasterNotRunningException;
  5. import org.apache.hadoop.hbase.client.HBaseAdmin;
  6. public class DeleteColoumn{
  7. public static void main(String args[]) throws MasterNotRunningException, IOException{
  8. // Instantiating configuration class.
  9. Configuration conf = HBaseConfiguration.create();
  10. // Instantiating HBaseAdmin class.
  11. HBaseAdmin admin = new HBaseAdmin(conf);
  12. // Deleting a column family
  13. admin.deleteColumn("employee","contactDetails");
  14. System.out.println("coloumn deleted");
  15. }
  16. }

HBase Exists

exists ‘student’

  1. import java.io.IOException;
  2. import org.apache.hadoop.hbase.HBaseConfiguration;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.client.HBaseAdmin;
  5. public class TableExists{
  6. public static void main(String args[])throws IOException{
  7. // Instantiating configuration class
  8. Configuration conf = HBaseConfiguration.create();
  9. // Instantiating HBaseAdmin class
  10. HBaseAdmin admin = new HBaseAdmin(conf);
  11. // Verifying the existance of the table
  12. boolean bool = admin.tableExists("emp");
  13. System.out.println( bool);
  14. }
  15. }

HBase关闭

  1. import java.io.IOException;
  2. import org.apache.hadoop.hbase.HBaseConfiguration;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.client.HBaseAdmin;
  5. public class ShutDownHbase{
  6. public static void main(String args[])throws IOException {
  7. // Instantiating configuration class
  8. Configuration conf = HBaseConfiguration.create();
  9. // Instantiating HBaseAdmin class
  10. HBaseAdmin admin = new HBaseAdmin(conf);
  11. // Shutting down HBase
  12. System.out.println("Shutting down hbase");
  13. admin.shutdown();
  14. }
  15. }

put

  1. package com.hfqx.decodelpd.dao;
  2. import com.hfqx.decodelpd.utils.HBaseUtil;
  3. import com.stumbleupon.async.Deferred;
  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.TableName;
  9. import org.apache.hadoop.hbase.client.HBaseAdmin;
  10. import org.hbase.async.HBaseClient;
  11. import org.hbase.async.PutRequest;
  12. import java.io.IOException;
  13. import java.util.List;
  14. /**
  15. * description
  16. *
  17. * @author ${user}
  18. * @Time 2019-06-21
  19. */
  20. public class HbaseDao {
  21. private static String hbaseip = null;
  22. private static String zkhost = null;
  23. private static String zkport = null;
  24. private static HBaseClient hBaseClient = null;
  25. private static Configuration config = null;
  26. static {
  27. hbaseip = HBaseUtil.getHbaseMaster();
  28. zkhost = HBaseUtil.getZkhost();
  29. zkport = HBaseUtil.getZkport();
  30. config = HBaseConfiguration.create();// 配置
  31. config.set("hbase.zookeeper.quorum", zkhost); // zookeeper地址
  32. config.set("hbase.zookeeper.property.clientPort", zkport);// zookeeper端口
  33. hBaseClient = new HBaseClient(hbaseip);
  34. }
  35. /**
  36. * check table that is exists or not
  37. * @param tablename
  38. * @return
  39. */
  40. public boolean checkTable(String tablename) {
  41. HBaseAdmin admin = null; // hbase表管理
  42. try {
  43. admin = new HBaseAdmin(config);
  44. String[]tablenames = admin.getTableNames();
  45. for( String table1:tablenames){
  46. if ( table1.equals(tablename)){
  47. System.out.println("表已经存在");
  48. return true;
  49. }
  50. }
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. try {
  55. admin.close();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. return false;
  60. }
  61. public boolean checkTable(HBaseAdmin admin, String tablename) {
  62. try {
  63. String[]tablenames = admin.getTableNames();
  64. for( String table1:tablenames){
  65. if ( table1.equals(tablename)){
  66. System.out.println("表已经存在");
  67. return true;
  68. }
  69. }
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73. return false;
  74. }
  75. /**
  76. * create table that is exists or not
  77. * @param tablename
  78. * @param columnfamily
  79. * @return
  80. */
  81. public boolean createTable(String tablename, String columnfamily) {
  82. HBaseAdmin admin = null; // hbase表管理
  83. try {
  84. admin = new HBaseAdmin(config);
  85. } catch (IOException e) {
  86. e.printStackTrace();
  87. }
  88. if ( checkTable(admin, tablename) ) {
  89. return true;
  90. }
  91. // 创建表描述类
  92. TableName tableName = TableName.valueOf(tablename); // 表名称
  93. HTableDescriptor desc = new HTableDescriptor(tableName);
  94. HColumnDescriptor[] hColumnDescriptors = desc.getColumnFamilies();
  95. System.out.println(hColumnDescriptors.toString());
  96. // 创建列族的描述类
  97. HColumnDescriptor family = new HColumnDescriptor(columnfamily); // 列族
  98. // 将列族添加到表中
  99. desc.addFamily(family);
  100. // 创建表
  101. try {
  102. admin.createTable(desc); // 创建表
  103. admin.close();
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }
  107. System.out.println("创建表成功!");
  108. return true;
  109. }
  110. public Deferred<Object> Put(PutRequest putRequest) {
  111. return hBaseClient.put(putRequest);
  112. }
  113. public void Put(List<PutRequest> putRequestList) {
  114. for(PutRequest putRequest: putRequestList) {
  115. try {
  116. Put(putRequest).join();
  117. } catch (Exception e) {
  118. e.printStackTrace();
  119. }
  120. }
  121. }
  122. public static void main(String[] args) {
  123. HbaseDao hbaseDao = new HbaseDao();
  124. hbaseDao.createTable("QX_LPD","f");
  125. }
  126. }