Spark读取mysql数据库实例
时间:2021-07-01 10:21:17
帮助过:33人阅读
= SparkSession.builder().master("local").appName("getDatafromMysql"
)
.config("spark.sql.shuffle.partitions", 1
).getOrCreate()
val properties: Properties =
new Properties()
properties.setProperty("user", "root"
)
properties.setProperty("password", "root"
)
properties.setProperty("driver", "com.mysql.jdbc.Driver"
)
//方式一
val person: DataFrame = spark.read.jdbc("jdbc:mysql://localhost:3306/acc", "ttt"
, properties)
person.show()
//方式二
spark.read.jdbc("jdbc:mysql://localhost:3306/acc", "(select * from ut_tt) T"
, properties).show()
}
}
所需引用:
import org.apache.spark.sql.DataFrame
import org.apache.spark.sql.SparkSession
import java.util.Properties
在pom文件中,添加mysql-connector-java引用:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>*****</version>
</dependency>
4.运行结果:
Spark读取mysql数据库实例
标签:session config href sql property mys create color ORC