Spark报错:ClassNotFoundException: com.mysql.jdbc.Driver和Public Key Retrieval is not allowed


执行下面这条往mysql数据库插入数据的语句,触发了下面两条错误:

data_df.write.jdbc(url,tablename,'append', prop)

错误一: ClassNotFoundException: com.mysql.jdbc.Driver

  • ClassNotFoundException: com.mysql.jdbc.Driver

错误一解决方法

  • 把mysql的jar包mysql-connector-java-5.1.49.jar放到pyspark的jars目录下/site-packages/pyspark/jars

错误二:Public Key Retrieval is not allowed

  • Public Key Retrieval is not allowed

错误二解决方法

  • 配置连接数据库的url时,加上&allowPublicKeyRetrieval=true
  • 代码中既可以加在url后面,也可以加在prop中,如下所示
  • “allowPublicKeyRetrieval”:“true”
prop={
    
    "user":user,"password":password,"driver":"com.mysql.jdbc.Driver","allowPublicKeyRetrieval":"true"}
data_df.write.jdbc(url,tablename,'append', prop)

错误三:Establishing SSL connection without server’s identity verification is not recommended

  • Establishing SSL connection without server’s identity verification is not recommended

错误三解决方法

  • url后面加上如下一串参数
  • ?useUnicode=true&characterEncoding=utf-8&useSSL=false
url='jdbc:mysql://10.10.128.146/unified_view_test?useUnicode=true&characterEncoding=utf-8&useSSL=false'

至此,并能够成功往mysql数据库中插入数据

猜你喜欢

转载自blog.csdn.net/zhengzaifeidelushang/article/details/124795830