搜索
简帛阁>技术文章>Springboot集成Clickhouse附完整代码和测试数据。

Springboot集成Clickhouse附完整代码和测试数据。

ClickHouse的安装教程查看前面的文章。

工程结构:

maven依赖

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

配属数据源

spring:
  datasource:
    driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
    url: jdbc:clickhouse://192.168.8.145:8123/test
  application:
    name: clickouse-application
mybatis:
  mapper-locations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
  port: 9000

Controller

package com.geewise.bigdata.controller;

import com.geewise.bigdata.entity.Order;
import com.geewise.bigdata.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author lgn
 * @created 2021-01-13 9:41
 */
@RestController
@RequestMapping("/order")
public class OrderController {<!-- -->

    @Autowired
    private OrderService orderService;

    @GetMapping("/{skuId}")
    public BigDecimal getAmountBySkuId(
            @PathVariable(name = "skuId", required = true) String skuId
    ){<!-- -->
        return orderService.getTotalAmountBySkuId(skuId);
    }

    @GetMapping("/detail/{id}")
    public List<Order> getOderBySkuId(
            @PathVariable(name = "id", required = true) Integer id
    ){<!-- -->
        return orderService.getOrderById(id);
    }
}

entity

import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author lgn
 * @created 2021-01-13 15:24
 */
@Data
public class Order {<!-- -->
    private Integer id;
    private String skuId;
    private BigDecimal totalAmount;
    private Date createTime;
}

service

import com.geewise.bigdata.entity.Order;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author lgn
 * @created 2021-01-13 9:39
 */
public interface OrderService {<!-- -->
    BigDecimal getTotalAmountBySkuId(String skuId);
    List<Order> getOrderById(Integer id);
}

ServiceImpl

import com.geewise.bigdata.entity.Order;
import com.geewise.bigdata.mapper.OrderMapper;
import com.geewise.bigdata.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author lgn
 * @created 2021-01-13 9:40
 */
@Service
public class OrderServiceImpl implements OrderService {<!-- -->

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public BigDecimal getTotalAmountBySkuId(String skuId) {<!-- -->
        return orderMapper.selectTotalAmountSkuId(skuId);
    }

    @Override
    public List<Order> getOrderById(Integer id) {<!-- -->
        return orderMapper.selectOrder(id);
    }
}

mapper

import com.geewise.bigdata.entity.Order;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author lgn
 * @created 2021-01-13 9:28
 */
public interface OrderMapper {<!-- -->
    BigDecimal selectTotalAmountSkuId(String skuId);
    List<Order> selectOrder(Integer Id);
}

Mapper.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.geewise.bigdata.mapper.OrderMapper">
    <sql id="baseSql">
        SELECT id as id,sku_id as skuId, total_amount as totalAmount,create_time as createTime
    </sql>

    <!--map-underscore-to-camel-case 大写转下划线, 所以column的配置需要写成驼峰 result property="totalAmount" column="totalAmount"-->
    <resultMap id="baseResult" type="com.geewise.bigdata.entity.Order">
        <id property="id" column="id" />
        <result property="skuId" column="skuId" />
        <result property="totalAmount" column="totalAmount" />
        <result property="createTime" column="createTime" />
    </resultMap>
    <select id="selectTotalAmountSkuId" parameterType="String" resultType="java.math.BigDecimal">
      SELECT SUM(total_amount) as sum_amount FROM `order` WHERE sku_id=#{<!-- -->skuId}
    </select>

    <select id="selectOrder" parameterType="Integer" resultMap="baseResult">
      <include refid="baseSql"></include>
        FROM `order` WHERE id = #{<!-- -->Id}
    </select>

</mapper>

测试数据:

create table order(id UInt32,sku_id String,total_amount Decimal(16,2),create_time Datetime) engine =MergeTree partition by toYYYYMMDD(create_time) primary key (id) order by (id,sku_id);

查看一下数据库(可视化数据库安装翻看我以前的文章)

启动项目

访问一下接口


访问成功。

准备:请按照下面的连接安装ck的集群https://wwwcnblogscom/ywjfx/p/14329605html1、pomxml配置文件添加依赖<dependency><gr
ClickHouse应用场景:1绝大多数请求都是用于读访问的2数据需要以大批次(大于1000行)进行更新,而不是单行更新;或者根本没有更新操作3数据只是添加到数据库,没有必要修改4读取数据时,会从数据
一、加入clickhousejar包依赖<dependency><groupId>ruyandexclickhouse</groupId><artifactI
目录1pomxml2applicationyml3MyJPushClient4test工作中经常会遇到服务器向App推送消息的需求,一般企业中选择用极光推送的比较多,在集成极光时发现极光的文档并不完整
之前做过公司的SSO接入,也网上找了很多博客,结合自己的理解梳理了一些,文章有点长,希望可以有耐心的看下去。一、Springsecurity是什么?SpringSecurity是一个能够为基于Spri
一、ClickHouse简介1、基础简介Yandex开源的数据分析的数据库,名字叫做ClickHouse,适合流式或批次入库的时序数据。ClickHouse不应该被用作通用数据库,而是作为超高性能的海
1、ClickHouse介绍ck不使用HDFS存储数据可以继承HDFSmpp架构是一个DBMS数据库列式存储实现语言C++处理数据速度快横向扩展,高可靠。CK引擎介绍分为数据库引擎表引擎,不同的引
一、简介ClickHouse是“战斗民族”俄罗斯搜索巨头Yandex公司开源的一个极具战斗力的实时数据分析数据库,是面向OLAP的分布式列式DBMS。ClickHouse有一个简称CK,与Hadoo
展示图:对接的完整流程如下首先是配置gzhappid公众号appidwxPaymchId商户号wxPaykey支付密钥wxPaynotifyUrl域名回调地址常量:/**微信支付统一下单接口*/pub
Application:filesystemerror:inrename:Invalidcrossdevicelink[/var/lib/clickhouse/store/项目场景:用docker镜像