首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法通过python火花连接MysqlDB

无法通过python火花连接MysqlDB
EN

Stack Overflow用户
提问于 2016-02-05 12:37:11
回答 2查看 1K关注 0票数 2

我想将已处理的rdd保存到mysql表中,因为我使用的是SparkDataFrame,但是我得到了后续错误。

代码语言:javascript
运行
复制
py4j.protocol.Py4JJavaError: An error occurred while calling o216.jdbc.
: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/student?user=root&password=root.

我把mysql-jar添加到了sparkshell中。

火花壳驱动程序类路径/path-to-mysql-jar/mysql-connectorjava-5.1.38-bin.jar.

代码语言:javascript
运行
复制
    from pyspark import SparkContext
    from datetime import datetime
    import os
    import sys
    from pyspark.sql import SQLContext, Row
    sqlContext = SQLContext(sc)
    file1 = sc.textFile("/home/hadoop/text1").cache()
    file2 = sc.textFile("/home/hadoop/text2").cache()
    file3 = file1.union(file2).coalesce(1).map(lambda line: line.split(','))
    file1.unpersist()
    file2.unpersist()
    result = file3.map(lambda x: (x[0]+', '+x[1],float(x[2]))).reduceByKey(lambda a,b:a+b).sortByKey('true').coalesce(1)
    result = result.map(lambda x:x[0]+','+str(x[1]))\
    schema_site = sqlContext.createDataFrame(result)
    schema_site.registerTempTable("table1")
    mysql_url="jdbc:mysql://localhost:3306/test?user=root&password=root&driver=com.mysql.jdbc.Driver"
    schema_site.write.jdbc(url=mysql_url, table="table1", mode="append")

我用的是火花-1.5.0-bin-hadoop2.4

还设置了蜂巢亚稳态。

因此,我如何将RDD结果加载到Mysql表中。

输入文件是

代码语言:javascript
运行
复制
  file1 contents are

  1234567  65656545  12

  1234567  65675859  11 

  file2 contents are,

  1234567  65656545  12

  1234567  65675859  11

 and the resultnat RDD is like

 1234567  65656545 24

 1234567  65675859  22

i created the table in mysql with three colunm,

std_id  std_code  std_res

我想要表的输出,

代码语言:javascript
运行
复制
  std_id  std_code  std_res

  1234567  65656545    24

   1234567  65675859   24
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-05 20:19:39

当将jdbc驱动程序或其他java依赖项传递给spark程序时,您应该使用--jars参数。

票数 1
EN

Stack Overflow用户

发布于 2016-02-11 13:14:26

通过添加-jar/path/ to /mysql/连接器来解决这个问题,

代码语言:javascript
运行
复制
 ./bin/spark-submit --jars lib/mysql-connector-java-5.1.38-bin.jar sample.py
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35224457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档