最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何將Mybatis連接到ClickHouse

 更新時(shí)間:2021年03月13日 09:13:20   作者:空夜  
這篇文章主要介紹了如何將Mybatis連接到ClickHouse,幫助大家更好得理解和學(xué)習(xí)使用Mybatis,感興趣的朋友可以了解下

場(chǎng)景

最近在做數(shù)據(jù)分析項(xiàng)目,里面有這樣一個(gè)業(yè)務(wù):把匹配的數(shù)據(jù)打上標(biāo)簽,放到新的索引中。

數(shù)據(jù)量:累計(jì)億級(jí)的數(shù)據(jù)

使用場(chǎng)景:可能會(huì)單次查詢大量的數(shù)據(jù),但不會(huì)設(shè)置復(fù)雜的條件,且這些數(shù)據(jù)不會(huì)被再次修改

原來(lái)使用的數(shù)據(jù)庫(kù):ElasticSearch

問題:上面也說(shuō)了我這里打上標(biāo)記后,這些數(shù)據(jù)幾乎不會(huì)再修改了。ES 是一個(gè)全文檢索引擎,更適用于進(jìn)行大量文本檢索的情況。這里與我上面的使用場(chǎng)景就不太匹配了。

技術(shù)選型的考慮:改用戰(zhàn)斗民族開發(fā)的 ClickHouse,它適用于 OLAP 也就是數(shù)據(jù)分析的場(chǎng)景,當(dāng)數(shù)據(jù)寫入后,通過不同維度不斷挖掘、分析,發(fā)現(xiàn)其中的商業(yè)價(jià)值。ClickHouse 適用于讀遠(yuǎn)大于寫的情況。

此外,相比ES,ClickHouse 占用的硬盤空間更小,也有利于降低運(yùn)維成本。

下面是我在嘗試接入 ClickHouse 時(shí)的一些實(shí)踐,以及關(guān)于 ClickHouse數(shù)組類型轉(zhuǎn)換問題的解決方案。

關(guān)于 ClickHouse 更詳細(xì)的知識(shí)參考:https://zhuanlan.zhihu.com/p/98135840

示例代碼已經(jīng)上傳到了 Git,目前更新第 28 節(jié):https://github.com/laolunsi/spring-boot-examples/

Mybatis + ClickHouse

以前一直用 Mybatis 去操作 MySQL,其實(shí) Mybatis 還可以操作 ClickHouse,這里用 Druid 進(jìn)行連接管理。

maven 配置

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.2.5</version>
    </dependency>

    <dependency>
      <groupId>ru.yandex.clickhouse</groupId>
      <artifactId>clickhouse-jdbc</artifactId>
      <version>0.2.6</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.3</version>
    </dependency>

配置文件:

spring:
 datasource:
  type: com.alibaba.druid.pool.DruidDataSource

  # 注意這里是自定義的配置,通過 JdbcParamConfig 來(lái)加載配置到 Spring 中
  # 然后由 DruidConfig 來(lái)配置數(shù)據(jù)源
  click:
   driverClassName: ru.yandex.clickhouse.ClickHouseDriver
   url: jdbc:clickhouse://127.0.0.1:8123/test # ip:port/database
   userName: default
   password: default # 按照自己連接的 clickhouse 數(shù)據(jù)庫(kù)來(lái)
   initialSize: 10
   maxActive: 100
   minIdle: 10
   maxWait: 6000
   validationQuery: SELECT 1

加載配置項(xiàng)的類:

@Component
@ConfigurationProperties(prefix = "spring.datasource.click")
public class JdbcParamConfig {
  private String userName;
  private String password;
  private String driverClassName ;
  private String url ;
  private Integer initialSize ;
  private Integer maxActive ;
  private Integer minIdle ;
  private Integer maxWait ;
  private String validationQuery;

  // ignore getters and setters
}

配置 Druid:

@Configuration
@MapperScan(basePackages = {
    "com.aegis.analysis.clickhousestorage.dao"
})
public class DruidConfig {
  @Resource
  private JdbcParamConfig jdbcParamConfig ;

  @Bean(name = "clickDataSource")
  public DataSource dataSource() throws ClassNotFoundException {
    Class classes = Class.forName("com.alibaba.druid.pool.DruidDataSource");
    DruidDataSource dataSource = (DruidDataSource) DataSourceBuilder
        .create()
        .driverClassName(jdbcParamConfig.getDriverClassName())
        .type(classes)
        .url(jdbcParamConfig.getUrl())
        .username(jdbcParamConfig.getUserName())
        .password(jdbcParamConfig.getPassword())
        .build();
    dataSource.setMaxWait(jdbcParamConfig.getMaxWait());
    dataSource.setValidationQuery(jdbcParamConfig.getValidationQuery());
    return dataSource;
  }

  @Bean
  public SqlSessionFactory clickHouseSqlSessionFactoryBean() throws Exception {
    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
    factory.setDataSource(dataSource());
    // 實(shí)體 model的 路徑 比如 com.order.model
    factory.setTypeAliasesPackage("com.example.clickhousedemo.model");
    //添加XML目錄
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
    //開啟駝峰命名轉(zhuǎn)換
    factory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return factory.getObject();
  }
}

定義一個(gè) UserInfo 類,建表語(yǔ)句如下:

CREATE TABLE test.user (
   `id` Int16,
   `name` String,
   `score` Float32,
   `score2` Float64,
   `state` Int8,
   `createTime` DateTime,
   `ranks` Array(UInt8)
   ) ENGINE = MergeTree() ORDER BY id;

實(shí)體類:

public class UserInfo {

  private Integer id; // int16
  private String name; // String
  private Float score; // float16
  private Double score2; // float32
  private Boolean state; // int8
  private Date createTime; // datetime
  private Integer[] ranks; // Array - Array 類型需要進(jìn)行類型轉(zhuǎn)換
  // 具體轉(zhuǎn)換方法與配置參考 ClickArrayToIntHandler 類與 UserMapper.xml 中關(guān)于查詢和插入時(shí) ranks 字段的配置

  // ignore getters and setters
}

DAO 和 Mapper 文件就按照連接 MYSQL 時(shí)的寫法一樣。

這里有個(gè)需要注意的點(diǎn),ClickHouse 有個(gè) Array 類型,可以用來(lái)存數(shù)組,就像 ES 一樣。問題是類型轉(zhuǎn)換需要自己定義。網(wǎng)上一些資料僅列出了基本類型的場(chǎng)景,我自己實(shí)現(xiàn)了一個(gè)轉(zhuǎn)換器,可以參考一下:

/**
 * Java Int 數(shù)組與 ClockHouse Array Int 轉(zhuǎn)換器
 * @version 1.0
 * @since 2019/11/14 9:59
 */
public class ClickArrayToIntHandler extends BaseTypeHandler<Integer[]> {

  @Override
  public void setNonNullParameter(PreparedStatement preparedStatement, int i, Integer[] integers, JdbcType jdbcType) throws SQLException {
    preparedStatement.setObject(i, integers);
  }

  @Override
  public Integer[] getNullableResult(ResultSet resultSet, String s) throws SQLException {
    Object obj = resultSet.getObject(s);
    return parseClickHouseArrayToInt(obj);
  }

  @Override
  public Integer[] getNullableResult(ResultSet resultSet, int i) throws SQLException {
    Object obj = resultSet.getObject(i);
    return parseClickHouseArrayToInt(obj);
  }

  @Override
  public Integer[] getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
    Object obj = callableStatement.getObject(i);
    return parseClickHouseArrayToInt(obj);
  }

  private Integer[] parseClickHouseArrayToInt(Object obj) {
    if (obj instanceof ClickHouseArray) {
      int[] res = new int[0];
      try {
        res = (int[]) ((ClickHouseArray) obj).getArray();
      } catch (SQLException ex) {
        ex.printStackTrace();
      }

      if (res != null && res.length > 0) {
        Integer[] resI = new Integer[res.length];
        for (int i = 0; i < res.length; i++) {
          resI[i] = res[i];
        }

        return resI;
      }
    }
    return new Integer[0];
  }
}

DAO.xml 也給一個(gè)示例:

<?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.aegis.analysis.clickhousestorage.dao.UserInfoMapper">
  <resultMap id="BaseResultMap" type="com.example.clickhousedemo.model.UserInfo">
    <id column="id" property="id" />
    <result column="name" property="name" />
    <result column="name" property="name" />
    <result column="score" property="score" />
    <result column="score2" property="score2" />
    <result column="state" property="state" />
    <result column="createTime" property="createTime" />
    <!-- <result column="ranks" property="ranks" jdbcType="JAVA_OBJECT" javaType="java.lang.Object" />-->
    <result column="ranks" property="ranks" typeHandler="com.example.clickhousedemo.dao.ClickArrayToIntHandler" />
  </resultMap>

  <sql id="Base_Column_List">
    *
  </sql>

  <insert id="saveData" parameterType="com.aegis.analysis.clickhousestorage.model.UserInfo" >
    INSERT INTO user
      (id,name, score, score2, state, createTime, ranks)
    VALUES
    (#{id},#{name}, #{score}, #{score2}, #{state}, #{createTime}, #{ranks, jdbcType=ARRAY,
    typeHandler=com.example.clickhousedemo.dao.ClickArrayToIntHandler})
  </insert>

  <select id="selectById" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from user
    where id = #{id}
    limit 1
  </select>

  <select id="selectList" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List" />
    from user
  </select>
</mapper>

具體代碼可以去我的 Git 倉(cāng)庫(kù)里查看,還有 SpringBoot 整合其他中間件技術(shù)的示例,歡迎 Star!

https://github.com/laolunsi/spring-boot-examples

以上就是如何將Mybatis連接到ClickHouse的詳細(xì)內(nèi)容,更多關(guān)于Mybatis連接到ClickHouse的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringCloud hystrix服務(wù)降級(jí)學(xué)習(xí)筆記

    SpringCloud hystrix服務(wù)降級(jí)學(xué)習(xí)筆記

    什么是服務(wù)降級(jí)?當(dāng)服務(wù)器壓力劇增的情況下,根據(jù)實(shí)際業(yè)務(wù)情況及流量,對(duì)一些服務(wù)和頁(yè)面有策略的不處理或換種簡(jiǎn)單的方式處理,從而釋放服務(wù)器資源以保證核心交易正常運(yùn)作或高效運(yùn)作
    2022-10-10
  • Kryo序列化及反序列化用法示例

    Kryo序列化及反序列化用法示例

    這篇文章主要介紹了Kryo序列化及反序列化用法示例,小編覺得挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下。
    2017-10-10
  • java多線程三種上鎖方式小結(jié)

    java多線程三種上鎖方式小結(jié)

    本文主要介紹了java多線程三種上鎖方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-12-12
  • SpringBoot程序打包失敗(.jar中沒有主清單屬性)

    SpringBoot程序打包失敗(.jar中沒有主清單屬性)

    在學(xué)習(xí)SpringBoot,打包SpringBoot程序后,在cmd運(yùn)行出現(xiàn)了 某某某.jar中沒有注清單屬性,本文就來(lái)介紹一下原因以及解決方法,感興趣的可以了解一下
    2023-06-06
  • SpringBoot整合Web開發(fā)之文件上傳與@ControllerAdvice

    SpringBoot整合Web開發(fā)之文件上傳與@ControllerAdvice

    @ControllerAdvice注解是Spring3.2中新增的注解,學(xué)名是Controller增強(qiáng)器,作用是給Controller控制器添加統(tǒng)一的操作或處理。對(duì)于@ControllerAdvice,我們比較熟知的用法是結(jié)合@ExceptionHandler用于全局異常的處理,但其作用不止于此
    2022-08-08
  • Spring Boot框架中的@Conditional注解示例詳解

    Spring Boot框架中的@Conditional注解示例詳解

    這篇文章主要介紹了Spring Boot框架中的@Conditional系列注解,@ConditionalOnProperty注解的作用是解析application.yml/application.properties 里的配置生成條件來(lái)生效,也是與@Configuration注解一起使用,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友一起看看吧
    2022-09-09
  • Java中CompletableFuture?的詳細(xì)介紹

    Java中CompletableFuture?的詳細(xì)介紹

    這篇文章主要介紹了Java中的CompletableFuture,通過創(chuàng)建?CompletableFuture?的對(duì)象的工廠方法展開詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-05-05
  • mybatis-plus主鍵生成策略

    mybatis-plus主鍵生成策略

    這篇文章主要介紹了mybatis-plus主鍵生成策略,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • java8 stream sort自定義復(fù)雜排序案例

    java8 stream sort自定義復(fù)雜排序案例

    這篇文章主要介紹了java8 stream sort自定義復(fù)雜排序案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-10-10
  • Spring常用配置及解析類說(shuō)明

    Spring常用配置及解析類說(shuō)明

    這篇文章主要介紹了Spring常用配置及解析類說(shuō)明,涉及Spring常用配置項(xiàng)的方法以及它的解析類等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11

最新評(píng)論

齐河县| 仁寿县| 潼关县| 临潭县| 柞水县| 嵊泗县| 浏阳市| 卢龙县| 蕉岭县| 神农架林区| 肇源县| 宜宾市| 九龙县| 夏河县| 临夏市| 辛集市| 肃南| 庆元县| 大英县| 遵义市| 湘阴县| 乡城县| 浦北县| 肃南| 信宜市| 麟游县| 华宁县| 峨眉山市| 芷江| 绍兴县| 保山市| 武宣县| 虞城县| 乐平市| 曲麻莱县| 介休市| 临湘市| 唐海县| 灌阳县| 舞钢市| 遵化市|