赞
踩
语法:
val sparkSession = SparkSession.builder()
.master("local[*]")
.appName("Spark SQL JDBC")
.getOrCreate()
语法:
val jdbcDF = sparkSession.read
.format("jdbc")
.option("url", "jdbc:mysql://localhost:3306/bigdata0408?characterEncoding=UTF-8")
.option("dbtable", "paydata")
.option("user", "root")
.option("password", "root")
.load()
TempView意为临时的视图,view,它可以使用sql方法进行SQL操作。
语法:
jdbcDF.createOrReplaceTempView("paydata")
package demo11 import org.apache.spark.rdd.RDD import org.apache.spark.sql.{Row, SparkSession} object Test02 { def main(args: Array[String]): Unit = { val sparkSession = SparkSession.builder() .master("local[*]") .appName("Spark SQL JDBC") .getOrCreate() val jdbcDF = sparkSession.read .format("jdbc") .option("url", "jdbc:mysql://localhost:3306/bigdata0408?characterEncoding=UTF-8") .option("dbtable", "paydata") .option("user", "root") .option("password", "root") .load() jdbcDF.createOrReplaceTempView("paydata") //18、计算出商户名称为33333328的所有金额为多少? sparkSession.sql("SELECT sum(money) FROM paydata WHERE posname == '33333328'").show() //19、计算出支付通道为alipay的所有金额? sparkSession.sql("SELECT sum(money) FROM paydata WHERE paychannel == 'alipay'").show() } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。