Spark Core实战:使用JDBC RDD操作数据库

1.需求:

使用JDBC RDD 操作数据库

2.在数据库中建表并插入数据:

1
2
3
4
5
6
7
8
9
10
create table emp(
id int(11),
ename varchar(20),
deptno int(11),
sal int(11));

insert into emp values(1,"Tom",10,2500);
insert into emp values(2,"Movle",11,1000);
insert into emp values(2,"Mike",10,1500);
insert into emp values(2,"jack",11,500);

3.添加JDBC驱动

4.写代码:

(1)MyJDBCRddDemo.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.rdd.JdbcRDD
import java.sql.DriverManager

/**
* 使用JDBC RDD 操作数据库
*/


object MyJDBCRddDemo {

val connection = () => {
Class.forName("com.mysql.jdbc.Driver").newInstance()
DriverManager.getConnection("jdbc:mysql://localhost:3306/company?serverTimezone=UTC&characterEncoding=utf-8", "root", "123456")
}

def main(args: Array[String]): Unit = {

//创建Spark对象
val conf = new SparkConf().setAppName("My JDBC Rdd Demo").setMaster("local")
val sc = new SparkContext(conf)

val mysqlRDD = new JdbcRDD(sc,connection,"select * from emp where sal > ? and sal <= ?",900,2000, 2, r=>{
//获取员工的姓名和薪水
val ename = r.getString(2)
val sal = r.getInt(4)
(ename,sal)
})

val result = mysqlRDD.collect()
println(result.toBuffer)
sc.stop
}
}

5.结果:

打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2021 Movle
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信