• 采用springboot 2.7.10来操作clickhouse


    1、采用springboot与clickhouse结合,其实和操作mysql,oracle区别不大。直接上代码开干

    2、所采用的环境

    jdk1.8

    springboot 2.7.10

    clickhouse 22.8.3.13

    clickhouse 0.5.0

    3、项目的pom.xml文件

    1. <dependency>
    2. <groupId>com.clickhouse</groupId>
    3. <artifactId>clickhouse-jdbc</artifactId>
    4. <version>0.5.0</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.clickhouse</groupId>
    8. <artifactId>clickhouse-client</artifactId>
    9. <version>0.5.0</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>com.clickhouse</groupId>
    13. <artifactId>clickhouse-http-client</artifactId>
    14. <version>0.5.0</version>
    15. </dependency>
    16. <dependency>
    17. <groupId>org.apache.httpcomponents.client5</groupId>
    18. <artifactId>httpclient5</artifactId>
    19. <version>5.2.1</version>
    20. </dependency>
    21. <dependency>
    22. <groupId>org.apache.httpcomponents.client5</groupId>
    23. <artifactId>httpclient5-fluent</artifactId>
    24. <version>5.1.3</version>
    25. </dependency>

    4、application.yml配置

    1. spring:
    2. application:
    3. name: demobigdata
    4. profiles:
    5. active: mybatis
    6. datasource:
    7. type: com.alibaba.druid.pool.DruidDataSource
    8. druid:
    9. # 数据源 clickhouse
    10. clickhouse:
    11. driverClassName: com.clickhouse.jdbc.ClickHouseDriver
    12. url: jdbc:clickhouse://192.168.42.142:8123/bigdata
    13. username: default
    14. password: 123456
    15. initialSize: 10
    16. maxActive: 100
    17. minIdle: 10
    18. maxWait: 6000

    5、application-mybatis.yml配置

    1. #mybatis的相关配置
    2. mybatis:
    3. #mapper配置文件
    4. type-aliases-package: org.demoflowable.pojo
    5. mapper-locations: classpath:META-INF/mapper/clickhouse/*.xml
    6. config-location: classpath:META-INF/spring/mybatis-config.xml

    6、ClickHouseJdbcParamConfig

    1. import org.springframework.boot.context.properties.ConfigurationProperties;
    2. import org.springframework.stereotype.Component;
    3. @Component
    4. @ConfigurationProperties(prefix = "spring.datasource.druid.clickhouse")
    5. public class ClickHouseJdbcParamConfig {
    6. private String driverClassName;
    7. private String url;
    8. private Integer initialSize;
    9. private Integer maxActive;
    10. private Integer minIdle;
    11. private Integer maxWait;
    12. private String username;
    13. private String password;
    14. public String getDriverClassName() {
    15. return driverClassName;
    16. }
    17. public void setDriverClassName(String driverClassName) {
    18. this.driverClassName = driverClassName;
    19. }
    20. public String getUrl() {
    21. return url;
    22. }
    23. public void setUrl(String url) {
    24. this.url = url;
    25. }
    26. public Integer getInitialSize() {
    27. return initialSize;
    28. }
    29. public void setInitialSize(Integer initialSize) {
    30. this.initialSize = initialSize;
    31. }
    32. public Integer getMaxActive() {
    33. return maxActive;
    34. }
    35. public void setMaxActive(Integer maxActive) {
    36. this.maxActive = maxActive;
    37. }
    38. public Integer getMinIdle() {
    39. return minIdle;
    40. }
    41. public void setMinIdle(Integer minIdle) {
    42. this.minIdle = minIdle;
    43. }
    44. public Integer getMaxWait() {
    45. return maxWait;
    46. }
    47. public void setMaxWait(Integer maxWait) {
    48. this.maxWait = maxWait;
    49. }
    50. public String getUsername() {
    51. return username;
    52. }
    53. public void setUsername(String username) {
    54. this.username = username;
    55. }
    56. public String getPassword() {
    57. return password;
    58. }
    59. public void setPassword(String password) {
    60. this.password = password;
    61. }
    62. }

     7、ClickHouseConfig

    1. import javax.sql.DataSource;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import com.alibaba.druid.pool.DruidDataSource;
    5. @Configuration
    6. public class ClickHouseConfig {
    7. @javax.annotation.Resource
    8. private ClickHouseJdbcParamConfig jdbcParamConfig;
    9. @Bean
    10. public DataSource dataSource() {
    11. DruidDataSource datasource = new DruidDataSource();
    12. datasource.setUrl(jdbcParamConfig.getUrl());
    13. datasource.setDriverClassName(jdbcParamConfig.getDriverClassName());
    14. datasource.setInitialSize(jdbcParamConfig.getInitialSize());
    15. datasource.setMinIdle(jdbcParamConfig.getMinIdle());
    16. datasource.setMaxActive(jdbcParamConfig.getMaxActive());
    17. datasource.setMaxWait(jdbcParamConfig.getMaxWait());
    18. datasource.setUsername(jdbcParamConfig.getUsername());
    19. datasource.setPassword(jdbcParamConfig.getPassword());
    20. return datasource;
    21. }
    22. }

    8、UserInfo 

    1. public class UserInfo {
    2. private Integer id;
    3. private String userName;
    4. private String passWord;
    5. private String phone;
    6. private String createDay;
    7. public Integer getId() {
    8. return id;
    9. }
    10. public void setId(Integer id) {
    11. this.id = id;
    12. }
    13. public String getUserName() {
    14. return userName;
    15. }
    16. public void setUserName(String userName) {
    17. this.userName = userName;
    18. }
    19. public String getPassWord() {
    20. return passWord;
    21. }
    22. public void setPassWord(String passWord) {
    23. this.passWord = passWord;
    24. }
    25. public String getPhone() {
    26. return phone;
    27. }
    28. public void setPhone(String phone) {
    29. this.phone = phone;
    30. }
    31. public String getCreateDay() {
    32. return createDay;
    33. }
    34. public void setCreateDay(String createDay) {
    35. this.createDay = createDay;
    36. }
    37. }

     

    9、UserInfoMapper

    1. import java.util.List;
    2. import org.apache.ibatis.annotations.Param;
    3. import org.demoflowable.pojo.user.po.UserInfo;
    4. public interface UserInfoMapper {
    5. void saveData(UserInfo userInfo);
    6. UserInfo selectById(@Param("id") Integer id);
    7. List selectList();
    8. }

    10、UserInfoService 

    1. import java.util.List;
    2. import javax.annotation.Resource;
    3. import org.demoflowable.clickhouse.dao.user.UserInfoMapper;
    4. import org.demoflowable.pojo.user.po.UserInfo;
    5. import org.springframework.stereotype.Service;
    6. @Service
    7. public class UserInfoService {
    8. @Resource
    9. private UserInfoMapper userInfoMapper;
    10. public void saveData(UserInfo userInfo) {
    11. userInfoMapper.saveData(userInfo);
    12. }
    13. public UserInfo selectById(Integer id) {
    14. return userInfoMapper.selectById(id);
    15. }
    16. public List<UserInfo> selectList() {
    17. return userInfoMapper.selectList();
    18. }
    19. }

    11、UserInfoController

    1. import java.util.List;
    2. import javax.annotation.Resource;
    3. import org.demoflowable.pojo.user.po.UserInfo;
    4. import org.demoflowable.service.user.UserInfoService;
    5. import org.springframework.web.bind.annotation.GetMapping;
    6. import org.springframework.web.bind.annotation.RestController;
    7. @RestController
    8. public class UserInfoController {
    9. @Resource
    10. private UserInfoService userInfoService;
    11. /**
    12. * 保存
    13. * @return
    14. */
    15. @GetMapping("/saveUser")
    16. public String saveUser() {
    17. UserInfo userInfo = new UserInfo();
    18. userInfo.setId(4);
    19. userInfo.setUserName("xiaolin");
    20. userInfo.setPassWord("54321");
    21. userInfo.setPhone("18500909876");
    22. userInfo.setCreateDay("2022-02-06");
    23. userInfoService.saveData(userInfo);
    24. return "success";
    25. }
    26. /**
    27. * 根据id查询
    28. * @param id
    29. * @return
    30. */
    31. @GetMapping("/getById")
    32. public UserInfo getById(int id) {
    33. return userInfoService.selectById(id);
    34. }
    35. /**
    36. * 查询所有
    37. * @return
    38. */
    39. @GetMapping("/getList")
    40. public List getList() {
    41. return userInfoService.selectList();
    42. }
    43. }

    12、Application

    1. import org.mybatis.spring.annotation.MapperScan;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.context.annotation.ComponentScan;
    5. import org.springframework.web.bind.annotation.GetMapping;
    6. import org.springframework.web.bind.annotation.RequestParam;
    7. import org.springframework.web.bind.annotation.RestController;
    8. /**
    9. * @Description: 启动类
    10. */
    11. @SpringBootApplication
    12. @ComponentScan({ "org.demoflowable" })
    13. @MapperScan(basePackages = "org.demoflowable.*.dao")
    14. public class Application {
    15. /**
    16. * @Title: main
    17. * @Description: 启动类
    18. * @param args
    19. * @date 2023-11-08 23:49:23
    20. */
    21. public static void main(String[] args) {
    22. SpringApplication.run(Application.class, args);
    23. }
    24. }

    启动代码成功

    12、查看接口数据

    数据对应上了,springboot与clickhouse集成成功

  • 相关阅读:
    淘宝官方开放平台API接口获得店铺的所有商品、商品id、商品标题、销量参数调用示例
    python基本知识-第一周
    #安装es 常见报错#
    20220809NOI模拟赛
    用 50 张游戏显卡检测癌症,这是“业余”程序员?
    动态内存申请
    OSPF高级特性1(重发布,虚链路)
    rabbitmq 消费者报错 ListenerExecutionFailedException NullPointerException
    粉末冶金5G智能工厂工业物联数字孪生平台,推进制造业数字化转型
    春秋云镜 CVE-2017-5638
  • 原文地址:https://blog.csdn.net/itorac/article/details/134298680