spark SQL学习(spark连接 mysql)

spark连接mysql(打jar包方式)
package wujiadong_sparkSQL

import java.util.Properties

import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}

/**
  * Created by Administrator on 2017/2/14.
  */
object JdbcOperation {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("JdbcOperation")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    val properties = new Properties()
    properties.put("user","feigu")
    properties.put("password","feigu")
    val url = "jdbc:mysql://slave02:3306/testdb?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull"
    val stud_scoreDF = sqlContext.read.jdbc(url,"stud_score",properties)
    stud_scoreDF.show()

  }

}

提交集群
hadoop@master:~/wujiadong sparksubmitdriverclasspath/home/hadoop/bigdata/hive/lib/mysqlconnectorjava5.1.102.jarclasswujiadongsparkSQL.JdbcOperationexecutormemory500mtotalexecutorcores2/home/hadoop/wujiadong/wujiadong.spark.jarhadoop@master: /wujiadong spark-submit –jars /home/hadoop/bigdata/hive/lib/mysql-connector-java-5.1.10-2.jar –class wujiadong_sparkSQL.JdbcOperation –executor-memory 500m –total-executor-cores 2 /home/hadoop/wujiadong/wujiadong.spark.jar

运行结果
hadoop@master:~/wujiadong$ spark-submit –driver-class-path /home/hadoop/bigdata/hive/lib/mysql-connector-java-5.1.10-2.jar –class wujiadong_sparkSQL.JdbcOperation –executor-memory 500m –total-executor-cores 2 /home/hadoop/wujiadong/wujiadong.spark.jar
17/02/15 13:21:06 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform… using builtin-java classes where applicable
17/02/15 13:21:09 INFO Slf4jLogger: Slf4jLogger started
17/02/15 13:21:09 INFO Remoting: Starting remoting
17/02/15 13:21:09 INFO Remoting: Remoting started; listening on addresses :[akka.tcp://sparkDriver@192.168.1.131:40654]
17/02/15 13:21:13 WARN MetricsSystem: Using default name DAGScheduler for source because spark.app.id is not set.
+———-+——–+——–+——–+———+———+
| stud_code|sub_code|sub_name|sub_tech|sub_score|stat_date|
+———-+——–+——–+——–+———+———+
|2015101000| 10101| 数学分析| | 90| null|
|2015101000| 10102| 高等代数| | 88| null|
|2015101000| 10103| 大学物理| | 67| null|
|2015101000| 10104| 计算机原理| | 78| null|
|2015101000| 10105| 电磁学| | 89| null|
|2015101001| 10101| 数学分析| | 87| null|
|2015101001| 10102| 高等代数| | 78| null|
|2015101001| 10103| 大学物理| | 88| null|
|2015101001| 10104| 计算机原理| | 86| null|
|2015101001| 10105| 电磁学| | 91| null|
|2015101002| 10101| 数学分析| | 98| null|
|2015101002| 10102| 高等代数| | 97| null|
|2015101002| 10103| 大学物理| | 95| null|
|2015101002| 10104| 计算机原理| | 96| null|
|2015101002| 10105| 电磁学| | 90| null|
|2015101003| 10101| 数学分析| | 70| null|
|2015101003| 10102| 高等代数| | 87| null|
|2015101003| 10103| 大学物理| | 65| null|
|2015101003| 10104| 计算机原理| | 98| null|
|2015101003| 10105| 电磁学| | 76| null|
+———-+——–+——–+——–+———+———+
only showing top 20 rows

17/02/15 13:21:24 INFO RemoteActorRefProvider RemotingTerminator:Shuttingdownremotedaemon.17/02/1513:21:24INFORemoteActorRefProvider RemotingTerminator: Remote daemon shut down; proceeding with flushing remote transports.

常见报错1
Exception in thread “main” java.sql.SQLException: No suitable driver found for jdbc:mysql://slave02:3306/testdb
报错原因是没有jdbc驱动

解决办法
–driver-class-path xxx.jar 或者
–jars xxx.jar
如果添加了命令和jar运行也不行,则用以下办法

在%JAVA_HOME%\jre\lib\ext下添加mysql-connector-java-5.1.12-bin.jar 问题解决
常见报错2
java.sql.SQLException: Value ‘0000-00-00’ can not be represented as java.sql.Date

0000-00-00 ”在MySQL中是作为一个特殊值存在的,但是在Java中, java.sql.Date 会被视为 不合法的值,被JVM认为格式不正确。

解决办法:在jdbc的url加上 zeroDateTimeBehavior参数

url = “jdbc:mysql://slave02:3306/testdb?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull”
spark连接mysql(spark shell方式)
方式1

//import sqlContext.implicits._ //有时需要用到,需要时导入
scala> import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.SQLContext

scala> val sqlContext = new SQLContext(sc)
sqlContext: org.apache.spark.sql.SQLContext = org.apache.spark.sql.SQLContext@6cd1ee

scala> val url =”jdbc:mysql://slave02:3306/testdb?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull”
url: String = jdbc:mysql://slave02:3306/testdb?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull

scala> val prop = new java.util.Properties
prop: java.util.Properties = {}

scala> prop.setProperty(“user”,”feigu”)
res3: Object = null

scala> prop.setProperty(“password”,”feigu”)
res4: Object = null

scala> val stud_scoreDF = sqlContext.read.jdbc(url,”stud_score”,prop)
stud_scoreDF: org.apache.spark.sql.DataFrame = [stud_code: string, sub_code: string, sub_name: string, sub_tech: string, sub_score: int, stat_date: date]

scala> stud_scoreDF.show()
+———-+——–+——–+——–+———+———+
| stud_code|sub_code|sub_name|sub_tech|sub_score|stat_date|
+———-+——–+——–+——–+———+———+
|2015101000| 10101| 数学分析| | 90| null|
|2015101000| 10102| 高等代数| | 88| null|
|2015101000| 10103| 大学物理| | 67| null|
|2015101000| 10104| 计算机原理| | 78| null|
|2015101000| 10105| 电磁学| | 89| null|
|2015101001| 10101| 数学分析| | 87| null|
|2015101001| 10102| 高等代数| | 78| null|
|2015101001| 10103| 大学物理| | 88| null|
|2015101001| 10104| 计算机原理| | 86| null|
|2015101001| 10105| 电磁学| | 91| null|
|2015101002| 10101| 数学分析| | 98| null|
|2015101002| 10102| 高等代数| | 97| null|
|2015101002| 10103| 大学物理| | 95| null|
|2015101002| 10104| 计算机原理| | 96| null|
|2015101002| 10105| 电磁学| | 90| null|
|2015101003| 10101| 数学分析| | 70| null|
|2015101003| 10102| 高等代数| | 87| null|
|2015101003| 10103| 大学物理| | 65| null|
|2015101003| 10104| 计算机原理| | 98| null|
|2015101003| 10105| 电磁学| | 76| null|
+———-+——–+——–+——–+———+———+
only showing top 20 rows
方式2
scala> import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.SQLContext

scala> val sqlContext = new SQLContext(sc)
sqlContext: org.apache.spark.sql.SQLContext = org.apache.spark.sql.SQLContext@351d726c

scala> import sqlContext.implicits._
import sqlContext.implicits._

scala> val url =”jdbc:mysql://slave02:3306/testdb?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull”
url: String = jdbc:mysql://slave02:3306/testdb?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull

scala> val table = “stud_score”
table: String = stud_score

scala> val reader = sqlContext.read.format(“jdbc”)
reader: org.apache.spark.sql.DataFrameReader = org.apache.spark.sql.DataFrameReader@49c37918

scala> val reader = sqlContext.read.format(“jdbc”)
reader: org.apache.spark.sql.DataFrameReader = org.apache.spark.sql.DataFrameReader@49c37918

scala> reader.option(“url”,url)
res0: org.apache.spark.sql.DataFrameReader = org.apache.spark.sql.DataFrameReader@49c37918

scala> reader.option(“dbtable”,table)
res4: org.apache.spark.sql.DataFrameReader = org.apache.spark.sql.DataFrameReader@49c37918

scala> reader.option(“driver”,”com.mysql.jdbc.Driver”)
res6: org.apache.spark.sql.DataFrameReader = org.apache.spark.sql.DataFrameReader@49c37918

scala> reader.option(“user”,”feigu”)
res7: org.apache.spark.sql.DataFrameReader = org.apache.spark.sql.DataFrameReader@49c37918

scala> reader.option(“password”,”feigu”)
res8: org.apache.spark.sql.DataFrameReader = org.apache.spark.sql.DataFrameReader@49c37918

scala> val DF = reader.load()
DF: org.apache.spark.sql.DataFrame = [stud_code: string, sub_code: string, sub_name: string, sub_tech: string, sub_score: int, stat_date: date]

scala> DF.show()
+———-+——–+——–+——–+———+———+
| stud_code|sub_code|sub_name|sub_tech|sub_score|stat_date|
+———-+——–+——–+——–+———+———+
|2015101000| 10101| 数学分析| | 90| null|
|2015101000| 10102| 高等代数| | 88| null|
|2015101000| 10103| 大学物理| | 67| null|
|2015101000| 10104| 计算机原理| | 78| null|
|2015101000| 10105| 电磁学| | 89| null|
|2015101001| 10101| 数学分析| | 87| null|
|2015101001| 10102| 高等代数| | 78| null|
|2015101001| 10103| 大学物理| | 88| null|
|2015101001| 10104| 计算机原理| | 86| null|
|2015101001| 10105| 电磁学| | 91| null|
|2015101002| 10101| 数学分析| | 98| null|
|2015101002| 10102| 高等代数| | 97| null|
|2015101002| 10103| 大学物理| | 95| null|
|2015101002| 10104| 计算机原理| | 96| null|
|2015101002| 10105| 电磁学| | 90| null|
|2015101003| 10101| 数学分析| | 70| null|
|2015101003| 10102| 高等代数| | 87| null|
|2015101003| 10103| 大学物理| | 65| null|
|2015101003| 10104| 计算机原理| | 98| null|
|2015101003| 10105| 电磁学| | 76| null|
+———-+——–+——–+——–+———+———+
only showing top 20 rows
方式3
scala> import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.SQLContext

scala> val sqlContext = new SQLContext(sc)
sqlContext: org.apache.spark.sql.SQLContext = org.apache.spark.sql.SQLContext@fdf029a

scala> val jdbcDF = sqlContext.read.format(“jdbc”).options(Map(“url” -> “jdbc:mysql://slave02:3306/testdb?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull”,”driver” -> “com.mysql.jdbc.Driver”, “dbtable” -> “testdb.stud_score”,”user” -> “feigu”,”password” -> “feigu”)).load()
jdbcDF: org.apache.spark.sql.DataFrame = [stud_code: string, sub_code: string, sub_name: string, sub_tech: string, sub_score: int, stat_date: date]

scala> jdbcDF.show()
+———-+——–+——–+——–+———+———+
| stud_code|sub_code|sub_name|sub_tech|sub_score|stat_date|
+———-+——–+——–+——–+———+———+
|2015101000| 10101| 数学分析| | 90| null|
|2015101000| 10102| 高等代数| | 88| null|
|2015101000| 10103| 大学物理| | 67| null|
|2015101000| 10104| 计算机原理| | 78| null|
|2015101000| 10105| 电磁学| | 89| null|
|2015101001| 10101| 数学分析| | 87| null|
|2015101001| 10102| 高等代数| | 78| null|
|2015101001| 10103| 大学物理| | 88| null|
|2015101001| 10104| 计算机原理| | 86| null|
|2015101001| 10105| 电磁学| | 91| null|
|2015101002| 10101| 数学分析| | 98| null|
|2015101002| 10102| 高等代数| | 97| null|
|2015101002| 10103| 大学物理| | 95| null|
|2015101002| 10104| 计算机原理| | 96| null|
|2015101002| 10105| 电磁学| | 90| null|
|2015101003| 10101| 数学分析| | 70| null|
|2015101003| 10102| 高等代数| | 87| null|
|2015101003| 10103| 大学物理| | 65| null|
|2015101003| 10104| 计算机原理| | 98| null|
|2015101003| 10105| 电磁学| | 76| null|
+———-+——–+——–+——–+———+———+
only showing top 20 rows

//注册为一个表。这就可以直接进行select等操作样
scala> jdbcDF.registerTempTable(“wu_stud_info”)
scala> jdbcDF.sqlContext.sql(“select sub_name from wu_stud_info”).collect.foreach(println)
[数学分析]
[高等代数]
[大学物理]
[计算机原理]
[电磁学]
[数学分析]
[高等代数]
[大学物理]
[计算机原理]
[电磁学]
[数学分析]
[高等代数]
[大学物理]
[计算机原理]
[电磁学]
[数学分析]
[高等代数]
[大学物理]
[计算机原理]
[电磁学]
[数学分析]
[高等代数]
[大学物理]
[计算机原理]
[电磁学]
[数学分析]
[高等代数]
[大学物理]
[计算机原理]
[电磁学]
[数学分析]
[高等代数]
[大学物理]
[计算机原理]
[电磁学]
[数学分析]
[高等代数]
[大学物理]
[计算机原理]
[电磁学]
[数学分析]
[高等代数]
[大学物理]
[计算机原理]
[电磁学]
[数学分析]
[高等代数]
[大学物理]
[计算机原理]
[电磁学]
[数学分析]
[高等代数]
[大学物理]
[计算机原理]
[电磁学]
[计算机软件与理论]
[计算机系统结构]
[操作系统]
[概率统计]
[汇编语言]
[数据结构]
[计算机软件与理论]
[计算机系统结构]
[操作系统]
[概率统计]
[汇编语言]
[数据结构]
[计算机软件与理论]
[计算机系统结构]
[操作系统]
[概率统计]
[汇编语言]
[数据结构]
[计算机软件与理论]
[计算机系统结构]
[操作系统]
[概率统计]
[汇编语言]
[数据结构]
[计算机软件与理论]
[计算机系统结构]
[操作系统]
[概率统计]
[汇编语言]
[数据结构]
[计算机软件与理论]
[计算机系统结构]
[操作系统]
[概率统计]
[汇编语言]
[数据结构]
[计算机软件与理论]
[计算机系统结构]
[操作系统]
[概率统计]
[汇编语言]
[数据结构]
[计算机软件与理论]
[计算机系统结构]
[操作系统]
[概率统计]
[汇编语言]
[数据结构]
[计算机软件与理论]
[计算机系统结构]
[操作系统]
[概率统计]
[汇编语言]
[数据结构]
[计算机软件与理论]
[计算机系统结构]
[操作系统]
[概率统计]
[汇编语言]
[数据结构]
[计算机软件与理论]
[计算机系统结构]
[操作系统]
[概率统计]
[汇编语言]
[数据结构]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值