• sparksql链接mysql


    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
     
  • 相关阅读:
    [ SDOI 2006 ] 保安站岗
    [ TJOI 2012 ] 防御
    [ ZJOI 2012 ] 灾难
    [ HNOI 2008 ] 玩具装箱
    「UER#2」信息的交换
    「UR#5」怎样跑得更快
    「UR#6」懒癌
    清华集训2014 做题记录
    「UR#5」怎样更有力气
    弦图及区间图学习笔记
  • 原文地址:https://www.cnblogs.com/aijianiula/p/4741320.html
Copyright © 2020-2023  润新知