使用jdbc方式操作ClickHouse

server/2024/9/23 4:14:44/

1、创建测试表,和插入测试数据

create table t_order01(id UInt32,sku_id String,total_amount Decimal(16,2),create_time Datetime
) engine =MergeTreepartition by toYYYYMMDD(create_time)primary key (id)order by (id,sku_id);insert into t_order01 values
(101,'sku_001',1000.00,'2021-12-01 12:00:00'),
(102,'sku_002',2000.00,'2021-12-01 11:00:00'),
(102,'sku_004',2500.00,'2021-12-01 12:00:00'),
(102,'sku_002',2000.00,'2021-12-01 13:00:00'),
(102,'sku_002',12000.00,'2021-12-01 13:00:00'),
(102,'sku_002',600.00,'2020-06-12 12:00:00');

2、使用jdbc方式操作ClickHouse

2.1、引入clickhouse的jdbc依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>clickhouse-test</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>ru.yandex.clickhouse</groupId><artifactId>clickhouse-jdbc</artifactId><version>0.1.52</version></dependency></dependencies></project>

2.2、大部分的操作和我们使用jdbc操作mysql的步骤类似,下面直接贴出代码,可以结合注释进行参考使用

package org.example;import ru.yandex.clickhouse.ClickHouseConnection;
import ru.yandex.clickhouse.ClickHouseDataSource;
import ru.yandex.clickhouse.settings.ClickHouseProperties;import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class CreateTableTest {private static String username = "default";private static String password = "123456";private static String address = "jdbc:clickhouse://43.138.0.199:8123";private static String db = "default";private static int socketTimeout = 600000;public static void main(String[] args) throws Exception {//getConn();queryTable();//createTable("");//insertOne();//dropTable();//deleteById();//updateById();}/*** 查询数据*/public static void queryTable(){List<Map<String, Object>> list = new ArrayList<>();String sql = "select * from t_order01";Connection connection = getConn();try {Statement statement = connection.createStatement();ResultSet rs  = statement.executeQuery(sql);ResultSetMetaData rsmd = rs.getMetaData();while(rs.next()){Map<String, Object> row = new HashMap<>();for(int i = 1; i <= rsmd.getColumnCount(); i++){row.put(rsmd.getColumnName(i), rs.getObject(rsmd.getColumnName(i)));}list.add(row);}} catch (SQLException e) {e.printStackTrace();}//在此可以根据实际需求将解析的数据封装到对象中list.stream().forEach(item ->{Map<String, Object> rowData = item;System.out.println(rowData);});//System.out.println(list);}/*** 创建表* @throws Exception*/public static void createTable(String tableSql) throws Exception{/*tableSql = "create table t_order02(\n" +" id UInt32,\n" +" sku_id String,\n" +" total_amount Decimal(16,2),\n" +" create_time Datetime\n" +") engine =MergeTree\n" +" partition by toYYYYMMDD(create_time)\n" +" primary key (id)\n" +" order by (id,sku_id);";*/Connection connection = getConn();Statement statement = connection.createStatement();boolean execute = statement.execute(tableSql);if(execute){System.out.println(execute);System.out.println("创建表成功");}}/*** 删除表* @throws Exception*/public static void dropTable() throws Exception{Connection connection = getConn();Statement statement = connection.createStatement();statement.execute("drop table t_order01;");System.out.println("删除表成功");}/*** 插入数据* 实际使用时候,插入的语句里面的参数从外部传入进去* @throws Exception*/public static void insertOne() throws Exception{Connection connection = getConn();PreparedStatement pstmt = connection.prepareStatement("insert into t_order01 values('103', 'sku_004', '2500.00','2021-06-01 12:00:00')");pstmt.execute();System.out.println("insert success");}/*** 删除数据* 实际使用时候,删除的语句里面的参数从外部传入进去*/public static void deleteById() throws Exception{Connection connection = getConn();//sku_id ='sku_001'PreparedStatement pstmt = connection.prepareStatement("alter table t_order01 delete where sku_id = 'sku_002';");pstmt.execute();System.out.println("delete success");}/*** 修改数据* 实际使用时候,修改的语句里面的参数从外部传入进去*/public static void updateById() throws Exception{Connection connection = getConn();PreparedStatement pstmt = connection.prepareStatement("alter table t_order01 update total_amount=toDecimal32(3000.00,2) where id = '102'");pstmt.execute();System.out.println("update success");}public static Connection getConn() {ClickHouseProperties properties = new ClickHouseProperties();properties.setUser(username);properties.setPassword(password);properties.setDatabase(db);properties.setSocketTimeout(socketTimeout);ClickHouseDataSource clickHouseDataSource = new ClickHouseDataSource(address, properties);ClickHouseConnection conn = null;try {conn = clickHouseDataSource.getConnection();System.out.println(conn);System.out.println("连接成功");return conn;} catch (SQLException e) {e.printStackTrace();}return null;}
}

2.3测试


http://www.ppmy.cn/server/28985.html

相关文章

【webrtc】MessageHandler 9: 基于线程的消息处理:执行Port销毁自己

Port::Port 构造的时候,就触发了一个异步操作,但是这个操作是要在 thread 里执行的,因此要通过post 消息 MSG_DESTROY_IF_DEAD 到thread跑:port的创建并米有要求在thread中 但是port的析构却在thread里 这是为啥呢?

【右一的实操记录】全导航,持续更新...

文章目录 &#x1f4da;大数据管理与分析【实验】&#x1f4da;数据结构【实验】&#x1f4da;机器学习【实验】&#x1f4da;大数据安全【实验】&#x1f4da;信息检索【实验】&#x1f4da;爬虫【小实践】&#x1f4da;AIGC&#x1f4da;杂货铺 大部分是和电子笔记对应的实验…

【研发管理】产品经理知识体系-产品设计与开发工具

导读&#xff1a;产品设计与开发工具的重要性体现在多个方面&#xff0c;它们对于产品的成功开发、质量提升以及市场竞争力都具有至关重要的影响。产品设计工具可以帮助设计师更高效地创建和优化产品原型。开发工具在产品开发过程中发挥着至关重要的作用。产品设计与开发工具还…

2023 广东省大学生程序设计竞赛(部分题解)

目录 A - Programming Contest B - Base Station Construction C - Trading D - New Houses E - New but Nostalgic Problem I - Path Planning K - Peg Solitaire A - Programming Contest 签到题&#xff1a;直接模拟 直接按照题目意思模拟即可&#xff0c;为了好去…

多网卡按进程或目标IP指定使用特定网卡

某些场景下&#xff0c;电脑上有多张网卡&#xff0c;且不同网卡用途不一&#xff0c;默认情况下电脑只会优先使用其中一张。比如一张内网网卡&#xff0c;一张外网网卡&#xff0c;无法同时能上内网和外网。 之前尝试了很多方法都不可行&#xff1a; 1、手动指定路由&#x…

机器学习——过拟合

优质博文&#xff1a;IT-BLOG-CN 一、过拟合得表现 模型在训练过程中&#xff0c;除了会出现过拟合现象&#xff0c;还有可能出现欠拟合的情况。相比而言&#xff0c;后者通常发生在建模前期&#xff0c;只要做好特征工程一般可以解决模型欠拟合问题。下图描述了模型在训练数据…

UIButton中addTarget和addAction有什么区别

addTarget(_:action:for:) 和 addAction(_:for:) 都是用来给 UIButton 添加事件监听的方法&#xff0c;但是它们的用法略有不同。 addTarget(_:action:for:)&#xff1a;这是 UIKit 中的方法&#xff0c;通过调用 addTarget 方法可以将一个目标对象&#xff08;通常是按钮的拥有…

centos 杀死一个进程又启动了

在CentOS中&#xff0c;如果一个进程被杀死后又自动启动&#xff0c;可能是由于系统服务管理器&#xff08;如systemd或init&#xff09;配置了该进程的重启。以下是检查和处理这种情况的方法&#xff1a; 查找启动脚本&#xff1a; 使用systemctl查找服务文件&#xff1a;syst…