实现代码
excel所需的pom依赖
案例实现
实现代码
package excel_mysqlimport org.apache.spark.sql.SparkSession
import java.util.Propertiesobject t1 {def main(args: Array[String]): Unit = {val spark = SparkSession.builder().appName("ExcelToMySQL") //项目名.master("local[*]").getOrCreate()// 读取Excel表val df = spark.read.format("com.crealytics.spark.excel") //需要的包.option("header", "true").load("file:///D:\\sparks\\src\\main\\datas\\d.xlsx") //本地excel文件路径// 创建MySQL连接配置val jdbcHostname = "192.168.10.137" //连接主机ipval jdbcPort = 3306 //端口val jdbcDatabase = "test" //数据库名val jdbcUsername = "root" //用户名val jdbcPassword = "123456" //mysql密码val jdbcUrl = s"jdbc:mysql://${jdbcHostname}:${jdbcPort}/${jdbcDatabase}"val connectionProperties = new Properties()connectionProperties.put("user", s"${jdbcUsername}")connectionProperties.put("password", s"${jdbcPassword}")// 将DataFrame写入MySQL表中df.write.mode("overwrite").jdbc(jdbcUrl, "user_info", connectionProperties) //导入目标的mysql数据库下的表名spark.stop()}
}
excel所需的pom依赖
<dependency><groupId>com.crealytics</groupId><artifactId>spark-excel_2.12</artifactId><version>0.13.7</version></dependency>
案例实现
准备的excel表:
放在路径下面:
执行代码:
在mysql中查看: