创建一个SpringBoot项目,可以先不选择依赖,待会再pom.xml里面再导入相应的依赖。
- spring-boot-starter-web(网页展示)
- impala-jdbc41(impala-jdbc连接)
- com.alibaba.druid(阿里巴巴数据源)
- mybatis-spring-boot-starter(SpringBoot整合Mybatis包)
- lombok(简化代码)
<?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.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.data</groupId>
<artifactId>impala-connect</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>impala-connect</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>impala</groupId>
<artifactId>impala-jdbc41</artifactId>
<version>2.6.4.1005</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
server:
port: 9001
spring:
application:
name: impala-connect
impala:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:impala://xx.xx.xx.xx:21050/default
driver-class-name: com.cloudera.impala.jdbc41.Driver
# 初始化大小,最小,最大
initialSize: 5
minIdle: 5
maxIdle: 100
maxActive: 200
# 配置获取连接等待超时的时间
maxWait: 5000
# 每次使用连接时进行校验,会影响系统性能。默认为false
testOnBorrow: false
#
testOnReturn: false
# 验证使用的SQL语句
validationQuery: SELECT 1
# 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除
testWhileIdle: true
# 每30秒运行一次空闲连接回收器(默认-1)
timeBetweenEvictionRunsMillis: 30000
# 池中的连接空闲30分钟后被回收(默认30分钟)
minEvictableIdleTimeMillis: 1800000
# 在每次空闲连接回收器线程(如果有)运行时检查的连接数量(设置为和maxIdle一样)
numTestsPerEvictionRun: 100
mybatis:
type-aliases-package: com.data.model
impala-mapper-locations: classpath*:impalaMapping/*.xml
这个配置主要使用阿里巴巴数据源配置Impala的一些连接配置,如路径、驱动以及mybatis的扫描包路径等。
package com.data.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.data.mapper.impala", sqlSessionFactoryRef = "impalaSessionFactory")
public class ImpalaSourceConfig {
@Value("${spring.impala.url}")
private String url;
@Value("${spring.impala.driver-class-name}")
private String driverClass;
@Value("${spring.impala.maxActive}")
private Integer maxActive;
@Value("${spring.impala.initialSize}")
private Integer initialSize;
@Value("${spring.impala.minIdle}")
private Integer minIdle;
@Value("${spring.impala.maxWait}")
private Integer maxWait;
@Value("${mybatis.type-aliases-package}")
private String PACKAGE;
@Value("${mybatis.impala-mapper-locations}")
private String LOCATION;
@Bean(name = "impalaDataSource")
public DataSource impalaDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setMaxActive(maxActive);
dataSource.setInitialSize(initialSize);
dataSource.setMinIdle(minIdle);
dataSource.setMaxWait(maxWait);
return dataSource;
}
@Bean(name = "impalaTransactionManager")
public DataSourceTransactionManager impalaTransactionManager() {
return new DataSourceTransactionManager(impalaDataSource());
}
@Bean(name = "impalaSessionFactory")
public SqlSessionFactory impalaSessionFactory() throws Exception {
final SqlSessionFactoryBean SESSIONFACTORY = new SqlSessionFactoryBean();
SESSIONFACTORY.setDataSource(impalaDataSource());
SESSIONFACTORY.setTypeAliasesPackage(PACKAGE);
System.out.println("------------------->"+PACKAGE);
System.out.println("---------------------->"+LOCATION);
SESSIONFACTORY.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(LOCATION));
SESSIONFACTORY.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);//该配置将数据库中下划线自动转成驼峰命名的变量,并且只针对自定义的实体类生效,对map不生效的
return SESSIONFACTORY.getObject();
}
}
package com.data.model;
import lombok.Data;
@Data
public class Person {
private Integer id;
private String name;
}
package com.data.mapper.impala;
import com.data.model.Person;
import org.springframework.stereotype.Repository;
@Repository
public interface ImpalaMapper {
void executeSql(Person person);
}
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.data.mapper.impala.ImpalaMapper">
<insert id="executeSql" parameterType="com.data.model.Person">
insert into test_person values(#{id},#{name});
</insert>
</mapper>
package com.data.services;
import com.data.mapper.impala.ImpalaMapper;
import com.data.model.Person;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class ImpalaService {
@Resource
private ImpalaMapper impalaMapper;
public void executeSql(Person person) {
try {
impalaMapper.executeSql(person);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.data.controller;
import com.data.model.Person;
import com.data.services.ImpalaService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class ImpalaController {
@Resource
private ImpalaService impalaService;
@GetMapping("/test")
public String insertPerson() {
Person person = new Person();
person.setId(1);
person.setName("张三");
impalaService.executeSql(person);
return "新增记录成功!";
}
}
启动ImpalaConnectApplication.class,打开http://localhost:9001/test,若能出现如此画面
说明你已经成功了,接下来我们去Impala数据库去验证一下,执行命令select * from test_person
,出现下面的画面,恭喜你已经完成这一整个项目啦!!!
最后,我们来总结一下,首先导入相应的依赖包以及配置好application.yml,接着使用阿里巴巴数据源搭建好impala连接,然后根据自己的需求编写中间的实现逻辑,最后就可以去验证自己的功能啦。