zoukankan      html  css  js  c++  java
  • 学习笔记--HBase

    HBase 和 HDFS

    HDFS HBase
    HDFS是适于存储大容量文件的分布式文件系统。 HBase是建立在HDFS之上的数据库。
    HDFS不支持快速单独记录查找。 HBase提供在较大的表快速查找
    它提供了高延迟批量处理;没有批处理概念。 它提供了数十亿条记录低延迟访问单个行记录(随机存取)。
    它提供的数据只能顺序访问。 HBase内部使用哈希表和提供随机接入,并且其存储索引,可将在HDFS文件中的数据进行快速查找。

    HBase和RDBMS

    HBase RDBMS
    HBase无模式,它不具有固定列模式的概念;仅定义列族。 RDBMS有它的模式,描述表的整体结构的约束。
    它专门创建为宽表。 HBase是横向扩展。 这些都是细而专为小表。很难形成规模。
    没有任何事务存在于HBase。 RDBMS是事务性的。
    它反规范化的数据。 它具有规范化的数据。
    它用于半结构以及结构化数据是非常好的。 用于结构化数据非常好。

    HBase的特点

    • HBase线性可扩展。
    • 它具有自动故障支持。
    • 它提供了一致的读取和写入。
    • 它集成了Hadoop,作为源和目的地。
    • 客户端方便的Java API。
    • 它提供了跨集群数据复制。

    HBase的架构

    HBase有三个主要组成部分:客户端库,主服务器和区域服务器。区域服务器可以按要求添加或删除。

    主服务器

    • 分配区域给区域服务器并在Apache ZooKeeper的帮助下完成这个任务。
    • 处理跨区域的服务器区域的负载均衡。它卸载繁忙的服务器和转移区域较少占用的服务器。
    • 通过判定负载均衡以维护集群的状态。
    • 负责模式变化和其他元数据操作,如创建表和列。

    区域服务器

    • 与客户端进行通信并处理数据相关的操作。
    • 句柄读写的所有地区的请求。
    • 由以下的区域大小的阈值决定的区域的大小。

    HBase的安装

    • 安装Java
    • 安装Hadoop
    • 安装HBase

    HBaseShell

    启动HBaseShell
    启动Hadoop文件系统

    $cd $HADOOP_HOME/sbin
    $start-all.sh
    

    启动HBase

    $cd /usr/local/HBase
    $./bin/start-hbase.sh
    

    启动HBase主服务器

    $./bin/local-master-backup.sh start 2 (number signifies specific
    server.) 
    

    启动区域服务器

    $./bin/./local-regionservers.sh start 3
    

    启动HBase Shell

    $cd bin
    $./hbase shell
    

    通用命令

    • status: 提供HBase的状态,例如,服务器的数量。
    • version: 提供正在使用HBase版本。
    • table_help: 表引用命令提供帮助。
    • whoami: 提供有关用户的信息。

    数据定义语言

    • create: 创建一个表。
    • list: 列出HBase的所有表。
    • disable: 禁用表。
    • is_disabled: 验证表是否被禁用。
    • enable: 启用一个表。
    • is_enabled: 验证表是否已启用。
    • describe: 提供了一个表的描述。
    • alter: 改变一个表。
    • exists: 验证表是否存在。
    • drop: 从HBase中删除表。
    • drop_all: 丢弃在命令中给出匹配“regex”的表。
    • Java Admin API: 在此之前所有的上述命令,Java提供了一个通过API编程来管理实现DDL功能。在这个org.apache.hadoop.hbase.client包中有HBaseAdmin和HTableDescriptor 这两个重要的类提供DDL功能。

    数据操纵语言

    • put: 把指定列在指定的行中单元格的值在一个特定的表。
    • get: 取行或单元格的内容。
    • delete: 删除表中的单元格值。
    • deleteall: 删除给定行的所有单元格。
    • scan: 扫描并返回表数据。
    • count: 计数并返回表中的行的数目。
    • truncate: 禁用,删除和重新创建一个指定的表。
    • Java client API: 在此之前所有上述命令,Java提供了一个客户端API来实现DML功能,CRUD(创建检索更新删除)操作更多的是通过编程,在org.apache.hadoop.hbase.client包下。 在此包HTable 的 Put和Get是重要的类。

    HBase Admin API

    HBaseAdmin类
    HBaseAdmin是一个类表示管理。这个类属于org.apache.hadoop.hbase.client包。使用这个类,可以执行管理员任务。使用Connection.getAdmin()方法来获取管理员的实例。

    S.No. 方法及说明
    1 void createTable(HTableDescriptor desc) 创建一个新的表
    2 void createTable(HTableDescriptor desc, byte[][] splitKeys) 创建一个新表使用一组初始指定的分割键限定空区域
    3 void deleteColumn(byte[] tableName, String columnName) 从表中删除列
    4 void deleteColumn(String tableName, String columnName) 删除表中的列
    5 void deleteTable(String tableName) 删除表

    Descriptor类

    HTableDescriptor(TableName name)     //构造函数,构造一个表描述符指定TableName对象
    HTableDescriptor addFamily(HColumnDescriptor family)         //将列家族给定的描述符
    

    HBase创建表

    语法

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

    示例

    create 'emp', 'personal data', ’professional data’
    

    使用Java API创建表

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

    HBase列出表

    语法

    list
    

    使用Java API列出表

    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禁用表

    disable 'emp'
    

    禁用表之后,仍然可以通过 list 和exists命令查看到。无法扫描到它存在,它会给下面的错误。

    hbase(main):028:0> scan 'emp'
    
    ROW        COLUMN+CELL
    
    ERROR: emp is disabled.
    

    查看表是否被禁用

    hbase> is_disabled 'table name'
    

    禁用所有给定的表

    hbase> disable_all 'r.*'
    

    使用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 'emp'
    scan 'emp'        //扫描看表是否成功启用
    is_enabled 'emp'
    

    使用Java API启用表

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

    描述

    describe 'table_name'
    

    修改
    更改列族单元格的最大数目

    hbase> alter 't1', NAME => 'f1', VERSIONS => 5
    

    表范围运算符

    //设置只读
    hbase>alter 't1', READONLY(option)
    //删除表范围运算符
    hbase> alter 't1', METHOD => 'table_att_unset', NAME => 'MAX_FILESIZE'
    //删除列族
    hbase> alter ‘ table name ’, ‘delete’ => ‘ column family ’ 
    

    使用Java API

    //添加列族
    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");
      }
    }
    //删除列族
    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"); 
      }
    }
    

    HBase Exists

    hbase(main):024:0> exists 'emp'
    

    使用Java API

    public class TableExists{
    
      public static void main(String args[])throws IOException{
    
      // Instantiating configuration class
      Configuration conf = HBaseConfiguration.create();
    
      // Instantiating HBaseAdmin class
      HBaseAdmin admin = new HBaseAdmin(conf);
    
      // Verifying the existance of the table
      boolean bool = admin.tableExists("emp");
      System.out.println( bool);
      }
    } 
    

    HBase删除表

    hbase(main):018:0> disable 'emp'
    hbase(main):019:0> drop 'emp'
    hbase(main):020:0> exists 'emp'
    

    drop_all

    hbase(main):002:0> disable_all 'raj.*'
    hbase(main):018:0> drop_all 'raj.*'
    

    使用Java API

    public class DeleteTable {
    
      public static void main(String[] args) throws IOException {
    
          // Instantiating configuration class
          Configuration conf = HBaseConfiguration.create();
    
          // Instantiating HBaseAdmin class
          HBaseAdmin admin = new HBaseAdmin(conf);
    
          // disabling table named emp
          admin.disableTable("emp12");
    
          // Deleting emp
          admin.deleteTable("emp12");
          System.out.println("Table deleted");
      }
    }
    

    HBase关闭

    hbase(main):021:0> exit
    ./bin/stop-hbase.sh
    

    使用Java API

    public class ShutDownHbase{
    
      public static void main(String args[])throws IOException {
    
          // Instantiating configuration class
          Configuration conf = HBaseConfiguration.create();
    
          // Instantiating HBaseAdmin class
          HBaseAdmin admin = new HBaseAdmin(conf);
    
          // Shutting down HBase
          System.out.println("Shutting down hbase");
          admin.shutdown();
      }
    }
    

    HBase创建数据

    语法

    put ’<table name>’,’row1’,’<colfamily:colname>’,’<value>’
    

    示例

    hbase(main):005:0> put 'emp','1','personal data:name','raju'
    

    使用Java API

    import java.io.IOException;
    
    import org.apache.hadoop.conf.Configuration;
    
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.Put;
    import org.apache.hadoop.hbase.util.Bytes;
    
    public class InsertData{
    
      public static void main(String[] args) throws IOException {
    
          // Instantiating Configuration class
          Configuration config = HBaseConfiguration.create();
    
          // Instantiating HTable class
          HTable hTable = new HTable(config, "emp");
    
          // Instantiating Put class
          // accepts a row name.
          Put p = new Put(Bytes.toBytes("row1")); 
    
          // adding values using add() method
          // accepts column family name, qualifier/row name ,value
          p.add(Bytes.toBytes("personal"),
          Bytes.toBytes("name"),Bytes.toBytes("raju"));
    
          p.add(Bytes.toBytes("personal"),
          Bytes.toBytes("city"),Bytes.toBytes("hyderabad"));
    
          p.add(Bytes.toBytes("professional"),Bytes.toBytes("designation"),
          Bytes.toBytes("manager"));
    
          p.add(Bytes.toBytes("professional"),Bytes.toBytes("salary"),
          Bytes.toBytes("50000"));
          
          // Saving the put Instance to the HTable.
          hTable.put(p);
          System.out.println("data inserted");
          
          // closing HTable
          hTable.close();
      }
    }
    

    HBase更新数据

    更新数据和插入数据操作一样,都是用put

    HBase读取数据

    //读取一行
    get ’<table name>’,’row1’
    //读取指定列
    hbase>get 'table name', ‘rowid’, {COLUMN => ‘column family:column name ’}
    

    使用Java API

    import java.io.IOException;
    
    import org.apache.hadoop.conf.Configuration;
    
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.client.Get;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.Result;
    import org.apache.hadoop.hbase.util.Bytes;
    
    public class RetriveData{
    
      public static void main(String[] args) throws IOException, Exception{
      
          // Instantiating Configuration class
          Configuration config = HBaseConfiguration.create();
    
          // Instantiating HTable class
          HTable table = new HTable(config, "emp");
    
          // Instantiating Get class
          Get g = new Get(Bytes.toBytes("row1"));
    
          // Reading the data
          Result result = table.get(g);
    
          // Reading values from Result class object
          byte [] value = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("name"));
    
          byte [] value1 = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("city"));
    
          // Printing the values
          String name = Bytes.toString(value);
          String city = Bytes.toString(value1);
          
          System.out.println("name: " + name + " city: " + city);
      }
    }
    

    HBase删除数据

    delete ‘<table name>’, ‘<row>’, ‘<column name >’, ‘<time stamp>’
    deleteall ‘<table name>’, ‘<row>’
    

    使用Java API

    import java.io.IOException;
    
    import org.apache.hadoop.conf.Configuration;
    
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.client.Delete;
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.util.Bytes;
    
    public class DeleteData {
    
      public static void main(String[] args) throws IOException {
    
          // Instantiating Configuration class
          Configuration conf = HBaseConfiguration.create();
    
          // Instantiating HTable class
          HTable table = new HTable(conf, "employee");
    
          // Instantiating Delete class
          Delete delete = new Delete(Bytes.toBytes("row1"));
          delete.deleteColumn(Bytes.toBytes("personal"), Bytes.toBytes("name"));
          delete.deleteFamily(Bytes.toBytes("professional"));
    
          // deleting the data
          table.delete(delete);
    
          // closing the HTable object
          table.close();
          System.out.println("data deleted.....");
      }
    }
    

    HBase扫描

    scan ‘<table name>’ 
    

    使用Java API扫描

    import java.io.IOException;
    
    import org.apache.hadoop.conf.Configuration;
    
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import org.apache.hadoop.hbase.client.HTable;
    import org.apache.hadoop.hbase.client.Result;
    import org.apache.hadoop.hbase.client.ResultScanner;
    import org.apache.hadoop.hbase.client.Scan;
    
    
    public class ScanTable{
    
      public static void main(String args[]) throws IOException{
    
          // Instantiating Configuration class
          Configuration config = HBaseConfiguration.create();
    
          // Instantiating HTable class
          HTable table = new HTable(config, "emp");
    
          // Instantiating the Scan class
          Scan scan = new Scan();
    
          // Scanning the required columns
          scan.addColumn(Bytes.toBytes("personal"), Bytes.toBytes("name"));
          scan.addColumn(Bytes.toBytes("personal"), Bytes.toBytes("city"));
    
          // Getting the scan result
          ResultScanner scanner = table.getScanner(scan);
    
          // Reading values from scan result
          for (Result result = scanner.next(); result != null; result = Scanner.next())
    
          System.out.println("Found row : " + result);
          //closing the scanner
          scanner.close();
      }
    }
    

    HBase计算和截断

    //计数
    count ‘<table name>’
    //截断  
    truncate 'table name'
    

    HBase安全

    //授权
    hbase> grant <user> <permissions> [<table> [<column family> [<column; qualifier>]]
    //示例
    hbase(main):018:0> grant 'Tutorialspoint', 'RWXCA'
    //撤销权限
    hbase> revoke <user>
    //列出权限
    hbase>user_permission ‘tablename’
    
  • 相关阅读:
    yarn的安装和使用
    git clone不了项目文件的解决方法和----vscode操作git的方法
    vue使用swiper遇到的坑
    stylus介绍与引入
    Win10在当前目录快速打开cmd的方法
    git切换到指定分支,git新建分支与合并
    jQuery之表格隔行变色
    jQuery之onload与ready
    jQuery之多库共存
    jQuery之插件
  • 原文地址:https://www.cnblogs.com/kioluo/p/8824792.html
Copyright © 2011-2022 走看看