Hbase1.2.0以后 JavaAPI最新接口调用方法

2023-12-13 07:58

本文主要是介绍Hbase1.2.0以后 JavaAPI最新接口调用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

[java]  view plain copy
  1. package cn.gaiay.hbase;  
  2.   
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.hbase.*;  
  5. import org.apache.hadoop.hbase.client.*;  
  6. import org.apache.hadoop.hbase.util.Bytes;  
  7.   
  8. import java.io.IOException;  
  9.   
  10. public class Test {  
  11.     // 声明静态配置  
  12.     static Configuration conf = null;  
  13.     static  Connection  connection = null;  
  14.     static {  
  15.         try{  
  16.             Configuration conf = HBaseConfiguration.create();  
  17.             conf.set("hbase.zookeeper.quorum""hadoop-01,hadoop-02,hadoop-03");  
  18.             connection = ConnectionFactory.createConnection(conf);  
  19.         }catch (Exception e){  
  20.             e.printStackTrace();  
  21.         }  
  22.     }  
  23.   
  24.     /* 
  25.      * 创建表 
  26.      * 
  27.      * @tableName 表名 
  28.      * 
  29.      * @family 列族列表 
  30.      */  
  31.     public static void creatTable(String tableName, String[] family)  
  32.             throws Exception {  
  33.         Admin admin = connection.getAdmin();  
  34.         HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));  
  35.         for (int i = 0; i < family.length; i++) {  
  36.             desc.addFamily(new HColumnDescriptor(family[i]));  
  37.         }  
  38.         if (admin.tableExists(TableName.valueOf(tableName))) {  
  39.             System.out.println("table Exists!");  
  40.             System.exit(0);  
  41.         } else {  
  42.             admin.createTable(desc);  
  43.             System.out.println("create table Success!");  
  44.         }  
  45.     }  
  46.   
  47.     /* 
  48.      * 为表添加数据(适合知道有多少列族的固定表) 
  49.      * 
  50.      * @rowKey rowKey 
  51.      * 
  52.      * @tableName 表名 
  53.      * 
  54.      * @column1 第一个列族列表 
  55.      * 
  56.      * @value1 第一个列的值的列表 
  57.      * 
  58.      * @column2 第二个列族列表 
  59.      * 
  60.      * @value2 第二个列的值的列表 
  61.      */  
  62.     public static void addData(String rowKey, String tableName,  
  63.                                String[] column1, String[] value1, String[] column2, String[] value2)  
  64.             throws IOException {  
  65.         Put put = new Put(Bytes.toBytes(rowKey));// 设置rowkey  
  66.         Table table = connection.getTable(TableName.valueOf(tableName));// HTabel负责跟记录相关的操作如增删改查等//  
  67.         // 获取表  
  68.         HColumnDescriptor[] columnFamilies = table.getTableDescriptor() // 获取所有的列族  
  69.                 .getColumnFamilies();  
  70.   
  71.         for (int i = 0; i < columnFamilies.length; i++) {  
  72.             String familyName = columnFamilies[i].getNameAsString(); // 获取列族名  
  73.             if (familyName.equals("article")) { // article列族put数据  
  74.                 for (int j = 0; j < column1.length; j++) {  
  75.                     put.addColumn(Bytes.toBytes(familyName),  
  76.                             Bytes.toBytes(column1[j]), Bytes.toBytes(value1[j]));  
  77.                 }  
  78.             }  
  79.             if (familyName.equals("author")) { // author列族put数据  
  80.                 for (int j = 0; j < column2.length; j++) {  
  81.                     put.addColumn(Bytes.toBytes(familyName),  
  82.                             Bytes.toBytes(column2[j]), Bytes.toBytes(value2[j]));  
  83.                 }  
  84.             }  
  85.         }  
  86.         table.put(put);  
  87.         System.out.println("add data Success!");  
  88.     }  
  89.   
  90.     /* 
  91.      * 根据rwokey查询 
  92.      * 
  93.      * @rowKey rowKey 
  94.      * 
  95.      * @tableName 表名 
  96.      */  
  97.     public static Result getResult(String tableName, String rowKey)  
  98.             throws IOException {  
  99.         Get get = new Get(Bytes.toBytes(rowKey));  
  100.         Table table = connection.getTable(TableName.valueOf(tableName));// 获取表  
  101.         Result result = table.get(get);  
  102.         for (Cell cell : result.listCells()) {  
  103.             System.out.println("family:" + Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()));  
  104.             System.out.println("qualifier:" + Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()));  
  105.             System.out.println("value:" + Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));  
  106.             System.out.println("Timestamp:" + cell.getTimestamp());  
  107.             System.out.println("-------------------------------------------");  
  108.         }  
  109.         return result;  
  110.     }  
  111.   
  112.     /* 
  113.      * 遍历查询hbase表 
  114.      * 
  115.      * @tableName 表名 
  116.      */  
  117.     public static void getResultScann(String tableName) throws IOException {  
  118.         Scan scan = new Scan();  
  119.         ResultScanner rs = null;  
  120.         Table table = connection.getTable(TableName.valueOf(tableName));  
  121.         try {  
  122.             rs = table.getScanner(scan);  
  123.             for (Result r : rs) {  
  124.                 for (Cell cell : r.listCells()) {  
  125.                     System.out.println("row:" + Bytes.toString(cell.getRowArray(),cell.getRowOffset(),cell.getRowLength()));  
  126.                     System.out.println("family:"  
  127.                             + Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getValueLength()));  
  128.                     System.out.println("qualifier:"  
  129.                             + Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));  
  130.                     System.out  
  131.                             .println("value:" + Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));  
  132.                     System.out.println("timestamp:" + cell.getTimestamp());  
  133.                     System.out  
  134.                             .println("-------------------------------------------");  
  135.                 }  
  136.             }  
  137.         } finally {  
  138.             rs.close();  
  139.         }  
  140.     }  
  141.   
  142.     /* 
  143.      * 遍历查询hbase表 
  144.      * 
  145.      * @tableName 表名 
  146.      */  
  147.     public static void getResultScann(String tableName, String start_rowkey,  
  148.                                       String stop_rowkey) throws IOException {  
  149.         Scan scan = new Scan();  
  150.         scan.setStartRow(Bytes.toBytes(start_rowkey));  
  151.         scan.setStopRow(Bytes.toBytes(stop_rowkey));  
  152.         ResultScanner rs = null;  
  153.         Table table = connection.getTable(TableName.valueOf(tableName));  
  154.         try {  
  155.             rs = table.getScanner(scan);  
  156.             for (Result r : rs) {  
  157.                 for (Cell cell : r.listCells()) {  
  158.                     System.out.println("row:" + Bytes.toString(cell.getRowArray(),cell.getRowOffset(),cell.getRowLength()));  
  159.                     System.out.println("family:"  
  160.                             + Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getValueLength()));  
  161.                     System.out.println("qualifier:"  
  162.                             + Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));  
  163.                     System.out  
  164.                             .println("value:" + Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));  
  165.                     System.out.println("timestamp:" + cell.getTimestamp());  
  166.                     System.out  
  167.                             .println("-------------------------------------------");  
  168.                 }  
  169.             }  
  170.         } finally {  
  171.             rs.close();  
  172.         }  
  173.     }  
  174.   
  175.     /* 
  176.      * 查询表中的某一列 
  177.      * 
  178.      * @tableName 表名 
  179.      * 
  180.      * @rowKey rowKey 
  181.      */  
  182.     public static void getResultByColumn(String tableName, String rowKey,  
  183.                                          String familyName, String columnName) throws IOException {  
  184.         Table table = connection.getTable(TableName.valueOf(tableName));  
  185.         Get get = new Get(Bytes.toBytes(rowKey));  
  186.         get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); // 获取指定列族和列修饰符对应的列  
  187.         Result result = table.get(get);  
  188.         for (Cell cell : result.listCells()) {  
  189.             System.out.println("family:"  
  190.                     + Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getValueLength()));  
  191.             System.out.println("qualifier:"  
  192.                     + Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));  
  193.             System.out  
  194.                     .println("value:" + Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));  
  195.             System.out.println("timestamp:" + cell.getTimestamp());  
  196.             System.out.println("-------------------------------------------");  
  197.         }  
  198.     }  
  199.   
  200.     /* 
  201.      * 更新表中的某一列 
  202.      * 
  203.      * @tableName 表名 
  204.      * 
  205.      * @rowKey rowKey 
  206.      * 
  207.      * @familyName 列族名 
  208.      * 
  209.      * @columnName 列名 
  210.      * 
  211.      * @value 更新后的值 
  212.      */  
  213.     public static void updateTable(String tableName, String rowKey,  
  214.                                    String familyName, String columnName, String value)  
  215.             throws IOException {  
  216.         Table table = connection.getTable(TableName.valueOf(tableName));  
  217.         Put put = new Put(Bytes.toBytes(rowKey));  
  218.         put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName),  
  219.                 Bytes.toBytes(value));  
  220.         table.put(put);  
  221.         System.out.println("update table Success!");  
  222.     }  
  223.   
  224.     /* 
  225.      * 查询某列数据的多个版本 
  226.      * 
  227.      * @tableName 表名 
  228.      * 
  229.      * @rowKey rowKey 
  230.      * 
  231.      * @familyName 列族名 
  232.      * 
  233.      * @columnName 列名 
  234.      */  
  235.     public static void getResultByVersion(String tableName, String rowKey,  
  236.                                           String familyName, String columnName) throws IOException {  
  237.         Table table = connection.getTable(TableName.valueOf(tableName));  
  238.         Get get = new Get(Bytes.toBytes(rowKey));  
  239.         get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName));  
  240.         get.setMaxVersions(5);  
  241.         Result result = table.get(get);  
  242.         for (KeyValue kv : result.list()) {  
  243.             System.out.println("family:" + Bytes.toString(kv.getFamily()));  
  244.             System.out  
  245.                     .println("qualifier:" + Bytes.toString(kv.getQualifier()));  
  246.             System.out.println("value:" + Bytes.toString(kv.getValue()));  
  247.             System.out.println("Timestamp:" + kv.getTimestamp());  
  248.             System.out.println("-------------------------------------------");  
  249.         }  
  250.         /* 
  251.          * List<?> results = table.get(get).list(); Iterator<?> it = 
  252.          * results.iterator(); while (it.hasNext()) { 
  253.          * System.out.println(it.next().toString()); } 
  254.          */  
  255.     }  
  256.   
  257.     /* 
  258.      * 删除指定的列 
  259.      * 
  260.      * @tableName 表名 
  261.      * 
  262.      * @rowKey rowKey 
  263.      * 
  264.      * @familyName 列族名 
  265.      * 
  266.      * @columnName 列名 
  267.      */  
  268.     public static void deleteColumn(String tableName, String rowKey,  
  269.                                     String falilyName, String columnName) throws IOException {  
  270.         Table table = connection.getTable(TableName.valueOf(tableName));  
  271.         Delete deleteColumn = new Delete(Bytes.toBytes(rowKey));  
  272.         deleteColumn.addColumn(Bytes.toBytes(falilyName),  
  273.                 Bytes.toBytes(columnName));  
  274.         table.delete(deleteColumn);  
  275.         System.out.println(falilyName + ":" + columnName + "is deleted!");  
  276.     }  
  277.   
  278.     /* 
  279.      * 删除指定的列 
  280.      * 
  281.      * @tableName 表名 
  282.      * 
  283.      * @rowKey rowKey 
  284.      */  
  285.     public static void deleteAllColumn(String tableName, String rowKey)  
  286.             throws IOException {  
  287.         Table table = connection.getTable(TableName.valueOf(tableName));  
  288.         Delete deleteAll = new Delete(Bytes.toBytes(rowKey));  
  289.         table.delete(deleteAll);  
  290.         System.out.println("all columns are deleted!");  
  291.     }  
  292.   
  293.     /* 
  294.      * 删除表 
  295.      * 
  296.      * @tableName 表名 
  297.      */  
  298.     public static void deleteTable(String tableName) throws IOException {  
  299.         Admin admin = connection.getAdmin();  
  300.         admin.disableTable(TableName.valueOf(tableName));  
  301.         admin.deleteTable(TableName.valueOf(tableName));  
  302.         System.out.println(tableName + "is deleted!");  
  303.     }  
  304.   
  305.     public static void main(String[] args) throws Exception {  
  306.   
  307.         // 创建表  
  308.         String tableName = "blog2";  
  309.         String[] family = { "article""author" };  
  310.          creatTable(tableName, family);  
  311.   
  312.         // 为表添加数据  
  313.   
  314.         String[] column1 = { "title""content""tag" };  
  315.         String[] value1 = {  
  316.                 "Head First HBase",  
  317.                 "HBase is the Hadoop database. Use it when you need random, realtime read/write access to your Big Data.",  
  318.                 "Hadoop,HBase,NoSQL" };  
  319.         String[] column2 = { "name""nickname" };  
  320.         String[] value2 = { "nicholas""lee" };  
  321.         addData("rowkey1""blog2", column1, value1, column2, value2);  
  322.         addData("rowkey2""blog2", column1, value1, column2, value2);  
  323.         addData("rowkey3""blog2", column1, value1, column2, value2);  
  324.   
  325.         // 遍历查询  
  326.         getResultScann("blog2""rowkey4""rowkey5");  
  327.         // 根据row key范围遍历查询  
  328.         getResultScann("blog2""rowkey4""rowkey5");  
  329.   
  330.         // 查询  
  331.         getResult("blog2""rowkey1");  
  332.   
  333.         // 查询某一列的值  
  334.         getResultByColumn("blog2""rowkey1""author""name");  
  335.   
  336.         // 更新列  
  337.         updateTable("blog2""rowkey1""author""name""bin");  
  338.   
  339.         // 查询某一列的值  
  340.         getResultByColumn("blog2""rowkey1""author""name");  
  341.   
  342.         // 查询某列的多版本  
  343.         getResultByVersion("blog2""rowkey1""author""name");  
  344.   
  345.         // 删除一列  
  346.         deleteColumn("blog2""rowkey1""author""nickname");  
  347.   
  348.         // 删除所有列  
  349.         deleteAllColumn("blog2""rowkey1");  
  350.   
  351.         // 删除表  
  352.         deleteTable("blog2");  
  353.   
  354.     }  
  355. }  

这篇关于Hbase1.2.0以后 JavaAPI最新接口调用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/487675

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr