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(二)常用API案例 -> 正文阅读

[大数据]大数据入门--HBase(二)常用API案例

HBase常用API案例

概述

抛砖引玉篇,不废话。主要演示了HBase常见操作API

  • DDL
    • namespace 创建 删除
    • table 创建 删除
  • DML
    • 数据的增改(改==增,都是put操作)、删、查

话不多说,撸代码!!!

package com.xbz.study.bigdata.hbase;

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.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* 封装Hbase API的常用操作
* DDL:
* 创建名称空间
* 创建表
* 判断表是否存在
* 删除表
* DML:
* 创建数据
* 更新数据
* 删除数据
* 查询数据
*/
public class ApiUtil {
   private static final Configuration configuration;

   static {
       configuration = HBaseConfiguration.create();
       configuration.set("hbase.zookeeper.quorum", "hadoop101:2181,hadoop102:2181,hadoop103:2181");
   }

   private static Connection openConnection() throws IOException {
       return ConnectionFactory.createConnection(configuration);
   }

   /**
    * 创建命名空间
    *
    * @param nameSpace
    */
   public static boolean createNameSpace(String nameSpace) throws IOException {
       Connection connection = openConnection();
       Admin admin = null;
       try {
           admin = connection.getAdmin();
           NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(nameSpace).build();
           admin.createNamespace(namespaceDescriptor);

       } catch (NamespaceExistException ex) {
           return false;
       } catch (Exception ex) {
           ex.printStackTrace();
           return false;
       } finally {
           if (admin != null) {
               admin.close();
           }
           connection.close();
       }
       return true;
   }

   /**
    * 删除命名空间
    *
    * @param nameSpace
    * @return
    * @throws IOException
    */
   public static boolean deleteNameSpace(String nameSpace) throws IOException {
       Connection connection = openConnection();
       Admin admin = null;
       try {
           admin = connection.getAdmin();
           admin.deleteNamespace(nameSpace);
       } catch (NamespaceNotFoundException ex) {
           return false;
       } catch (Exception ex) {
           ex.printStackTrace();
           return false;
       } finally {
           if (admin != null) {
               admin.close();
           }
           connection.close();
       }
       return true;
   }

   /**
    * 创建表
    *
    * @param tableName
    * @param cfs
    */
   public static boolean createTable(String tableName, String... cfs) throws IOException {
       if (cfs.length == 0) {
           throw new IllegalArgumentException("列族不能为空");
       }
       Connection connection = openConnection();
       Admin admin = null;
       try {
           admin = connection.getAdmin();
           boolean exists = admin.tableExists(TableName.valueOf(tableName));
           if (exists) {
               return false;
           }
           HTableDescriptor hTableDesc = new HTableDescriptor(TableName.valueOf(tableName));
           for (String cf : cfs) {
               hTableDesc.addFamily(new HColumnDescriptor(cf));
           }
           admin.createTable(hTableDesc);
       } catch (Exception ex) {
           ex.printStackTrace();
           return false;
       } finally {
           admin.close();
           connection.close();
       }
       return true;
   }

   /**
    * 删除表
    *
    * @param tableName
    * @return
    * @throws IOException
    */
   public static boolean deleteTable(String tableName) throws IOException {
       Connection connection = openConnection();
       Admin admin = null;
       try {
           admin = connection.getAdmin();
           //判断表是否存在
           TableName tName = TableName.valueOf(tableName);
           boolean exists = admin.tableExists(tName);
           if (!exists) {
               return true;
           }
           //如果存在,判断表是否已禁用
           boolean tableDisabled = admin.isTableDisabled(tName);
           if (!tableDisabled) {
               admin.disableTable(tName);
           }
           admin.deleteTable(tName);
       } catch (Exception ex) {
           ex.printStackTrace();
           return false;
       } finally {
           admin.close();
           connection.close();
       }
       return true;
   }

   /**
    * 添加数据
    *
    * @param tableName
    * @param cf        列族
    * @param cn        列名
    * @param value     值
    */
   public static void put(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
       Connection connection = openConnection();
       Table table = null;
       try {
           table = connection.getTable(TableName.valueOf(tableName));
           Put put = new Put(Bytes.toBytes(rowKey));
           put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
           table.put(put);
       } catch (Exception ex) {
           throw new RuntimeException(ex);
       } finally {
           if (table != null) {
               table.close();
           }
           connection.close();
       }
   }

   /**
    * 删除一行或者多行数据
    *
    * @param tableName
    * @param rowKeys
    */
   public static void deleteRow(String tableName, String... rowKeys) throws IOException {
       if (rowKeys == null || rowKeys.length == 0) {
           throw new IllegalArgumentException("至少指定一个rowKey");
       }
       Connection connection = openConnection();
       Table table = null;
       try {
           table = connection.getTable(TableName.valueOf(tableName));
           List<Delete> deletes = Arrays.stream(rowKeys).map((i) -> new Delete(Bytes.toBytes(i))).collect(Collectors.toList());
           table.delete(deletes);
       } catch (Exception ex) {
           throw new RuntimeException(ex);
       } finally {
           if (table != null) {
               table.close();
           }
           connection.close();
       }
   }

   /**
    * 删除指定列
    *
    * @param tableName
    * @param cf        列族
    * @param cn        列名称
    */
   public static void deleteCol(String tableName, String rowKey, String cf, String cn) throws IOException {
       Connection connection = openConnection();
       Table table = null;
       try {
           table = connection.getTable(TableName.valueOf(tableName));
           Delete delete = new Delete(Bytes.toBytes(rowKey));
           /*
            * 注意使用addColumns是删除多个版本
            * addCloumn是删除指定版本,如果此时有多个版本数据,则上个版本数据会冒出来,慎用!!!
            */
           delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));
           table.delete(delete);
       } catch (Exception ex) {
           throw new RuntimeException(ex);
       } finally {
           if (table != null) {
               table.close();
           }
           connection.close();
       }
   }

   /**
    * 获取列数据
    *
    * @param tableName
    * @param rowKey
    * @param cf
    * @param cn
    * @return
    * @throws IOException
    */
   public static String getCol(String tableName, String rowKey, String cf, String cn) throws IOException {
       Connection connection = openConnection();
       Table table = null;
       try {
           table = connection.getTable(TableName.valueOf(tableName));
           Get get = new Get(Bytes.toBytes(rowKey));
           get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
           Result result = table.get(get);
           return Bytes.toString(result.value());
       } catch (Exception ex) {
           throw new RuntimeException(ex);
       } finally {
           if (table != null) {
               table.close();
           }
           connection.close();
       }
   }

   /**
    * 扫描多行
    *
    * @param tableName
    * @param beginRowKey
    * @param endRowKey
    * @throws IOException
    */
   public static void scan(String tableName, String beginRowKey, String endRowKey) throws IOException {
       Connection connection = openConnection();
       Table table = null;
       try {
           table = connection.getTable(TableName.valueOf(tableName));
           Scan scan = new Scan();
           scan.setStartRow(Bytes.toBytes(beginRowKey));
           scan.setStopRow(Bytes.toBytes(endRowKey));
           ResultScanner scanner = table.getScanner(scan);
           System.out.println("---------------------------------------------------");
           scanner.forEach((result -> {
               String row = Bytes.toString(result.getRow());
               System.out.println("row:" + row);
               Cell[] cells = result.rawCells();
               for (Cell cell : cells) {
//                    String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
                   String cf = Bytes.toString(CellUtil.cloneFamily(cell));
                   String cn = Bytes.toString(CellUtil.cloneQualifier(cell));
                   String value = Bytes.toString(CellUtil.cloneValue(cell));
                   String timestamp = cell.getTimestamp() + "";
                   System.out.println(cf + ":" + cn + "=" + value + "," + timestamp);
               }
               System.out.println("--------------------------------------------------------------");
           }));
       } catch (Exception ex) {
           throw new RuntimeException(ex);
       } finally {
           if (table != null) {
               table.close();
           }
           connection.close();
       }
   }


   public static void main(String[] args) throws IOException {
       //创建命名空间
       String nameSpace = "study02";
//        System.out.println("创建命名空间:"+nameSpace+":"+(createNameSpace(nameSpace)?"成功":"失败"));
//        System.out.println("删除命名空间:" + nameSpace + ":" + (deleteNameSpace(nameSpace) ? "成功" : "失败"));
       String tableName = "tab01";
//        System.out.println("创建表:"+createTable(tableName,"info1","info2"));
//        System.out.println("删除表:"+deleteTable(tableName));
//        put(tableName,"001","info1","name","zhangsan");
//        deleteRow(tableName,"001","002");
//        deleteCol(tableName, "001", "info1", "name");
//        System.out.println(getCol(tableName, "001", "info1", "age"));
       scan(tableName, "001", "002");
   }
}

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-07-25 11:45:16  更:2021-07-25 11:45:39 
 
开发: 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年4日历 -2024/4/30 20:16:34-

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