Spark JdbcRDD 简单使用

package org.apache.spark.sql.sources

import org.apache.spark.SparkContext
import java.sql.{ResultSet, DriverManager}
import org.apache.spark.rdd.JdbcRDD

/**
 * @author luogankun
 * Created by spark on 14-12-25.
 */
object JdbcTest extends  App{
    val sc = new SparkContext("local[2]", "demo")

    def getConnection() = {
      Class.forName("com.mysql.jdbc.Driver").newInstance()
      DriverManager.getConnection("jdbc:mysql://hadoop000:3306/hive", "root", "root")
    }

    def flatValue(result: ResultSet) = {
      (result.getInt("TBL_ID"), result.getString("TBL_NAME"))
    }

    //select * from TBLS WHERE TBL_ID>=1 AND TBL_ID<=10
    val data = new JdbcRDD(
      sc,
      getConnection,
      "select * from TBLS where TBL_ID >= ? and TBL_ID <= ?",
      1,
      10,
      2,
      flatValue
    )

    println(data.collect().toList)

    sc.stop
}

执行报错:

查看JdbcRDD代码发现,sql语句一定要带上2个条件:

这个使用起来不太方便,最近需要找时间将JdbcRDD优化下,以便后续更方便的在jdbc external data source中能使用JdbcRDD。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值