借助 Apache Phoenix,使用标准 SQL 和 JDBC 接口来操作 HBase

news/2025/9/22 20:23:20/文章来源:https://www.cnblogs.com/ztn195/p/19105965

注:本篇博客是对 https://www.cnblogs.com/shanheyongmu/p/15661006.html 这篇博客的补充与实践。 在此膜拜大佬!d(゚∀゚d)点赞!

点击查看代码

package com.example;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@RestController
public class HelloController {//phoenix驱动private String phoenixDriver = "org.apache.phoenix.jdbc.PhoenixDriver";//zookeeper地址private String phoenixURL = "jdbc:phoenix:node1:2181";@GetMapping("/test")public void test() throws Exception {// 创建表System.out.println("\n--- 开始创建 student 表 ---");createTable();// 获取Phoenix中的表(系统表除外)System.out.println("\n--- 获取Phoenix中的表(系统表除外) ---");List<String> tables = getTables();System.out.println(tables);// 插入数据System.out.println("\n--- 开始插入数据 ---");insertData();// 删除数据System.out.println("\n--- 开始删除数据 ---");deleteData();// 查询数据System.out.println("\n--- 开始查询数据 ---");List<Map<String, String>> list = getData("\"student\"");System.out.println(list);//删除表System.out.println("\n--- 开始删除 student 表 ---");dropTable();}// 获取连接public Connection getConnection() throws Exception {Class.forName(phoenixDriver);return DriverManager.getConnection(phoenixURL);}// 创建表public void createTable() throws Exception {//获取连接Connection connection = getConnection();// 创建Statement对象String sql = "CREATE TABLE IF NOT EXISTS \"student\"(" +"id VARCHAR primary key," +"name VARCHAR," +"age VARCHAR)";PreparedStatement statement = connection.prepareStatement(sql);// 执行sql操作statement.execute();// 关闭statement.close();connection.close();}// 获取Phoenix中的表(系统表除外)public List<String> getTables() throws Exception {//获取连接Connection connection = getConnection();List<String> tables = new ArrayList<>();DatabaseMetaData metaData = connection.getMetaData();String[] types = {"TABLE"}; //"SYSTEM TABLE"ResultSet resultSet = metaData.getTables(null, null, null, types);while (resultSet.next()) {tables.add(resultSet.getString("TABLE_NAME"));}return tables;}// 删除表public void dropTable() throws Exception {//获取连接Connection connection = getConnection();// 创建Statement对象String sql = "DROP TABLE \"student\"";PreparedStatement statement = connection.prepareStatement(sql);// 执行sql操作statement.execute();// 关闭statement.close();connection.close();}// 插入数据public void insertData() throws Exception {//获取连接Connection connection = getConnection();//获取Statement对象,并进行数据插入Statement statement = connection.createStatement();statement.executeUpdate("upsert into \"student\" values('1001','大刘','20')");statement.executeUpdate("upsert into \"student\" values('1002','小星','22')");connection.commit();statement.close();//获取PreparedStatement对象,并进行数据插入PreparedStatement preparedStatement = connection.prepareStatement("upsert into \"student\" values(?,?,?)");//给参数赋值preparedStatement.setString(1,"1003");preparedStatement.setString(2,"hangge");preparedStatement.setString(3,"1000");//执行插入preparedStatement.execute();connection.commit();preparedStatement.close();connection.close();}// 删除数据public void deleteData() throws Exception {//获取连接Connection connection = getConnection();//获取Statement对象,并进行数据删除Statement statement = connection.createStatement();statement.execute("delete from \"student\" where id = '1002'");connection.commit();statement.close();connection.close();}// 查询数据(获取表中的所有数据)public List<Map<String, String>> getData(String tableName) throws Exception {//获取连接Connection connection = getConnection();String sql = "SELECT * FROM " + tableName;PreparedStatement preparedStatement = connection.prepareStatement(sql);ResultSet resultSet = preparedStatement.executeQuery();ResultSetMetaData resultSetMetaData = resultSet.getMetaData();List<Map<String, String>> resultList = new ArrayList<>();while (resultSet.next()) {Map<String, String> result = new HashMap<>();for (int i = 1, len = resultSetMetaData.getColumnCount(); i <= len; i++) {result.put(resultSetMetaData.getColumnName(i), resultSet.getString(i));}resultList.add(result);}return resultList;}
}

注意事项:

遇到问题:

Class [org.apache.jasper.servlet.JspServlet] is not a Servlet
Caused by: java.lang.ClassCastException: class org.apache.jasper.servlet.JspServlet cannot be cast to class jakarta.servlet.Servlet

这是一个典型的 JSP 与 Tomcat 10 兼容性 问题。

直接用我的pom.xml文件就好了,但是Phoenix版本和springboot版本尽量和我一致。

点击查看代码

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.5.6</version><relativePath/></parent><groupId>com.example</groupId><artifactId>back-Phoenix</artifactId><version>0.0.1-SNAPSHOT</version><name>back-Phoenix</name><description>back-Phoenix</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency><!-- 使用 Jetty 替代 Tomcat --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency><!-- 引入log4j2依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId></dependency><!-- phoenix相关依赖配置 --><dependency><groupId>org.apache.phoenix</groupId><artifactId>phoenix-core</artifactId><version>5.0.0-HBase-2.0</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion><exclusion><groupId>joda-time</groupId><artifactId>joda-time</artifactId></exclusion><exclusion><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId></exclusion><exclusion><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId></exclusion><exclusion><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId></exclusion><exclusion><groupId>org.glassfish.web</groupId><artifactId>javax.servlet.jsp</artifactId></exclusion><exclusion><groupId>org.mortbay.jetty</groupId><artifactId>*</artifactId></exclusion><exclusion><groupId>tomcat</groupId><artifactId>*</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.9.2</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion><exclusion><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId></exclusion><exclusion><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId></exclusion><exclusion><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId></exclusion><exclusion><groupId>org.mortbay.jetty</groupId><artifactId>*</artifactId></exclusion><exclusion><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></exclusion></exclusions></dependency><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>2.3</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId></exclude></excludes></configuration></plugin></plugins></build>
</project>

注意直接运行大概率会碰到

java.sql.SQLException: ERROR 726 (43M10): Inconsistent namespace mapping properties. Cannot initiate connection as SYSTEM:CATALOG is found but client does not have phoenix.schema.isNamespaceMappingEnabled enabled

这是因为Phoenix 开启了 SCHEMA。

直接参考 https://www.cnblogs.com/shanheyongmu/p/15661287.html
这篇博客中关闭SCHEMA就好了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/910260.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode:15.转轮数组 - 详解

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

sem网络营销重庆seo研究中心

来源&#xff1a;知乎&#xff08;zibuyu9&#xff09;作者&#xff1a;韩旭、高天宇、刘知远最近几年深度学习引发的人工智能浪潮席卷全球&#xff0c;在互联网普及带来的海量数据资源和摩尔定律支配下飞速提升的算力资源双重加持下&#xff0c;深度学习深入影响了自然语言处理…

长春做网站长春网站设计浙江省龙泉市建设局网站

目录 一、UART 概述二、UART 模块相关API三、UART 接口调用实例四、UART HDF驱动开发4.1、开发步骤(待续...) 坚持就有收获 一、UART 概述 UART 是通用异步收发传输器&#xff08;Universal Asynchronous Receiver/Transmitter&#xff09;的缩写&#xff0c;是通用串行数据总…

专业建设网站技术wordpress数据迁移

文章目录 &#x1f56e;原始图像&#x1f56e;改变图像大小&#x1f56e;使图像靠左 在 jupyter notebook中&#xff0c;导入的图片过大&#xff0c;想要改变图像的大小 &#x1f56e;原始图像 &#x1f56e;改变图像大小 复制小括号里面的内容到src后面&#xff0c;满足<…

东莞购物网站建设工商注册是什么意思

最近看了个HS的时间显示的例子&#xff0c;顺便学习了一下这个lua定义函数的方法&#xff0c;被折腾了许久&#xff0c;最后竟然是gpt解答了。 定义方式 -- 定义一个对象 local myObject {isVisible false, }-- 定义对象的方法 function myObject:toggleShow()self.isVisibl…

珠宝首饰网站建设策划书公司建品牌网站好

观察题目我们发现从前往后推会有条件判断&#xff0c;不容易写出来。所以就从后往前推。 也就是说后面的状态已经是推出来了&#xff0c;保证是最大值。 //数字三角形 #include<iostream> using namespace std; const int N 510; int f[N][N], n;int main() {ios::sync…

汝城网站建设公司东海县建网站

软件的特征 抽象&#xff1a; 不可触摸&#xff0c;逻辑实体&#xff0c;可记录&#xff0c;但看不到复制成本低&#xff1a;不受物质材料的限制&#xff0c;不受物理定律或加工过程的制约&#xff0c;与开发成本相比&#xff0c;复制成本很低无折旧、受硬件制约、未完全摆脱手…

深圳坪山住房和建设局网站如何做网页快捷方式

Check Point Sofrware Technologies很可能成为下一个会产生收购案的主流安全厂商&#xff0c;首席执行官Gil Shwed在该公司第二季度财报电话会议上这样表示。 “我们正在积极地寻求收购目标&#xff0c;期待无论是大规模的还是小规模的扩张&#xff0c;”Shwed表示。“我们在并…

行业门户型网站制作临沂城乡建设管理局网站

1.73. 矩阵置零 给定一个 m x n 的矩阵&#xff0c;如果一个元素为 0 &#xff0c;则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。 提示&#xff1a; m matrix.lengthn matrix[0].length1 < m, n < 200-2^31 < matrix[i][j] < 2^31 - 1 思路&#xf…

手机网站网站建设雅安交通建设集团网站

当研究oracle的查找方式中发现&#xff0c;在全表扫描和索引扫描时&#xff0c;会用到db_file_multiblock_read_count来一次读取多个IO。也看了一些关于db_file_multiblock_read_count的文章&#xff0c;为加深自己的理解&#xff0c;特做个试验研究。 以下是reference对它的解…

做网站的标准流程视觉设计师作品集

文章目录1、static存在的主要意义2、static的独特之处3、static应用场景4、静态变量和实例变量的概念5、静态变量和实例变量区别【重点常用】6、访问静态变量和实例变量的两种方式7、static静态方法8、static静态代码块9、static变量与普通变量区别10、静态内部类11、静态导包1…

网站开发工程师面试问哪些问题南京做网站南京乐识赞

爬虫项目实战0x01 目标分析最近发现一个比较好的欧美音乐下载网站&#xff0c;可以下载大部分高质量欧美音乐。该爬虫项目要实现自动化批量获取用户想要下载的音乐。本文从网站分析、爬虫设计、代码实现三个方面出发&#xff0c;系统介绍该爬虫项目。项目完整代码在Github中可以…

网站建设博客作业动漫制作专业就业方向和前景

CentOS6.5安装详细教程1、准备好CentOS-6.5.iso文件&#xff0c;并刻录到光盘中&#xff0c;放入光驱&#xff0c;重启服务器&#xff0c;修改BIOS为光驱启动&#xff0c;之后开始进入安装&#xff1a;2、按任意键&#xff0c;进入引导菜单。按上下键&#xff0c;移动光标&…

外贸推广建站公司百度网站推广服务商

一、前言 本文使用的虚幻引擎5.3.2&#xff0c;继点击场景3D物体的两种处理方式的基础完成对3D物体的点击触发后&#xff0c;我们需要制作一个可以弹窗显示该物体信息的UI面板&#xff0c;同时保证弹窗可以跟随物体。另外还讲了一种UI上的悬浮提示跟随弹窗。 二、实现 2.1、创…

网站开发的学习方法室内设计效果图qq群

文章目录1. 题目2. DFS 解题1. 题目 给定一个有 N 个结点的二叉树的根结点 root&#xff0c;树中的每个结点上都对应有 node.val 枚硬币&#xff0c;并且总共有 N 枚硬币。 在一次移动中&#xff0c;我们可以选择两个相邻的结点&#xff0c;然后将一枚硬币从其中一个结点移动…

四川建设工程网站新市区做网站

在你连接到 MySQL 数据库后&#xff0c;可能有多个可以操作的数据库&#xff0c;所以你需要选择你要操作的数据库。 从命令提示窗口中选择 MySQL 数据库 在 mysql> 提示窗口中可以很简单的选择特定的数据库。 在 MySQL 中&#xff0c;要选择要使用的数据库&#xff0c;可…

Android 中获取稳定时间的方法 - 指南

Android 中获取稳定时间的方法 - 指南2025-09-22 20:10 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !i…

【精品资料鉴赏】RPA财务机器人应用(基于UiPath)教材配套课件 - 详解

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

如何让AI生成多页面APP原型图?AI原型设计实用指南

引言 很多产品经理已经在实际项目中开始使用AI原型设计工具,尤其是在制作APP原型图、小程序原型图等产品项目中。AI可以帮助产品经理快速产出原型图,但是也有一些容易被忽视的实用功能。例如:如何一次性生成多个页面…

国外优秀网站建设公司淄博优化网站

默认为递增顺序&#xff1b;注&#xff1a;一下例子希望自己再次复习时&#xff0c;可以用笔在纸上画画内存图。 包括有: 选择排序冒泡排序插入排序 1.选择排序 <--------------------------------------选择排序---------------------------------------> 1、选择排…