尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

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

借助 Apache Phoenix,使用标准 SQL 和 JDBC 接口来操作 HBase
📅 发布时间:2026/6/17 23:32:06

注:本篇博客是对 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就好了。

相关新闻

  • 9月22日
  • 20250922
  • 基于springboot的图书进销存管理系统 - 详解

最新新闻

  • 传统观念分散持仓越多风险越低,编程逐步增加持仓个股数量,测算组合波动率拐点,找到最优分散上限。
  • 2026知名GEO服务商大盘点!不同场景选型攻略全覆盖 - 品牌测评鉴赏家
  • 如何快速掌握SuperCom串口调试工具:从零开始的终极使用指南
  • PyCaret低代码实现房价预测:从数据准备到模型上线全链路
  • 【Springboot毕设全套源码+文档】基于springboot的智慧仓库(丰富项目+远程调试+讲解+定制)
  • 2026年6月PE排水管企业推荐指南 - 多才菠萝

日新闻

  • 2026年不锈钢卷板厂家推荐排行榜:冷轧热轧/304/201不锈钢卷板,高颜值耐腐蚀源头厂家实力精选 - 企业推荐官【官方】
  • FLUX.1-dev FP8模型实战指南:24GB以下显卡高效部署方案
  • 2026佛山长途搬家价目表:跨省跨市搬家费用完整计算指南 - 从来都是英雄出少年

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号