1 ,创建一个conf对象
val conf = new SparkConf()
.setAppName("moxie")
.setMaster("local[*]")
因为需要连接hive 所以set一下参数
.set("spark.driver.allowMultipleContexts", "true")
.set("hive.metastore.uris", "thrift://192.168.157.133:9083")
.set("spark.sql.warehouse.dir", "/user/hive/warehouse/")
**spark.driver.allowMultipleContexts: ** If true, log warnings instead of throwing exceptions when multiple SparkContexts are active.
* * spark.driver。allowMultipleContexts: **如果为true,当多个sparkcontext处于活动状态时,日志警告而不是抛出异常。
**hive.metastore.uris** hive的metastore地址
**spark.sql.warehouse.dir** hive中的数据库在hadoop该路径下 需要将该路径权限改成777
hdfs dfs -chmod 777 /
2,创建sparkSession对象
val spark = new SparkSession
.Builder()
.config(conf)
.enableHiveSupport() //开启hive支持
.getOrCreate()
3,连接Mysql
val url = "jdbc:mysql://localhost:3306/mydb"
val df = spark.read.format("jdbc")
.option("url",url)
.option("driver","com.mysql.jdbc.Driver")
.option("dbtable","mytable")
.option("user","root")
.option("password","123456")
.load() // 加载
url:mysql的地址
driver:连接驱动
dbtable:表名 //数据库在url后面写