ClickHouse應用場景:
1.絕大多數請求都是用於讀訪問的 2.數據需要以大批次(大於1000行)進行更新,而不是單行更新;或者根本沒有更新操作 3.數據只是添加到數據庫,沒有必要修改 4.讀取數據時,會從數據庫中提取出大量的行,但只用到一小部分列 5.表很“寬”,即表中包含大量的列 6.查詢頻率相對較低(通常每台服務器每秒查詢數百次或更少) 7.對於簡單查詢,允許大約50毫秒的延遲 8.列的值是比較小的數值和短字符串(例如,每個URL只有60個字節) 9.在處理單個查詢時需要高吞吐量(每台服務器每秒高達數十億行) 10.不需要事務 11.數據一致性要求較低 12.每次查詢中只會查詢一個大表。除了一個大表,其余都是小表 13.查詢結果顯著小於數據源。即數據有過濾或聚合。返回結果不超過單個服務器內存大小
行式存儲對比列式存儲:
(1)、行式數據
(2)、列式數據
(3)、對比分析
分析類查詢,通常只需要讀取表的一小部分列。在列式數據庫中可以只讀取需要的數據。數據總是打包成批量讀取的,所以壓縮是非常容易的。同時數據按列分別存儲這也更容易壓縮。這進一步降低了I/O的體積。由於I/O的降低,這將幫助更多的數據被系統緩存。
整合Springboot:
核心依賴(mybatis plus做持久層,druid做數據源):
<dependencies> <!--clickhouse--> <dependency> <groupId>ru.yandex.clickhouse</groupId> <artifactId>clickhouse-jdbc</artifactId> <version>0.3.1-patch</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.6</version> </dependency> <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> </dependencies>
配置yml文件:
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource click: driverClassName: ru.yandex.clickhouse.ClickHouseDriver url: jdbc:clickhouse://127.0.0.1:8123/dbname username: username password: 123456 initialSize: 10 maxActive: 100 minIdle: 10 maxWait: 6000 mybatis-plus: mapper-locations: classpath*:mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl map-underscore-to-camel-case: true cache-enabled: true lazy-loading-enabled: true multiple-result-sets-enabled: true use-generated-keys: true default-statement-timeout: 60 default-fetch-size: 100 type-aliases-package: com.example.tonghp.entity
ClickHouse與Druid連接池配置類:
參數配置:
package com.example.tonghp.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @author: tonghp * @create: 2021/07/26 16:23 */ @Data @Component @ConfigurationProperties(prefix = "spring.datasource.click") public class JdbcParamConfig { private String driverClassName ; private String url ; private Integer initialSize ; private Integer maxActive ; private Integer minIdle ; private Integer maxWait ; private String username; private String password; // 省略 GET 和 SET }
Druid連接池配置
package com.example.tonghp.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.annotation.Resource; import javax.sql.DataSource; import javax.swing.*; /** * @author: tonghp * @create: 2021/07/26 16:22 */ @Configuration public class DruidConfig { @Resource private JdbcParamConfig jdbcParamConfig ; @Bean public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(jdbcParamConfig.getUrl()); datasource.setDriverClassName(jdbcParamConfig.getDriverClassName()); datasource.setInitialSize(jdbcParamConfig.getInitialSize()); datasource.setMinIdle(jdbcParamConfig.getMinIdle()); datasource.setMaxActive(jdbcParamConfig.getMaxActive()); datasource.setMaxWait(jdbcParamConfig.getMaxWait()); datasource.setUsername(jdbcParamConfig.getUsername()); datasource.setPassword(jdbcParamConfig.getPassword()); return datasource; } }
接下來配置實體類,mapper,service,controlle以及mapper.xml。與mybatisplus操作mysql一樣的思路。
package com.example.tonghp.entity; import lombok.Data; import java.io.Serializable; /** * @author: tonghp * @create: 2021/07/26 16:31 */ @Data public class UserInfo implements Serializable { private static final long serialVersionUID = 1L; private int id; private String userName; private String passWord; private String phone; private String email; private String createDay; }
package com.example.tonghp.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.tonghp.entity.UserInfo; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.List; /** * @author: tonghp * @create: 2021/07/26 16:32 */ @Repository public interface UserInfoMapper extends BaseMapper<UserInfo> { // 寫入數據 void saveData (UserInfo userInfo) ; // ID 查詢 UserInfo selectById (@Param("id") Integer id) ; // 查詢全部 List<UserInfo> selectList () ; }
UserInfoMapper.xml
<?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.example.tonghp.mapper.UserInfoMapper"> <resultMap id="BaseResultMap" type="com.example.tonghp.entity.UserInfo"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="user_name" jdbcType="VARCHAR" property="userName" /> <result column="pass_word" jdbcType="VARCHAR" property="passWord" /> <result column="phone" jdbcType="VARCHAR" property="phone" /> <result column="email" jdbcType="VARCHAR" property="email" /> <result column="create_day" jdbcType="VARCHAR" property="createDay" /> </resultMap> <sql id="Base_Column_List"> id,user_name,pass_word,phone,email,create_day </sql> <insert id="saveData" parameterType="com.example.tonghp.entity.UserInfo" > INSERT INTO cs_user_info (id,user_name,pass_word,phone,email,create_day) VALUES (#{id,jdbcType=INTEGER},#{userName,jdbcType=VARCHAR},#{passWord,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},#{email,jdbcType=VARCHAR},#{createDay,jdbcType=VARCHAR}) </insert> <select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from cs_user_info where id = #{id,jdbcType=INTEGER} </select> <select id="selectList" resultMap="BaseResultMap" > select <include refid="Base_Column_List" /> from cs_user_info </select> </mapper>
Service
package com.example.tonghp.service; import com.baomidou.mybatisplus.extension.service.IService; import com.example.tonghp.entity.UserInfo; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Service; import java.util.List; /** * @author: tonghp * @create: 2021/07/26 16:46 */ public interface UserInfoService extends IService<UserInfo> { // 寫入數據 void saveData (UserInfo userInfo) ; // ID 查詢 UserInfo selectById (@Param("id") Integer id) ; // 查詢全部 List<UserInfo> selectList () ; }
ServiceImpl
package com.example.tonghp.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.tonghp.entity.UserInfo; import com.example.tonghp.mapper.UserInfoMapper; import com.example.tonghp.service.UserInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @author: tonghp * @create: 2021/07/26 16:48 */ @Service public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService { @Autowired UserInfoMapper userInfoMapper; @Override public void saveData(UserInfo userInfo) { userInfoMapper.saveData(userInfo); } @Override public UserInfo selectById(Integer id) { return userInfoMapper.selectById(id); } @Override public List<UserInfo> selectList() { return userInfoMapper.selectList(); } }
Controller
package com.example.tonghp.controller; import com.example.tonghp.entity.UserInfo; import com.example.tonghp.service.UserInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; /** * @author: tonghp * @create: 2021/07/26 16:45 */ @RestController @RequestMapping("user") public class UserInfoController { @Autowired private UserInfoService userInfoService ; @RequestMapping("saveData") public String saveData (){ UserInfo userInfo = new UserInfo () ; userInfo.setId(4); userInfo.setUserName("winter"); userInfo.setPassWord("567"); userInfo.setPhone("13977776789"); userInfo.setEmail("winter"); userInfo.setCreateDay("2020-02-20"); userInfoService.saveData(userInfo); return "sus"; } @RequestMapping("selectById") public UserInfo selectById () { return userInfoService.selectById(1) ; } @RequestMapping("selectList") public List<UserInfo> selectList () { return userInfoService.selectList() ; } }
main()
package com.example.tonghp; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.tonghp.mapper") public class TonghpApplication { public static void main(String[] args) { SpringApplication.run(TonghpApplication.class, args); } }
https://blog.csdn.net/weixin_46792649/article/details/115306384
https://www.cnblogs.com/ywjfx/p/14333974.html
https://www.cnblogs.com/cicada-smile/p/11632251.html
https://github.com/cicadasmile/middle-ware-parent
另外還有一種直接操作JDBC的:
https://blog.csdn.net/Alice_qixin/article/details/84957380