gpt4 book ai didi

python - PySpark session 中缺少 java.util.HashMap

转载 作者:太空狗 更新时间:2023-10-30 02:43:58 24 4
gpt4 key购买 nike

我正在使用 Apache Spark Windows 7 x64 上的 1.4.0,IPython 3.2.0 中的 Java 1.8.0_45 x64 和 Python 2.7.10 x86

我正在尝试写一个 DataFrame IPython 笔记本中基于 - 的程序,该程序从 SQL Server 数据库读取和写回。

到目前为止我可以从数据库中读取数据

from pyspark.sql import SQLContext
sqlContext = SQLContext(sc)
df = sqlContext.load(source="jdbc",url="jdbc:sqlserver://serverURL", dbtable="dbName.tableName", driver="com.microsoft.sqlserver.jdbc.SQLServerDriver", user="userName", password="password")

并将数据转换为 Panda 并做任何我想做的事。 (这不仅仅是一个小麻烦,但是在将微软的sqljdbc42.jar添加到spark-defaults.conf中的spark.driver.extraClassPath之后就可以了)

当我使用 DataFrameWriter API 将数据写回 SQL Server 时,出现了当前问题。 :

df.write.jdbc("jdbc:sqlserver://serverURL", "dbName.SparkTestTable1", dict(driver="com.microsoft.sqlserver.jdbc.SQLServerDriver", user="userName", password="password"))

---------------------------------------------------------------------------
Py4JError Traceback (most recent call last)
<ipython-input-19-8502a3e85b1e> in <module>()
----> 1 df.write.jdbc("jdbc:sqlserver://jdbc:sqlserver", "dbName.SparkTestTable1", dict(driver="com.microsoft.sqlserver.jdbc.SQLServerDriver", user="userName", password="password"))

C:\Users\User\Downloads\spark-1.4.0-bin-hadoop2.6\python\pyspark\sql\readwriter.pyc in jdbc(self, url, table, mode, properties)
394 for k in properties:
395 jprop.setProperty(k, properties[k])
--> 396 self._jwrite.mode(mode).jdbc(url, table, jprop)
397
398

C:\Python27\lib\site-packages\py4j\java_gateway.pyc in __call__(self, *args)
536 answer = self.gateway_client.send_command(command)
537 return_value = get_return_value(answer, self.gateway_client,
--> 538 self.target_id, self.name)
539
540 for temp_arg in temp_args:

C:\Python27\lib\site-packages\py4j\protocol.pyc in get_return_value(answer, gateway_client, target_id, name)
302 raise Py4JError(
303 'An error occurred while calling {0}{1}{2}. Trace:\n{3}\n'.
--> 304 format(target_id, '.', name, value))
305 else:
306 raise Py4JError(

Py4JError: An error occurred while calling o49.mode. Trace:
py4j.Py4JException: Method mode([class java.util.HashMap]) does not exist
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:333)
at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:342)
at py4j.Gateway.invoke(Gateway.java:252)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:133)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:207)
at java.lang.Thread.run(Unknown Source)

问题似乎是 py4j 在将我的 connectionProperties 字典转换为 JVM 对象时找不到 Java java.util.HashMap 类。将我的 rt.jar(带路径)添加到 spark.driver.extraClassPath 并不能解决问题。从写入命令中删除字典可避免此错误,但当然写入失败是由于缺少驱动程序和身份验证。

编辑:错误的 o49.mode 部分会随着运行而变化。

最佳答案

Davies Liu 在 Spark 用户邮件列表中 found the problem . Scala 之间存在细微差别和 Python我错过的 API。您必须传入一个模式字符串(例如“overwrite”)作为 Python API 中的第三个参数,但 Scala API 则不行。如下更改语句可解决此问题:

df.write.jdbc("jdbc:sqlserver://serverURL", "dbName.SparkTestTable1", "overwrite", dict(driver="com.microsoft.sqlserver.jdbc.SQLServerDriver", user="userName", password="password"))

关于python - PySpark session 中缺少 java.util.HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31417653/

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