我一直在尝试但无法使用来自Google colab的pyspark从Windows 10上的localhost上的MySQL Server 8.0.19写入/读取表。还有很多类似的问题,并给出了一些建议的答案,但似乎所有解决方案都无法在此工作。这是我的代码:
<...installations ...>
from pyspark.sql import SparkSession
spark = SparkSession\
.builder\
.appName("Word Count")\
.config("spark.driver.extraClassPath", "/content/spark-2.4.5-bin-hadoop2.7/jars/mysql-connector-java-8.0.19.jar")\
.getOrCreate()
这里是连接字符串:
MyjdbcDF = spark.read.format("jdbc")\
.option("url", "jdbc:mysql://127.0.0.1:3306/mydb?user=testuser&password=pwtest")\
.option("dbtable", "collisions")\
.option("driver","com.mysql.cj.jdbc.Driver")\
.load()
我也已经使用过了,.option("driver","com.mysql.jdbc.Driver")
但仍然不断收到此错误:
Py4JJavaError: An error occurred while calling o154.load.
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
...
...
...
Caused by: java.net.ConnectException: Connection refused (Connection refused)
由此看来,我猜想MySQL Sever无法实现。我已远程登录到端口3306,它确认MySQL服务器正在接受来自客户端计算机的连接。我已经阅读了运行中的内容:netsh advfirewall firewall add rule name="MySQL Server" action=allow protocol=TCP dir=in localport=3306
如果MySQL Server被阻止,则允许防火墙规则,但没有任何更改。
有人可以帮忙吗?
经过大约几天的试验,我找到了解决方案,这就是为什么我要回答自己的问题。我能够使用WAMP服务器进行连接(感谢@Shubham Jain的建议),也可以不使用WAMP服务器进行连接。这个答案是没有WAMP服务器的。
ngrok
从https://ngrok.com/下载,
解压缩,
保存在我的本地窗口中,
并通过以下方式进行验证
./ngrok authtoken xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
:(网站上提供了非常简单明了的说明)
仍在本地Windows上,我ngrok tcp 3306
在命令行上复制并运行
C:\Users\userMe> ngrok tcp 3306
它给像这样的东西:
ngrok by @inconshreveable
Session Status online
Account userMe (Plan: Free)
Version 2.3.35
Region United States (us)
Web Interface http://localhost:4041
Forwarding tcp://0.tcp.ngrok.io:17992 -> localhost:3306
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
哪里0.tcp.ngrok.io:17992
是我唯一感兴趣的事情,那里3306
是MySQL的和唯一的港口,我在暴露于互联网与我的谷歌Colab链接感兴趣。
因此,最终,我的PySpark READ连接看起来像:
jdbcDF = spark.read.format("jdbc")\
.option("url", "jdbc:mysql://0.tcp.ngrok.io:17992/mydb?user=testUser&password=pestpw")\
.option("dbtable", "pipeLineTable")\
.option("driver","com.mysql.cj.jdbc.Driver")\
.load();
WRITE连接将是:
jdbcDF.write.mode("overwrite")\
.format("jdbc")\
.option("url", f"jdbc:mysql://0.tcp.ngrok.io:17992/mydb")\
.option("dbtable", "fromGcTable")\
.option("user", "testUser")\
.option("password", "testpw")\
.option("driver","com.mysql.cj.jdbc.Driver")\
.save()
在两个连接字符串中,请注意将0.tcp.ngrok.io:17992
替换为localhost:3306
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句