gpt4 book ai didi

python - 无法通过python spark连接MysqlDB

转载 作者:行者123 更新时间:2023-11-29 01:53:25 26 4
gpt4 key购买 nike

我想将我处理过的 rdd 保存到 mysql 表中,因为我正在使用 SparkDataFrame 但出现以下错误

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

spark-shell -–driver-class-path/path-to-mysql-jar/mysql-connectorjava-5.1.38-bin.jar。

    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")

我正在使用 spark spark-1.5.0-bin-hadoop2.4

还设置了 Hive Metastore。

那么我怎样才能将我的 RDD 结果加载到 Mysql 表中。

输入文件是

  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

我想要表格输出,

  std_id  std_code  std_res

1234567 65656545 24

1234567 65675859 24

最佳答案

通过将 --jar/path/to/mysql/connector 添加到 spark submit 来解决它,

 ./bin/spark-submit --jars lib/mysql-connector-java-5.1.38-bin.jar sample.py

关于python - 无法通过python spark连接MysqlDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35224457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com