package com.atguigu.bigdata.spark.mysql
import java.io
import java.sql.{
Connection, DriverManager, PreparedStatement}
import org.apache.spark.rdd.{
JdbcRDD, RDD}
import org.apache.spark.{
SparkConf, SparkContext}
object ChinaBook {
def main(args: Array[String]): Unit = {
val config: SparkConf = new SparkConf().setMaster("local[1]").setAppName("Spark_hive")
val sc = new SparkContext(config)
val driver="com.mysql.jdbc.Driver"
val url="jdbc:mysql://10.16.104.200:3306/duan"
val sql="select * from ChinaBook where ID >= ? and ID <= ?"
//查询-------------------------------------------------------------
val jdbc: JdbcRDD[List[Any]] = new JdbcRDD(
sc,
() => {
Class.forName(driver).newInstance()
DriverManager.getConnection(url, "root", "password")
},
sql,
1,
200000,
2,
(rs) => {
Spark操作mysql数据库
最新推荐文章于 2023-01-30 12:46:28 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)