MySQL,Presto,Hive,ClickHouse的JDBC连接

Posted Mr.zhou_Zxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL,Presto,Hive,ClickHouse的JDBC连接相关的知识,希望对你有一定的参考价值。

1 mysql

package com.zxy.bigdata.clickhouse.utils

import java.sql.{Connection, DriverManager, ResultSet, Statement}

import org.slf4j.LoggerFactory

/**
 * @Author zxy
 * @Content 创建JDBC连接对象
 * @Date 2021-07-10
 */
object MySQLUtils {
    private val MYSQL_DRIVER = "com.mysql.jdbc.Driver"
    private val MYSQL_URL = "jdbc:mysql://localhost:3306/zxy"
    private val MYSQL_USER = "root"
    private val MYSQL_PWD = "root"

    /**
     * 创建连接对象
     */
    def getMySQLConnection():Connection = {
        var connection:Connection = null
        try{
            Class.forName(MYSQL_DRIVER)
            connection = DriverManager.getConnection(MYSQL_URL,MYSQL_USER,MYSQL_PWD)
        }catch {
            case e:Exception => print("mysql创建连接异常")
        }
        connection
    }

    /**
     * 关闭Connection对象
     */
    def close(connection: Connection):Unit = {
        if(connection != null) connection.close()
    }

    /**
     * 测试JDBC创建的连接对象是否成功
     * @param args
     */
    def main(args: Array[String]): Unit = {
        val connection: Connection = MySQLUtils.getMySQLConnection()
        val statement: Statement = connection.createStatement()
        val sql = "select * from user"
        val resultSet: ResultSet = statement.executeQuery(sql)
        while (resultSet.next()){
            val username: String = resultSet.getString("username")
            val password: String = resultSet.getString("password")
            println(s"username = ${username},password = ${password}")
        }
    }

}



2 Presto

package com.zxy.bigdata.clickhouse.utils

import java.sql.{Connection, DriverManager}

/**
 * JDBC连接presto的方法
 */
object PrestoUtils {
    def getPrestoConnection(): Connection ={
        Class.forName("com.facebook.presto.jdbc.PrestoDriver")
        val connection: Connection = DriverManager.getConnection("jdbc:presto://192.168.130.111:8080/hive/zxy", "root", null)
        connection
    }
    def close(connection: Connection): Unit ={
        if(connection != null){
            connection.close()
        }
    }
}

3 Hive

package com.zxy.bigdata.clickhouse.utils

import java.sql.{Connection, DriverManager}

object HiveUtils {
    private val MYSQL_DRIVER = "com.mysql.jdbc.Driver"
    private val MYSQL_URL = "jdbc:mysql://192.168.130.111:3306/zxy"
    private val MYSQL_USER = "root"
    private val MYSQL_PWD = "111213"
    
    /**
     * 创建连接对象
     */
    def getHiveConnection():Connection = {
        var connection:Connection = null
        try{
            Class.forName(MYSQL_DRIVER)
            connection = DriverManager.getConnection(MYSQL_URL,MYSQL_USER,MYSQL_PWD)
        }catch {
            case e:Exception => print("hive创建连接异常")
        }
        connection
    }
    
    /**
     * 关闭Connection对象
     */
    def close(connection: Connection):Unit = {
        if(connection != null) connection.close()
    }
}

4 ClickHouse

package com.zxy.bigdata.clickhouse.utils

import ru.yandex.clickhouse.ClickHouseDataSource
import ru.yandex.clickhouse.settings.ClickHouseProperties

/**
 * ClickHouse DataSource
 */
object ClickHouseUtils {
    /**
     * 获取连接到clickhouse的数据源对象
     */
    def getDataSource():ClickHouseDataSource = {
        Class.forName("ru.yandex.clickhouse.ClickHouseDriver")
        val properties = new ClickHouseProperties()
        properties.setUser("root")
        properties.setPassword("111213")
        val dataSource = new ClickHouseDataSource("jdbc:clickhouse://192.168.130.111:8123/zxy",properties)
        dataSource
    }
    
}

以上是关于MySQL,Presto,Hive,ClickHouse的JDBC连接的主要内容,如果未能解决你的问题,请参考以下文章

Presto安装完成之后需要做的

如何将 Presto 数据库连接到 MySQL

Presto查询引擎简单分析

Presto 和 Hive

presto + Hive 安全配置

Presto 和 hive 分区发现