zoukankan      html  css  js  c++  java
  • spark读写mysql

    * 获取 Mysql 表的数据
    *
    * @param sqlContext
    * @param tableName 读取Mysql表的名字
    * @param proPath 配置文件的路径
    * @return 返回 Mysql 表的 DataFrame
    */
    def readMysqlTable(sqlContext: SQLContext, tableName: String, proPath: String) = {
    val properties: Properties = getProPerties(proPath)
    sqlContext
    .read
    .format("jdbc")
    .option("url", properties.getProperty("mysql.url"))
    .option("driver", properties.getProperty("mysql.driver"))
    .option("user", properties.getProperty("mysql.username"))
    .option("password", properties.getProperty("mysql.password"))
    // .option("dbtable", tableName.toUpperCase)
    .option("dbtable", tableName)
    .load()

    }

    /**
    * 获取 Mysql 表的数据 添加过滤条件
    *
    * @param sqlContext
    * @param table 读取Mysql表的名字
    * @param filterCondition 过滤条件
    * @param proPath 配置文件的路径
    * @return 返回 Mysql 表的 DataFrame
    */
    def readMysqlTable(sqlContext: SQLContext, table: String, filterCondition: String, proPath: String) = {
    val properties: Properties = getProPerties(proPath)
    var tableName = ""
    tableName = "(select * from " + table + " where " + filterCondition + " ) as t1"
    sqlContext
    .read
    .format("jdbc")
    .option("url", properties.getProperty("mysql.url"))
    .option("driver", properties.getProperty("mysql.driver"))
    .option("user", properties.getProperty("mysql.username"))
    .option("password", properties.getProperty("mysql.password"))
    .option("dbtable", tableName)
    .load()
    }

  • 相关阅读:
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Codeforces Round #551题解
  • 原文地址:https://www.cnblogs.com/muyue123/p/14047034.html
Copyright © 2011-2022 走看看