IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> HBase基础 -> 正文阅读

[大数据]HBase基础

HBase基础

基本操作

  1. 创建表
hbase:008:0> create 'student', 'info'
  1. 插入数据
hbase:012:0> put 'student','1001','info:sex','male'
Took 0.0165 seconds                                                        
hbase:013:0> put 'student', '1001', 'info:age', '18'
Took 0.0152 seconds                                                        
hbase:014:0> put 'student', '1002', 'info:name', 'zhangsan'
Took 0.0053 seconds                                                        
hbase:015:0> put 'student', '1002', 'info:sex', 'female'
Took 0.0102 seconds                                                        
hbase:016:0> put 'student', '1002', 'info:age', '20'
Took 0.0107 seconds 
  1. 扫描查看表数据
hbase:018:0> scan 'student'
ROW                                     COLUMN+CELL                                                                                                        
 1001                                   column=info:age, timestamp=2021-08-02T16:54:33.093, value=18                                                       
 1001                                   column=info:sex, timestamp=2021-08-02T16:53:54.575, value=male                                                     
 1002                                   column=info:age, timestamp=2021-08-02T16:55:22.819, value=20                                                       
 1002                                   column=info:name, timestamp=2021-08-02T16:54:53.701, value=zhangsan                                                
 1002                                   column=info:sex, timestamp=2021-08-02T16:55:10.711, value=female                                                   
2 row(s)
Took 0.0169 seconds    


hbase:021:0> scan 'student', {STARTROW => '1001', STOPROW => '1001'}
ROW                                     COLUMN+CELL                                                                                                        
 1001                                   column=info:age, timestamp=2021-08-02T16:54:33.093, value=18                                                       
 1001                                   column=info:sex, timestamp=2021-08-02T16:53:54.575, value=male                                                     
1 row(s)
Took 0.0265 seconds   


hbase:023:0> scan 'student', {STARTROW => '1002'}
ROW                                     COLUMN+CELL                                                                                                        
 1002                                   column=info:age, timestamp=2021-08-02T16:55:22.819, value=20                                                       
 1002                                   column=info:name, timestamp=2021-08-02T16:54:53.701, value=zhangsan                                                
 1002                                   column=info:sex, timestamp=2021-08-02T16:55:10.711, value=female                                                   
1 row(s)
Took 0.0151 seconds                                                                  
  1. 查看表结构
hbase:024:0> desc 'student'
Table student is ENABLED                                                                                                                                   
student                                                                                                                                                    
COLUMN FAMILIES DESCRIPTION                                                                                                                                
{NAME => 'info', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 
'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                                       

1 row(s)
Quota is disabled
Took 0.0935 seconds       
  1. 更新数据
hbase:026:0> put 'student', '1001', 'info:name', 'lisi'
Took 0.0123 seconds                                                        
hbase:027:0> put 'student', '1001', 'info:age', '20'
Took 0.0172 seconds  
  1. 查看“指定行”或“指定列族:列”的数据
hbase:030:0> get 'student', '1001'
COLUMN                                 CELL  

 info:age                               timestamp=2021-08-02T17:02:06.801, value=20                                           
 info:name                              timestamp=2021-08-02T17:01:46.259, value=lisi                                                                      
 info:sex                               timestamp=2021-08-02T16:53:54.575, value=male                                                                      
1 row(s)
Took 0.0241 seconds  


hbase:031:0> get 'student', '1001', 'info:name'
COLUMN                                  CELL                                                                                                               
 info:name                              timestamp=2021-08-02T17:01:46.259, value=lisi                                                                      
1 row(s)
Took 0.0259 seconds  
  1. 统计表数据行数
hbase:033:0> count 'student'
2 row(s)
Took 0.0343 seconds                                                                                                                                        
=> 2
  1. 删除数据
hbase:034:0> deleteall 'student', '1001'
Took 0.0192 seconds                                                                                                                                        
hbase:035:0> delete 'student', '1002', 'info:sex'
Took 0.0117 seconds  
  1. 清空表数据
hbase:038:0> disable 'student'
Took 1.1732 seconds                                                                                                                                        
hbase:039:0> truncate 'student'
Truncating 'student' table (it may take a while):
Truncating table...
Took 2.1732 seconds    
  1. 删除表
hbase:041:0> disable 'student'
Took 0.3492 seconds                                                                                                                                        
hbase:042:0> drop 'student'
Took 0.1452 seconds 
  1. 变更表信息
hbase:022:0> alter 'student',{NAME=>'info',VERSIONS=>3}
hbase:022:0> get 'student','1001',{COLUMN=>'info:name',VERSIONS=>3}

API操作

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Test01 {
    public static void main(String[] args) throws IOException {

    }

    //获取 Configuration 对象
    public static Configuration conf;
    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.200.101");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
    }

    //判断表是否存在
    public static boolean isTableExist(String tableName) throws IOException {
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        return admin.tableExists(TableName.valueOf(tableName));
    }

    //创建表
    public static void createTable(String tableName, String... columnFamily) throws IOException {
        Admin admin = ConnectionFactory.createConnection(conf).getAdmin();
        if (admin.tableExists(TableName.valueOf(tableName))) {
            System.out.println(tableName + "已经存在!");
        }else {
            HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
            for (String cf : columnFamily) {
                tableDescriptor.addFamily(new HColumnDescriptor(cf));
            }
            admin.createTable(tableDescriptor);
            System.out.println(tableName + "创建成功!");
        }
    }

    //删除表
    public static void dropTable(String tableName) throws IOException {
        Admin admin = ConnectionFactory.createConnection(conf).getAdmin();
        if (admin.tableExists(TableName.valueOf(tableName))) {
            admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
            System.out.println(tableName + "已经删除!");
        }else {
            System.out.println(tableName + "不存在!!");
        }
    }

    //插入数据向表中
    public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException{
        HTable hTable = new HTable(conf, tableName);
        Put put = new Put(Bytes.toBytes(rowKey));
        put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
        hTable.put(put);
        hTable.close();
        System.out.println("插入数据成功");
    }

    //删除多行数据
    public static void deleteMultiRow(String tableName, String... rows) throws IOException{
        HTable hTable = new HTable(conf, tableName);
        List<Delete> deleteList = new ArrayList<Delete>();
        for(String row : rows){
            Delete delete = new Delete(Bytes.toBytes(row));
            deleteList.add(delete);
        }
        hTable.delete(deleteList);
        hTable.close();
    }

    //获取所有数据
    public static void getAllRows(String tableName) throws IOException{
        HTable hTable = new HTable(conf, tableName);
        //得到用于扫描 region 的对象
        Scan scan = new Scan();
        //使用 HTable 得到 resultcanner 实现类的对象
        ResultScanner resultScanner = hTable.getScanner(scan);
        for(Result result : resultScanner){
            Cell[] cells = result.rawCells();
            for(Cell cell : cells){
                //得到 rowkey
                System.out.println(" 行 键 :" + Bytes.toString(CellUtil.cloneRow(cell)));
                //得到列族
                System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    }

    //获取某一行数据
    public static void getRow(String tableName, String rowKey) throws IOException{
        HTable table = new HTable(conf, tableName);
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = table.get(get);
        for(Cell cell : result.rawCells()){
            System.out.println(" 行 键 :" + Bytes.toString(result.getRow()));
            System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("时间戳:" + cell.getTimestamp());
        }
    }

    //获取某一行指定“列族:列”的数据
    public static void getRowQualifier(String tableName, String rowKey, String family, String qualifier) throws IOException{
        HTable table = new HTable(conf, tableName);
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
        Result result = table.get(get);
        for(Cell cell : result.rawCells()){
            System.out.println(" 行 键 :" + Bytes.toString(result.getRow()));
            System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }

}
  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-08-03 11:16:33  更:2021-08-03 11:17:23 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 5:28:50-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码