Spark读取Mysql,Redis,Hbase数据(一)
1、读取Mysql数据
object JdbcRddDemo {
def getConn() = {
Class.forName("com.mysql.jdbc.Driver").newInstance()
DriverManager.getConnection("jdbc:mysql://hadoop000:3306/hive", "root", "root")
}
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setAppName("jdbcRdd").setMaster("local[*]")
val sc = new SparkContext(sparkConf)
val jdbcRDD = new JdbcRDD(
sc,
getConn,
"select * from TBLS where TBL_ID >= ? and TBL_ID <= ?",
1,
10,
2,
rs => {
val id = rs.getInt(1)
val name = rs.getString(2)
val age = rs.getInt(3)
(id,name,age)
}
)
jdbcRDD.collect().toBuffer
}
} 相关推荐
ribavnu 2020-11-16
wangshuangbao 2020-11-13
苏康申 2020-11-13
vivenwan 2020-11-13
moyekongling 2020-11-13
云中舞步 2020-11-12
要啥自行车一把梭 2020-11-12
kuwoyinlehe 2020-11-12
minerk 2020-11-12
vitasfly 2020-11-12
jazywoo在路上 2020-11-11
敏敏张 2020-11-11