欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spark读取Mysql,Redis,Hbase数据

程序员文章站 2022-03-24 09:04:58
1、读取Mysql数据 object JdbcRddDemo { def getConn() = { Class.forName("com.mysql.jdbc.Driver").newInstance() DriverManager.getConnection("jdbc:mysql://hado ......

1、读取mysql数据

object jdbcrdddemo {

  def getconn() = {
    class.forname("com.mysql.jdbc.driver").newinstance()
    drivermanager.getconnection("jdbc:mysql://hadoop000:3306/hive", "root", "root")
  }

  def main(args: array[string]): unit = {
    val sparkconf = new sparkconf().setappname("jdbcrdd").setmaster("local[*]")
    val sc = new sparkcontext(sparkconf)

    val jdbcrdd = new jdbcrdd(
      sc,
      getconn,
      "select * from tbls where tbl_id >= ? and tbl_id <= ?",
      1,
      10,
      2,
      rs => {
        val id = rs.getint(1)
        val name = rs.getstring(2)
        val age = rs.getint(3)
        (id,name,age)
      }
    )

    jdbcrdd.collect().tobuffer
  }
}

转自:https://www.cnblogs.com/hgc-bky/p/12863544.html