SpringBoot中使用MybatisPlus集成MySQL与Hive进行数据分析
# SpringBoot中使用MybatisPlus集成MySQL与Hive进行数据分析
# pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<!-- MyBatis-Plus 多数据源-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.5.6</version>
</dependency>
<!--Hive 配置 需要查看对应的版本填写version-->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.3.4</version>
</dependency>
<!-- Hadoop 需要查看对应的版本填写version -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.9.2</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 配置文件
这里只配置一个hive数据源就可以了,MySQL的数据源正常配置,两个放一起会报SA模式的错误。可能我这个算不上给MybatisPlus配置多数据源,但是他一直报错我实在没办法了,找不到咋解决😰。跟噩梦一样的报错 ,于是就想着先上一个看能不能通,结果就通了。最后出现一个大胆的想法就是MySQL正常配置看看会是啥结果,然后就全可以用了。。。
application.yaml
# MyBatis-Plus 多数据源
spring:
datasource:
dynamic:
primary: mysql #设置默认的数据源或者数据源组,默认值即为master
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
# mysql:
# url: jdbc:mysql://your_mysql_ip:3306/your_database_name
# username: your_username
# password: your_password
# driver-class-name: com.mysql.cj.jdbc.Driver
# type: com.alibaba.druid.pool.DruidDataSource
hive:
url: jdbc:hive2://your_hive_ip:10000/your_database_name
username: your_username
password: your_password
driver-class-name: org.apache.hive.jdbc.HiveDriver
url: jdbc:mysql://your_mysql_ip:3306/your_database_name
username: your_username
password: your_password
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Controller
@RestController
@RequestMapping("/hive")
public class HiveController {
@Autowired
private JdbcTemplate jdbcTemplate;
// @DS 注解要标在执行 SQL 语句用到的数据源为非主要数据源的方法上,value 为 配置文件中的(此例为 hive)
@DS("hive")
@RequestMapping("/select")
public Object select() {
// 要执行的 SQL
String sql = "SELECT * FROM shop_pc";
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
log.info(rows.toString());
return rows;
}
}
@RestController
@RequestMapping("/admin")
public class AdminController {
@Resource
private GoodsService goodsService;
/**
* 分页查询商品信息
* @param index : 当前页
* @param count : 查询个数
* @return Page
*/
@GetMapping("/select/{index}/{count}")
public Result<Page<Goods>> selectList(@PathVariable Integer index, @PathVariable Integer count) {
if (index < 0 || count < 0){
return Result.error("请求参数有误");
}
Page<Goods> goodsPage = goodsService.selectPage(index, count);
return Result.success("请求成功!", goodsPage);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 验证
MySQL正常使用
hive也能使用