通过阅读列式数据库ClickHouse官网,不难看出它有支持JDBC规范的驱动jar包,可以直接集成到Object Relational Mapping框架等,下面我用SpringBoot+MybatisPlus环境连接ClickHouse来演示一下
集成步骤
1.Maven引入ClickHouse提供的JDBC依赖
<!-- ClickHouse JDBC 依赖 -->
<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.1.53</version>
</dependency>
2.数据源配置
下面数据源配置代表多数据源配置,单数据源配置按照单格式写即可
本人搭建的ClickHouse没有账密,它默认就是default用户,然后默认连接default库
spring:
datasource:
dynamic:
primary: clickHouseDataSource
datasource:
clickHouseDataSource:
url: jdbc:clickhouse://IP:端口号/default
username: default
password:
driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
3.数据Entity对象创建
建表相关操作请见ClickHouse复杂查询单表亿级数据案例(可导出Excel)_clickhouse单表查询-CSDN博客
@Data
@TableName("sales")
public class Sales {
@TableId(type = IdType.AUTO)
private Long id;
private Long productId;
private Integer quantity;
private Double price;
private Date timestamp;
private Long customerId;
private Double discount;
private Double totalAmount;
private String paymentMethod;
private String status;
private String shippingAddress;
private String billingAddress;
private String orderNotes;
private Date createdAt;
private Date updatedAt;
private Double shippingCost;
private Double tax;
private String orderSource;
private String fulfillmentStatus;
private String productName;
private String productCategory;
}
4.基本架构搭建Mapper、Service、ServiceImpl、Controller
@Mapper
public interface SalesMapper extends BaseMapper<Sales> {
}
public interface ISalesService extends IService<Sales> {
}
@Service
public class SalesServiceImpl extends ServiceImpl<SalesMapper, Sales> implements ISalesService {
}
@RestController
@RequestMapping("/sales")
public class SalesController {
@GetMapping("/{id}")
@DS("clickHouseDataSource")
public AppResponse<?> getSalesById(@PathVariable Long id) {
LambdaQueryWrapper<Sales> queryWrapper=new LambdaQueryWrapper();
// 数据多,先查询10条试试
queryWrapper.last("limit 10");
return AppResponse.success(salesService.list(queryWrapper));
}
}
5.尝试调用
此时我们开足马力启动项目,调用接口访问这个查询,拿到了数据
还有很多产品都是基于ClickHouse进行二次开发或者加强功能,比如火山云的ByteHouse等,这个组件使用率的上升趋势还是很明显的