首先需要开启大数据集群环境,然后导入Hive和MySQL的驱动包,再创建MySQL和Hive的连接封装类
Hive连接封装类:
package Conn
import java.sql.{Connection, DriverManager}
object HiveConn {
def conn(url:String,username:String,password:String):Connection = {
Class.forName("org.apache.hive.jdbc.HiveDriver")
val conn = DriverManager.getConnection(url,username,password)
conn
}
}
MySQL连接封装类:
package Conn
import java.sql.{Connection, DriverManager}
object MySQLConn {
def Conn(url: String, username: String, password: String): Connection = {
Class.forName("com.mysql.cj.jdbc.Driver")
val conn = DriverManager.getConnection(url, username, password)
conn
}
}
在创建完连接之后就是将MySQL中的文件导出到文件,然后上传到hdfs中,然后hive从hdfs中直接读取文件到表中这么一个过程:
package Conn
object useMySQL {
def main(args: Array[String]): Unit = {
Export()
}
//传入要导出数据的标名
def Export():Unit={
val url = "jdbc:mysql://192.168.88.151:3306/shtd_store"
val username = "root"
val password = "hadoop"
val MConn = MySQLConn.Conn(url, username, password)
val MSQL = "select * from user_info into outfile '/var/lib/mysql-files/user_info.txt'"
val MStat = MConn.createStatement()
MStat.executeQuery(MSQL)
}
def exception():Unit = {
val url = "jdbc:hive2://192.168.88.151:10000/shtd_store"
val username = "root"
val conn = HiveConn.conn(url,username,"")
val HSQLInto = "load data infile '/var/lib/mysql-files/user_info.txt' into user_info"
val stat = conn.createStatement()
stat.executeQuery(HSQLInto)
}
}
在执行完导出数据到文件的方法之后,需要将文件上传大hdfs,然后提前在hive中建立好需要导入文件的表,然后就可以开始执行导入文件的方法了。