1.在IDEA上建立一个sparksql_mysql的scala对象。
2.连接mysql的代码如下
import java.sql.{DriverManager, PreparedStatement, Connection}
import org.apache.spark.rdd.JdbcRDD
import org.apache.spark.{SparkContext, SparkConf} object SparkSql_mysql {
def main(args: Array[String]) {
// connect to the database named "mysql" on the localhost
val driver = "com.mysql.jdbc.Driver"
val url = "jdbc:mysql://localhost:3306/test"
val username = "root"
val password = "" // there's probably a better way to do this
var connection:Connection = null try {
// make the connection
Class.forName(driver)
connection = DriverManager.getConnection(url, username, password) // create the statement, and run the select query
val statement = connection.createStatement() val inserSql="insert into person(person_id,first_name,last_name,gender) values(6,'yuefei','zhang','w');"
statement.executeUpdate(inserSql) val resultSet = statement.executeQuery("SELECT first_name,last_name FROM person")
while ( resultSet.next() ) {
val first_name = resultSet.getString("first_name")
val last_name = resultSet.getString("last_name")
println("first_name, last_name = " + first_name + ", " + last_name)
} println("resultSet",resultSet) } catch {
case e:Exception =>
e.printStackTrace
// case e => e.printStackTrace
// case _: Throwable =>
// println("error")
} connection.close() } }
3.说明:
val driver = "com.mysql.jdbc.Driver" //指定mysql的dirver
val url = "jdbc:mysql://localhost:3306/test" //指定mysql的地址,test为数据库
val username = "root" //指定mysql的用户名
val password = "" //密码
spark自带hivesql运行:./bin/run-example org.apache.spark.examples.sql.hive.HiveFromSpark