1,读数据库数据实例
def getDataForDb() : DataFrame ={
//获取sparkSession
val spark: SparkSession = SparkSession.builder().master("local").appName("getData")
.config("spark.sql.shuffle.partitions", 1).getOrCreate()
val properties: Properties = new Properties()
properties.setProperty("user", "root")
properties.setProperty("password", "123456")
properties.setProperty("driver", "com.mysql.jdbc.Driver")
val winds = spark.read.jdbc("jdbc:mysql://localhost:3306/weather?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8", "(select wind_speed from weather) T", properties)
return winds
}
2.存入数据库
def updateForDb(rdd : RDD[(String, Int)] ) : Boolean ={
//存入RDD的每一条数据
rdd.foreachPartition(
it => {
var url = "jdbc:mysql://localhost:3306/weather?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"
val conn = DriverManager.getConnection(url, "root"
Spark实践:数据库词频统计

本文通过一个实例展示了如何使用Spark进行词频统计,并将结果存储到数据库中,包括从数据库读取数据、处理数据及最终存回数据库的步骤。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



