美文网首页程序员大数据开发
spark读取mysql某列值,封装成List集合

spark读取mysql某列值,封装成List集合

作者: 晨冉1688 | 来源:发表于2020-05-01 11:54 被阅读0次

    spark读取mysql某列值,封装成List集合

    def main(args: Array[String]) = {
    
        Logger.getLogger("org").setLevel(Level.ERROR)
    
        val sparkSession = SparkSession
          .builder()
          .master("local[*]")
          .appName("readFromMysql")
          .config("spark.sql.broadcastTimeout", "36000")
          .getOrCreate()
    
        val mysqlReader: DataFrame = sparkSession.read.format("jdbc")
          .option("url", "jdbc:mysql://localhost:3306/online?rewriteBatchedStatements=true")
          .option("dbtable", "student")
          .option("user", "root")
          .option("password", "szroot")
          .load()
    
        mysqlReader.createOrReplaceTempView("student")
        val student: List[Row] = sparkSession.sql("select uv from student").rdd.collect().toList
    
         val strings: List[String] = student.map(row => row.mkString)
    
        for (elem <- strings) {
          println(elem)
        }
    
    
      }
    

    相关文章

      网友评论

        本文标题:spark读取mysql某列值,封装成List集合

        本文链接:https://www.haomeiwen.com/subject/ctnfghtx.html