| |
| val properties = new ClickHouseProperties |
| properties.setSocketTimeout(ckTimeOut) |
| properties.setUser(ckUser) |
| properties.setPassword(ckPass) |
| |
1、clickhouse判断某表是否存在
| |
| |
| |
| |
| def tableExists(database: String, tablName: String): Boolean = { |
| |
| val dataSource = new ClickHouseDataSource("clickhouse地址", properties) |
| conn = dataSource.getConnection.asInstanceOf[ClickHouseConnectionImpl] |
| val set = conn.getMetaData.getTables(null, database, tablName, null) |
| if (set.next()) { |
| return true |
| } else { |
| println("不存在") |
| return false |
| } |
| } |
| |
2、判断是否存在该列
/**
tablesName:数据库名.表名
cols: 列名
*/
| def colsExists(tablesName: String, cols: String): Boolean = { |
| |
| val dataSource = new ClickHouseDataSource("clickhouse地址", properties) |
| conn = dataSource.getConnection.asInstanceOf[ClickHouseConnectionImpl] |
| |
| val statement = conn.createStatement() |
| val resultSet = statement.executeQuery("select * from " + tablesName) |
| println("select * from " + tablesName) |
| val data = resultSet.getMetaData() |
| val count = data.getColumnCount |
| for (i <- 1 to count) { |
| val str = data.getColumnName(i) |
| println(str + cols) |
| val bool = str.contains(cols) |
| println(bool) |
| return bool |
| } |
| return false |
| } |
| |
3、创建表
| |
| |
| |
| def createTable(sql: String): Unit = { |
| val arr: Array[String] = ckClusterUrl.split(",") |
| import java.sql.{Connection, DriverManager} |
| Class.forName("com.github.housepower.jdbc.ClickHouseDriver") |
| println("表") |
| val connection: Connection = DriverManager.getConnection("clickhouse地址", "用户名", "密码") |
| val statement = connection.createStatement() |
| statement.executeQuery(sql) |
| } |
| |
4、获取数据库中所有表名
| |
| |
| |
| def getOherTables(db: String , ckMasterUrl:String):ArrayBuffer[String] ={ |
| val tableList = new ArrayBuffer[String]() |
| val dataSource = new ClickHouseDataSource(ckMasterUrl, properties) |
| val conn = dataSource.getConnection.asInstanceOf[ClickHouseConnectionImpl] |
| val set: ResultSet = conn.getMetaData.getTables(null, db, null, null) |
| while (set.next()){ |
| val table = set.getString("TABLE_NAME") |
| tableList += table |
| } |
| tableList |
| } |
| |
5、根据某字段内容删除数据
| |
| |
| |
| |
| def deleteDateBytableAndTag(db: String, table: String, resourceId: String) = { |
| try { |
| val arr: Array[String] = ckClusterUrl.split(",") |
| for (e <- arr) { |
| val source = new ClickHouseDataSource(e, properties) |
| val clickHouseConnectionImpl: ClickHouseConnectionImpl = source.getConnection.asInstanceOf[ClickHouseConnectionImpl] |
| var sql = "ALTER TABLE " + db + "." + table + " DELETE WHERE" + " resourceId" + "='" + resourceId + "'" |
| clickHouseConnectionImpl.createStatement().executeQuery(sql) |
| } |
| } catch { |
| case e: Exception => |
| e.printStackTrace() |
| } |
| } |
| |
6、创建数据库库名
| |
| |
| def createDatabase(dbName: String): Unit ={ |
| val urls: Array[String] = ckClusterUrl.split(",") |
| val sql = "create database " + dbName |
| for (elem <- urls) { |
| try{ |
| val source = new ClickHouseDataSource(elem , properties) |
| val impl = source.getConnection().asInstanceOf[ClickHouseConnectionImpl] |
| impl.createStatement().executeQuery(sql) |
| println("创建 ==="+elem+ "======成功") |
| }catch{ |
| case e:Exception => |
| e.printStackTrace() |
| println("创建 ==="+elem+ "======失败") |
| } |
| } |
| } |
| |
7、获取clickhouse数据库中的表的结构
| def readTaleType(database: String, table: String): String = { |
| var str: String = null |
| val sql = String.format("SHOW CREATE TABLE %s", database + "." + table) |
| val statement = conn.prepareStatement(sql) |
| val resultSet = statement.executeQuery() |
| while (resultSet.next()) { |
| str = resultSet.getString(1) |
| } |
| return str |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)