
# 调测ClickHouse SpringBoot样例程序
#### 在本地Windows环境中调测ClickHouse SpringBoot样例程序
**编译并运行程序**
在程序代码完成开发后，您可以在Windows环境中运行应用。本地和集群业务平面网络互通时，您可以直接在本地进行调测。在开发环境IntelliJ IDEA工程"clickhouse-rest-client-examples"中右键单击"ClickHouseApplication"，选择"Run ClickHouseApplication"运行应用程序工程。
![](https://support.huaweicloud.com/devg-lts-mrs/public_sys-resources/note_3.0-zh-cn.png)
"clickhouse-rest-client-example"样例代码，JDK版本只支持JDK 8和JDK 17。
![](https://support.huaweicloud.com/devg-lts-mrs/zh-cn_image_0000001904370906.png "点击放大")
**操作步骤**
1. 在IDEA界面复制clickhouse-example.properties路径。右键clickhouse-example.properties文件，单击"Copy Path/Reference \> Absolute Path"。 
   图1复制配置文件绝对路径   
   ![](https://support.huaweicloud.com/devg-lts-mrs/zh-cn_image_0000001904530810.png "点击放大")
   
   
2. 使用clickhouse-example.properties路径替换ClickHouseFunc.java中getProperties()方法中proPath的路径。 
   图2ClickHouseFunc.java路径替换   
   ![](https://support.huaweicloud.com/devg-lts-mrs/zh-cn_image_0000001941210213.png "点击放大")
   
   
**查看调测结果**
1. ClickHouse springboot服务启动后，通过ClickHouse样例接口触发样例代码运行，在浏览器中输入需要执行具体操作的链接，如http://localhost:8080/clickhouse/executeQuery，返回结果：
   ```
   ClickHouse springboot client runs normally.
   ```
   
2. 通过ClickHouse日志获取应用运行情况，即日志文件：clickhouse-springboot-example.log**。**
   运行clickhouse-springboot样例后，控制台显示部分运行结果如下：
   ```
   .   ____          _            __ _ _
   /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
   ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
   \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
   '  |____| .__|_| |_|_| |_\__, | / / / /
   =========|_|==============|___/=/_/_/_/
   :: Spring Boot ::                (v2.7.0)
   2023-02-06 17:58:22.144  INFO 20556 --- [           main] c.h.f.c.e.s.r.ClickHouseApplication      : Starting ClickHouseApplication using Java 1.8.0_181 on DESKTOP-64PQBSD with PID 20556 (D:\Code\cloudBU\sample_project\src\springboot\clickhouse-examples\clickhouse-rest-client-example\target\classes started by l00467039 in D:\Code\cloudBU\sample_project\src\springboot\clickhouse-examples\clickhouse-rest-client-example)
   2023-02-06 17:58:22.146  INFO 20556 --- [           main] c.h.f.c.e.s.r.ClickHouseApplication      : No active profile set, falling back to 1 default profile: "default"
   2023-02-06 17:58:22.815  INFO 20556 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
   2023-02-06 17:58:22.820  INFO 20556 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
   2023-02-06 17:58:22.821  INFO 20556 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.63]
   2023-02-06 17:58:22.928  INFO 20556 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
   2023-02-06 17:58:22.928  INFO 20556 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 750 ms
   2023-02-06 17:58:23.175  INFO 20556 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
   2023-02-06 17:58:23.181  INFO 20556 --- [           main] c.h.f.c.e.s.r.ClickHouseApplication      : Started ClickHouseApplication in 1.388 seconds (JVM running for 3.989)
   2023-02-06 17:58:32.640  INFO 20556 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
   2023-02-06 17:58:32.640  INFO 20556 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
   2023-02-06 17:58:32.641  INFO 20556 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
   2023-02-06 17:58:32.656  INFO 20556 --- [nio-8080-exec-1] .f.c.e.s.r.c.ClickHouseExampleController : Begin to execute query in clickhouse...
   2023-02-06 17:58:32.658  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : loadBalancerIPList is 192.168.6.24, loadBalancerHttpPort is 21422, user is ckuser, clusterName is default_cluster, isSec is true.
   2023-02-06 17:58:32.659  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : ckLbServerList current member is 0, ClickhouseBalancer is 192.168.6.24:21422
   2023-02-06 17:58:32.664  INFO 20556 --- [nio-8080-exec-1] ru.yandex.clickhouse.ClickHouseDriver    : Driver registered
   2023-02-06 17:58:32.665  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Try times is 0, current load balancer is 192.168.6.24:21422.
   2023-02-06 17:58:35.494  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Execute sql drop table if exists testdb.testtb on cluster default_cluster no delay, time is 216 ms
   2023-02-06 17:58:35.495  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Try times is 0, current load balancer is 192.168.6.24:21422.
   2023-02-06 17:58:36.074  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Execute sql drop table if exists testdb.testtb_all on cluster default_cluster no delay, time is 196 ms
   2023-02-06 17:58:36.074  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Try times is 0, current load balancer is 192.168.6.24:21422.
   2023-02-06 17:58:36.765  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Execute sql create database if not exists testdb on cluster default_cluster, time is 218 ms
   2023-02-06 17:58:36.765  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Try times is 0, current load balancer is 192.168.6.24:21422.
   2023-02-06 17:58:37.364  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Execute sql create table testdb.testtb on cluster default_cluster (name String, age UInt8, date Date)engine=ReplicatedMergeTree('/clickhouse/tables/{shard}/testdb.testtb','{replica}') partition by toYYYYMM(date) order by age, time is 198 ms
   2023-02-06 17:58:37.366  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Try times is 0, current load balancer is 192.168.6.24:21422.
   2023-02-06 17:58:37.872  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Execute sql create table testdb.testtb_all on cluster default_cluster as testdb.testtb ENGINE = Distributed(default_cluster,testdb,testtb, rand());, time is 160 ms
   2023-02-06 17:58:38.959  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Insert batch time is 591 ms
   2023-02-06 17:58:41.114  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Insert batch time is 572 ms
   2023-02-06 17:58:43.095  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Insert batch time is 413 ms
   2023-02-06 17:58:45.220  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Insert batch time is 543 ms
   2023-02-06 17:58:47.207  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Insert batch time is 406 ms
   2023-02-06 17:58:49.236  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Insert batch time is 460 ms
   2023-02-06 17:58:51.197  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Insert batch time is 390 ms
   2023-02-06 17:58:53.233  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Insert batch time is 466 ms
   2023-02-06 17:58:55.355  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Insert batch time is 555 ms
   2023-02-06 17:58:57.321  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Insert batch time is 386 ms
   2023-02-06 17:58:58.832  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Inert all batch time is 20564 ms
   2023-02-06 17:58:58.832  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Try times is 0, current load balancer is 192.168.6.24:21422.
   2023-02-06 17:58:59.256  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Execute sql select * from testdb.testtb_all order by age limit 10, time is 119 ms
   2023-02-06 17:58:59.257  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Try times is 0, current load balancer is 192.168.6.24:21422.
   2023-02-06 17:58:59.972  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.restclient.impl.Util         : Execute sql select toYYYYMM(date),count(1) from testdb.testtb_all group by toYYYYMM(date) order by count(1) DESC limit 10, time is 289 ms
   2023-02-06 17:58:59.973  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : huawei_23402022-12-16
   2023-02-06 17:58:59.973  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : huawei_180502022-12-21
   2023-02-06 17:58:59.973  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : huawei_335902022-12-13
   2023-02-06 17:58:59.973  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : huawei_427502022-12-09
   2023-02-06 17:58:59.973  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : huawei_430702022-12-20
   2023-02-06 17:58:59.973  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : huawei_458602022-12-02
   2023-02-06 17:58:59.973  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : huawei_632602022-12-18
   2023-02-06 17:58:59.973  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : huawei_987802022-12-06
   2023-02-06 17:58:59.974  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : huawei_248202022-12-18
   2023-02-06 17:58:59.974  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : huawei_290402022-12-25
   2023-02-06 17:58:59.974  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : 2022018628
   2023-02-06 17:58:59.974  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : 2022088587
   2023-02-06 17:58:59.974  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : 2022058558
   2023-02-06 17:58:59.974  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : 2022108547
   2023-02-06 17:58:59.974  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : 2022078463
   2023-02-06 17:58:59.974  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : 2022038379
   2023-02-06 17:58:59.974  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : 2022048351
   2023-02-06 17:58:59.974  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : 2022068300
   2023-02-06 17:58:59.974  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : 2022098297
   2023-02-06 17:58:59.974  INFO 20556 --- [nio-8080-exec-1] c.h.f.c.e.s.r.impl.ClickHouseFunc        : 2022128228
   ```
   
#### 在Linux环境中调测ClickHouse SpringBoot样例程序
ClickHouse springboot应用程序也支持在Linux环境中运行。在程序代码完成开发后，您可以上传Jar包至准备好的Linux运行环境中运行。
**前提条件**
Linux环境已安装JDK，版本号需要和IntelliJ IDEA导出Jar包使用的JDK版本一致，并设置好Java环境变量。
**编译并运行程序**
1. 在IDEA中右侧单击"Maven"，展开"Lifecycle"，双击"package"，对当前工程进行打包。 
   ![](https://support.huaweicloud.com/devg-lts-mrs/zh-cn_image_0000001904370910.png "点击放大")
   
   
2. 使用root用户登录ClickHouse客户端节点，创建运行目录，例如"/opt/test"，在IDEA的"target"目录下获取带有"-with-dependencies"的jar包，并将jar包和idea中conf文件夹一同上传到"/opt/test"目录，如： 
   ![](https://support.huaweicloud.com/devg-lts-mrs/zh-cn_image_0000001904530818.png "点击放大")
   
   
3. 执行如下命令，配置环境变量并运行jar包： 
   **cd** *客户端安装路径*
   **source bigdata_env**
   **cd /opt/test**
   **java -jar clickhouse-examples-1.0-SNAPSHOT-jar-with-dependencies.jar**
   显示结果如下：
   ![](https://support.huaweicloud.com/devg-lts-mrs/zh-cn_image_0000001941210217.png "点击放大")
   
   
4. 调用ClickHouse的SpringBoot样例接口触发样例代码运行： 
   - Windows环境运行方式： 打开浏览器，输入：http://*ClickHouse客户端节点IP*:8080/clickhouse/executeQuery，查看浏览器返回信息：
     ```
     ClickHouse springboot client runs normally.
     ```
     
   
   - Linux环境下执行运行方式： 登录ClickHouse客户端节点，执行如下命令，查看linux下shell日志打印和日志文件打印：
     **curl http://** **localhost:8080** **/clickhouse/executeQuery**
     **vi clickhouse-springboot-example.log**
     ![](https://support.huaweicloud.com/devg-lts-mrs/zh-cn_image_0000001904370922.png "点击放大")
     
   
   
   
   
