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

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

Andrej Karpathy最新采访:认知核心模型10亿参数就够了,AI会打破教育不公的僵局

夕小瑶科技说 原创  作者 | 海野 AI圈子的红人,AI大神Andrej Karpathy,曾是OpenAI联合创始人之一,特斯拉AI总监。上一次的动态是官宣创办一家名为 Eureka Labs 的人工智能+教育公司 ,宣布将长期致力于AI原生教育。 近日,Andrej Karpathy接受了No Priors(投资博客)的采访,与硅谷知名投资人 Sara Guo 和 Elad G

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听