springboot整合hive

springboot整合hive

pom.xml

xml 复制代码
<?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>2.7.18</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>cn.lhz</groupId>
  <artifactId>hive-api</artifactId>
  <version>0.0.1</version>
  <name>hive-api</name>
  <description>hive-api</description>
  <url/>
  <licenses>
    <license/>
  </licenses>
  <developers>
    <developer/>
  </developers>
  <scm>
    <connection/>
    <developerConnection/>
    <tag/>
    <url/>
  </scm>
  <properties>
    <java.version>21</java.version>
    <jdk.version>21</jdk.version>
    <maven.compiler.source>${jdk.version}</maven.compiler.source>
    <maven.compiler.target>${jdk.version}</maven.compiler.target>
    <maven.compiler.compilerVersion>${jdk.version}</maven.compiler.compilerVersion>
    <maven.compiler.encoding>utf-8</maven.compiler.encoding>
    <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.test.failure.ignore>true</maven.test.failure.ignore>
    <maven.test.skip>true</maven.test.skip>
    <commons-io.version>2.17.0</commons-io.version>
    <commons-lang3.version>3.17.0</commons-lang3.version>
    <druid.version>1.2.23</druid.version>
    <dom4j.version>2.1.4</dom4j.version>
    <fastjson.version>2.0.53</fastjson.version>
    <fastjson2.version>2.0.53</fastjson2.version>
    <gson.version>2.11.0</gson.version>
    <generator.version>1.1.2</generator.version>
    <hutool.version>5.8.32</hutool.version>
    <junit5.version>5.11.2</junit5.version>
    <jaxen.version>2.0.0</jaxen.version>
    <jackson.version>2.18.0</jackson.version>
    <lombok.version>1.18.34</lombok.version>
    <mybatis-plus.version>3.5.9</mybatis-plus.version>
    <mybatis-spring.version>3.0.4</mybatis-spring.version>
    <spring-boot.version>2.7.18</spring-boot.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>${mybatis-plus.version}</version>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus</artifactId>
      <version>${mybatis-plus.version}</version>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-jsqlparser</artifactId>
      <version>${mybatis-plus.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-all</artifactId>
      <version>${hutool.version}</version>
    </dependency>
    <dependency>
      <groupId>com.github.binarywang</groupId>
      <artifactId>java-testdata-generator</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>${commons-lang3.version}</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>${commons-io.version}</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>${gson.version}</version>
    </dependency>
    <dependency>
      <groupId>org.webjars</groupId>
      <artifactId>layui</artifactId>
      <version>2.9.10</version>
    </dependency>

    <!--com.alibaba.druid-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>${druid.version}</version>
    </dependency>

    <!--swagger3-->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-boot-starter</artifactId>
      <version>3.0.0</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>3.0.0</version>
    </dependency>
    <dependency>
      <groupId>com.github.xiaoymin</groupId>
      <artifactId>swagger-bootstrap-ui</artifactId>
      <version>1.9.6</version>
    </dependency>
    <dependency>
      <groupId>com.github.xiaoymin</groupId>
      <artifactId>knife4j-spring-ui</artifactId>
      <version>3.0.3</version>
    </dependency>

    <!-- hive-jdbc -->
    <dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-jdbc</artifactId>
      <version>3.1.3</version>
      <exclusions>
        <exclusion>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-slf4j-impl</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.eclipse.jetty.orbit</groupId>
          <artifactId>*</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.eclipse.jetty.aggregate</groupId>
          <artifactId>*</artifactId>
        </exclusion>
        <exclusion>
          <groupId>tomcat</groupId>
          <artifactId>*</artifactId>
        </exclusion>
        <exclusion>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.mortbay.jetty</groupId>
          <artifactId>*</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.13.0</version>
        <configuration>
          <encoding>UTF-8</encoding>
          <source>${jdk.version}</source>
          <target>${jdk.version}</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
            </exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
    <finalName>${project.name}</finalName>
  </build>
  <repositories>
    <repository>
      <id>public</id>
      <name>aliyun nexus</name>
      <url>https://maven.aliyun.com/repository/public</url>
      <releases>
        <enabled>true</enabled>
      </releases>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>public</id>
      <name>aliyun nexus</name>
      <url>https://maven.aliyun.com/repository/public</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </pluginRepository>
  </pluginRepositories>
</project>

配置文件

application.yml

yaml 复制代码
server:
  port: 8080
  servlet:
    context-path: /api
spring:
  application:
    name: hive-api
  mvc:
    path match:
      matching-strategy: ant_path_matcher
  jackson:
    serialization:
      FAIL_ON_EMPTY_BEANS: false
    # json 序列化排除值为 null 的属性
    default-property-inclusion: non_null
    # 配置 Date 类的时间格式,如果不涉及可以不加
    date-format: yyyy-MM-dd HH:mm:ss
    # 配置 Date 类的时区,如果不涉及可以不加
    time-zone: GMT+8
  thymeleaf:
    #缓冲的配置
    cache: false
    check-template: true
    check-template-location: true
    #开启MVC thymeleaf 视图解析
    enabled: true
    #模板的模式,支持 HTML, XML TEXT JAVASCRIPT
    mode: HTML5
    #编码 可不用配置
    encoding: UTF-8
    #配置模板路径,默认是templates,可以不用配置
    prefix: classpath:templates
    # 文件后缀名
    suffix: .html
    #内容类别,可不用配置
    servlet:
      content-type: text/html;charset=utf-8
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.apache.hive.jdbc.HiveDriver
    url: jdbc:hive2://lihaozhe03:10000/lihaozhe
    username: root
    password:
mybatis-plus:
  # 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml
  # 如果是放在resource目录 classpath:/mapper/*Mapper.xml
  mapper-locations: classpath:mapper/*.xml
  #实体扫描,多个package用逗号或者分号分隔
  type-aliases-package: cn.lhz.dto,cn.lhz.vo
  configuration:
    #配置返回数据库(column下划线命名&&返回java实体是驼峰命名),自动匹配无需as(没开启这个,SQL需要写as: select user_id as userId)
    map-underscore-to-camel-case: true
    cache-enabled: false
    #配置JdbcTypeForNull, oracle数据库必须配置
    jdbc-type-for-null: 'null'
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#是否激活 swagger true or false
springfox:
  documentation:
    enabled: true

工具类

util 包下的工具类
opeapi配置类

swagger配置类

java 复制代码
package cn.lhz.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.Inet4Address;
import java.net.UnknownHostException;

/**
 * @author 李昊哲
 * @version 1.0.0
 */
@Configuration
@Slf4j
public class OpenApiConfig implements ApplicationListener<WebServerInitializedEvent> {
  @Bean
  public OpenAPI springOpenAPI() {
    Contact contact = new Contact();
    contact.setName("李昊哲");
    contact.setUrl("https://space.bilibili.com/480308139");
    contact.setEmail("646269678@qq.com");
    // 访问路径:http://localhost:8080/swagger-ui/index.html
    // 访问路径:http://localhost:8080/doc.html
    return new OpenAPI().info(new Info()
        .title("SpringBoot API")
        .description("SpringBoot Simple Application")
        .contact(contact)
        .version("1.0.0"));
  }

  @Override
  public void onApplicationEvent(WebServerInitializedEvent event) {
    try {
      //获取IP
      String hostAddress = Inet4Address.getLocalHost().getHostAddress();
      //获取端口号
      int port = event.getWebServer().getPort();
      //获取应用名
      String applicationName = event.getApplicationContext().getApplicationName();
      // TODO:这个localhost改成host地址
      log.info("项目启动启动成功!接口文档地址: http://" + hostAddress + ":" + port + applicationName + "/doc.html");
      log.info("项目启动启动成功!接口文档地址: http://" + hostAddress + ":" + port + applicationName + "/swagger-ui/index.html");
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
  }
}
MybatisPlus配置类
java 复制代码
package cn.lhz.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author 李昊哲
 * @version 1.0.0
 */
@Configuration
public class MybatisPlusConfig {
  @Bean
  public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    // 于 v3.5.9 起,PaginationInnerInterceptor 已分离出来。如需使用,则需单独引入 mybatis-plus-jsqlparser 依赖
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    return interceptor;
  }
}
全局异常处理
java 复制代码
package cn.lhz.config;

import cn.lhz.util.response.ResponseResult;
import cn.lhz.util.response.ResultCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @author 李昊哲
 * @version 1.0.0
 */
@ControllerAdvice
@RestControllerAdvice
public class GlobalException {
  @ExceptionHandler(Exception.class)
  public ResponseResult<String> defaultErrorHandler(Exception e) {
    return new ResponseResult<>(ResultCode.EXCEPTION.getCode(), e.getMessage());
  }
}

模型类

java 复制代码
package cn.lhz.dto;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;

/**
 * @author 李昊哲
 * @version 1.0.0
 */
@Setter
@Getter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@TableName("person")
public class Person {
  @TableId(type = IdType.NONE)
  private String idCard;
  private String realName;
  private String mobile;
  private String provinceCode;
}

持久层接口

java 复制代码
package cn.lhz.mapper;

import cn.lhz.dto.Person;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
 * @author 李昊哲
 * @version 1.0.0
 */
@Mapper
public interface PersonMapper extends BaseMapper<Person> {
}

业务层接口

java 复制代码
package cn.lhz.service;

import cn.lhz.dto.Person;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;

/**
 * @author 李昊哲
 * @version 1.0.0
 */
public interface PersonService extends IService<Person> {
  Page<Person> pageList(int pageNum, int pageSize);
}

业务层实现类

java 复制代码
package cn.lhz.service.impl;

import cn.lhz.dto.Person;
import cn.lhz.mapper.PersonMapper;
import cn.lhz.service.PersonService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author 李昊哲
 * @version 1.0.0
 */
@Service
@RequiredArgsConstructor
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {
  private final PersonMapper personMapper;

  @Override
  public Page<Person> pageList(int pageNum, int pageSize) {
    return personMapper.selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<>());
  }
}

API接口

java 复制代码
package cn.lhz.controller;

import cn.lhz.dto.Person;
import cn.lhz.service.PersonService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 李昊哲
 * @version 1.0.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/person")
public class PersonController {
  private final PersonService personService;

  @GetMapping("/pageList")
  public Page<Person> pageList(
      @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
      @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
    return personService.pageList(pageNum, pageSize);
  }
}

测试

相关推荐
m0_7482517214 分钟前
Spring Boot——统一功能处理
java·spring boot·后端
dream21st34 分钟前
从零开始采用命令行创建uniapp vue3 ts springboot项目
spring boot·后端·uni-app
小蒜学长1 小时前
基于Spring Boot的宠物领养系统的设计与实现(代码+数据库+LW)
java·前端·数据库·spring boot·后端·旅游·宠物
B站计算机毕业设计超人1 小时前
计算机毕业设计hadoop+spark+hive民宿推荐系统 酒店推荐系统 民宿价格预测 酒店价格 预测 机器学习 深度学习 Python爬虫 HDFS集群
大数据·python·机器学习·spark·课程设计·数据可视化·推荐算法
AIGC大时代1 小时前
如何判断一个学术论文是否具有真正的科研价值?ChatGPT如何提供帮助?
大数据·人工智能·物联网·chatgpt·aigc
沙滩de流沙2 小时前
Spark生态圈
大数据·分布式·spark·scala
GraduationDesign2 小时前
基于SpringBoot的在线文档管理系统的设计与实现
java·spring boot·后端
xiaosannihaiyl242 小时前
Scala语言的函数实现
开发语言·后端·golang
墨鸦_Cormorant3 小时前
JDK 8 升级 17 及 springboot 2.x 升级 3.x 指南
java·spring boot
Atlim3 小时前
flink cdc使用flink sql方式运行一直报Make sure a planner module is on the classpath
大数据·sql·flink