• 基于springboot操作clickhouse进行大数据查询和批量插入


    注:此例子可直接进行现实具体业务功能处理

    1.所需依赖

    
    
        ru.yandex.clickhouse
        clickhouse-jdbc
        0.1.53
    

    sql表

    1. -- test.bigtest definition
    2. CREATE TABLE test.bigtest
    3. (
    4. `id` Int64,
    5. `name` String,
    6. `age` String,
    7. `adress` String,
    8. `city` String,
    9. `habit` String
    10. )
    11. ENGINE = MergeTree
    12. ORDER BY id
    13. SETTINGS index_granularity = 8192;

    2.clickhouse环境配置及环境上下文类

    spring.datasource.clickhouse.address=jdbc:clickhouse://127.0.0.1:8123
    spring.datasource.clickhouse.username=default
    spring.datasource.clickhouse.password=
    spring.datasource.clickhouse.db=test
    spring.datasource.clickhouse.socketTimeout=600000

    clickhouse环境类

    1. @Component
    2. public class ClickHouseConfig {
    3. private static String clickhouseAddress;
    4. private static String clickhouseUsername;
    5. private static String clickhousePassword;
    6. private static String clickhouseDB;
    7. private static Integer clickhouseSocketTimeout;
    8. @Value("${spring.datasource.clickhouse.address}")
    9. public void setClickhouseAddress(String address) {
    10. ClickHouseConfig.clickhouseAddress = address;
    11. }
    12. @Value("${spring.datasource.clickhouse.username}")
    13. public void setClickhouseUsername(String username) {
    14. ClickHouseConfig.clickhouseUsername = username;
    15. }
    16. @Value("${spring.datasource.clickhouse.password}")
    17. public void setClickhousePassword(String password) {
    18. ClickHouseConfig.clickhousePassword = password;
    19. }
    20. @Value("${spring.datasource.clickhouse.db}")
    21. public void setClickhouseDB(String db) {
    22. ClickHouseConfig.clickhouseDB = db;
    23. }
    24. @Value("${spring.datasource.clickhouse.socketTimeout}")
    25. public void setClickhouseSocketTimeout(Integer socketTimeout) {
    26. ClickHouseConfig.clickhouseSocketTimeout = socketTimeout;
    27. }
    28. public static Connection getConn() {
    29. ClickHouseConnection conn = null;
    30. ClickHouseProperties properties = new ClickHouseProperties();
    31. properties.setUser(clickhouseUsername);
    32. properties.setPassword(clickhousePassword);
    33. properties.setDatabase(clickhouseDB);
    34. properties.setSocketTimeout(clickhouseSocketTimeout);
    35. ClickHouseDataSource clickHouseDataSource = new ClickHouseDataSource(clickhouseAddress,properties);
    36. try {
    37. conn = clickHouseDataSource.getConnection();
    38. return conn;
    39. } catch (SQLException e) {
    40. e.printStackTrace();
    41. }
    42. return null;
    43. }
    44. public static Connection getConnection() throws SQLException{
    45. //从数据源中获取数据库连接
    46. return getConn();
    47. }
    48. public static void release(Connection conn, Statement st, ResultSet rs) {
    49. if (rs != null) {
    50. try {
    51. //关闭存储查询结果的ResultSet对象
    52. rs.close();
    53. } catch (Exception e) {
    54. e.printStackTrace();
    55. }
    56. rs = null;
    57. }
    58. if (st != null) {
    59. try {
    60. //关闭负责执行SQL命令的Statement对象
    61. st.close();
    62. } catch (Exception e) {
    63. e.printStackTrace();
    64. }
    65. }
    66. if (conn != null) {
    67. try {
    68. //将Connection连接对象还给数据库连接池
    69. conn.close();
    70. } catch (Exception e) {
    71. e.printStackTrace();
    72. }
    73. }
    74. }
    75. }

    3.clickhouse查询案例

    1. @Component
    2. public class Clickhouse_Select {
    3. public static List<Map<String,String>> exeSql(String sql) throws SQLException {
    4. log.info("cliockhouse 执行sql:" + sql);
    5. List<Map<String, String>> results = new ArrayList>();
    6. Connection conn = null;
    7. PreparedStatement stmt = null;
    8. ResultSet rs = null;
    9. try {
    10. conn = ClickHouseConfig.getConnection();
    11. stmt = conn.prepareStatement(sql);
    12. rs = stmt.executeQuery();// 执行查询数据库的SQL语句 .
    13. ResultSetMetaData rsmd = rs.getMetaData();
    14. int colCount = rsmd.getColumnCount();
    15. while (rs.next()) {
    16. Map<String, String> map = new HashMap<String, String>();
    17. for(int r=1;r<colCount+1;r++)
    18. {
    19. map.put(rsmd.getColumnName(r),rs.getString(rsmd.getColumnName(r)));
    20. }
    21. results.add(map);
    22. }
    23. } catch (Exception ex) {
    24. ex.printStackTrace();
    25. } finally {
    26. //释放资源
    27. ClickHouseConfig.release(conn, stmt, rs);
    28. }
    29. return results;
    30. }

    4.插入clickhouse例子

    1. public class Clickhouse_Insert {
    2. public static String insertsql(List<Map<String, String>> itemEle) {
    3. String sql="INSERT INTO test.bigtest" +
    4. "(id, name, age, adress, city, habit)" +
    5. "VALUES(?, ?, ?, ?, ?, ?);";
    6. Connection conn = null;
    7. PreparedStatement stmt = null;
    8. try {
    9. conn = ClickHouseConfig.getConnection();
    10. conn.setAutoCommit(false);
    11. stmt = conn.prepareStatement(sql);
    12. for (int i = 0; i < itemEle.size(); i++) {
    13. stmt.setInt(1,Integer.parseInt(itemEle.get(i).get("id")));
    14. stmt.setString(2,itemEle.get(i).get("name"));
    15. stmt.setString(3,itemEle.get(i).get("age"));
    16. stmt.setString(4,itemEle.get(i).get("adress"));
    17. stmt.setString(5,itemEle.get(i).get("city"));
    18. stmt.setString(6,itemEle.get(i).get("habit"));
    19. stmt.addBatch();
    20. }
    21. // 执行update
    22. stmt.executeBatch();
    23. conn.commit();
    24. } catch (SQLException e) {
    25. e.printStackTrace();
    26. } finally {
    27. ClickHouseConfig.release(conn, stmt, null);
    28. }
    29. return "1";
    30. }

  • 相关阅读:
    vue-template-admin项目的主题样式设置
    全球第六大IT服务提供商富士通遭黑客攻击:多个系统被感染、客户敏感数据泄露
    【华为OD机试真题 python】流水线【2022 Q4 | 100分】
    知识表示学习(三):TransR
    微软出品,166页深度解读,多模态GPT-4V
    OpenCV For Unity Mat容器的创建与矩阵操作基础
    数据结构中常见的哈希表,到底是什么?
    快速入门Spring Cloud OpenFeign
    vue 预览视频
    5 运算符、表达式和语句
  • 原文地址:https://blog.csdn.net/m0_37942145/article/details/126406752