尝试使用 pyspark 访问 greenplum 表时出错
Posted
技术标签:
【中文标题】尝试使用 pyspark 访问 greenplum 表时出错【英文标题】:Errors when trying to access greenplum table using pyspark 【发布时间】:2018-07-16 19:28:16 【问题描述】:我正在尝试登录服务器并拉回数据以在 python 中本地构建模型。我正在使用 pyspark 库 - 但我不断收到同样的错误。这是我正在运行的代码,没有错误,所以我知道我已经正确安装了 .jar 文件:
import pyspark
print(pyspark.__file__)
from pyspark.sql import DataFrameReader
from pyspark.sql import SQLContext
from pyspark import SparkContext
sc = SparkContext(appName="PythonStreamingQueueStream")
sqlContext = SQLContext(sc)
sqlContext.read.format('io.pivotal.greenplum.spark.GreenplumRelationProvider')
sc.stop()
它提供此作为指示正确安装 jar 的输出:
<pyspark.sql.readwriter.DataFrameReader at 0x10819cd10>
我可以在 scala 中毫无错误地运行它,它会拉回我请求的表:
val gscReadOptionMap = Map(
"url" -> "jdbc:postgresql://12.3.45.678:9101/code",
"user" -> "my_name",
"password" -> "password",
"dbschema" -> "schema",
"dbtable" -> "table",
"partitionColumn" -> "max"
)
val gpdf = spark.read.format("greenplum").options(gscReadOptionMap).load()
它提供以下输出:
gpdf: org.apache.spark.sql.DataFrame = [output_code: string, input_code: string ... 11 more fields]
但是当我尝试将数据库登录放入 python 时,我不断收到相同的错误:
import pyspark
print(pyspark.__file__)
from pyspark.sql import DataFrameReader
from pyspark.sql import SQLContext
from pyspark import SparkContext
sc = SparkContext(appName="PythonStreamingQueueStream")
sqlContext = SQLContext(sc)
sqlContext.read.format('io.pivotal.greenplum.spark.GreenplumRelationProvider')
source_df = sqlContext.read.format('io.pivotal.greenplum.spark.GreenplumRelationProvider').options(
url='jdbc:postgresql://12.3.45.678:9101/code',
dbschema='schema',
dbtable = 'table',
user='my_name',
password='password',
driver='org.postgresql.Driver',
partitionColumn='max').load()
sc.stop()
这是错误,在通常的 python 方式中,错误是超长的:
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
File "/usr/local/Cellar/apache-spark/2.2.0/libexec/python/pyspark/sql/readwriter.py", line 165, in load
return self._df(self._jreader.load())
File "/usr/local/Cellar/apache-spark/2.2.0/libexec/python/lib/py4j-0.10.4-src.zip/py4j/java_gateway.py", line 1133, in __call__
File "/usr/local/Cellar/apache-spark/2.2.0/libexec/python/pyspark/sql/utils.py", line 63, in deco
return f(*a, **kw)
File "/usr/local/Cellar/apache-spark/2.2.0/libexec/python/lib/py4j-0.10.4-src.zip/py4j/protocol.py", line 319, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o111.load.
: java.lang.ClassNotFoundException: Failed to find data source: io.pivotal.greenplum.spark.GreenplumRelationProvider. Please find packages at http://spark.apache.org/third-party-projects.html
at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:549)
at org.apache.spark.sql.execution.datasources.DataSource.providingClass$lzycompute(DataSource.scala:86)
at org.apache.spark.sql.execution.datasources.DataSource.providingClass(DataSource.scala:86)
at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:301)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:178)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:146)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
at py4j.Gateway.invoke(Gateway.java:280)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:214)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: io.pivotal.greenplum.spark.GreenplumRelationProvider.DefaultSource
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$21$$anonfun$apply$12.apply(DataSource.scala:533)
at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$21$$anonfun$apply$12.apply(DataSource.scala:533)
at scala.util.Try$.apply(Try.scala:192)
at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$21.apply(DataSource.scala:533)
at org.apache.spark.sql.execution.datasources.DataSource$$anonfun$21.apply(DataSource.scala:533)
at scala.util.Try.orElse(Try.scala:84)
at org.apache.spark.sql.execution.datasources.DataSource$.lookupDataSource(DataSource.scala:533)
... 16 more
我有 'greenplum-spark_2.11-1.4.0.jar' 和 'postgresql-42.2.4.jre7.jar' 被引入...我不确定我做错了什么?
编辑: 我以 python 2 方式运行罐子,它与错误无关。
import os
os.getcwd()
os.environ['PYSPARK_SUBMIT_ARGS'] = '--master local[*] pyspark-shell --jars /Users/greenplum-spark_2.11-1.4.0.jar, /Users/postgresql-42.2.4.jre7.jar'
我也跑过:
%%bash
export GSC_JAR=/Users/greenplum-spark_2.11-1.4.0.jar export POSTGRES_JAR=/Users/postgresql-42.2.4.jre7.jar
spark-shell --jars $GSC_JAR, $POSTGRES_JAR
【问题讨论】:
【参考方案1】:您的 pyspark 似乎没有加载 greenplum-spark_2.11-1.4.0.jar
建议: 检查您的 pyspark 命令是否将参数“--jars”与 greenplum-sparkXXXX.jar 的路径一起传递。下面的示例使用“GSC_JAR”环境变量,该变量指向目录“/code”的greenplum-spark_2.11-*.jar 文件路径。所以请对 GSC_JAR 环境变量进行适当的更改。
root@master:/usr/spark-2.1.0#GSC_JAR=$(ls /code/greenplum-spark_2.11-*.jar)
root@master:/usr/spark-2.1.0#pyspark --jars "$GSC_JAR"
谢谢, 孔 将 Pyspark 与 Greenplum 一起使用的示例: https://greenplum-spark-connector.readthedocs.io/en/latest/using-pyspark.html
【讨论】:
我以 python 2 方式运行 jars,它与错误无关: import os os.getcwd() os.environ['PYSPARK_SUBMIT_ARGS'] = '--master local[*] pyspark-shell --jars /Users/sarahewing/Documents/OnBoarding/python/greenplum-spark_2.11-1.4.0.jar,/Users/sarahewing/Documents/OnBoarding/python/postgresql-42.2.4.jre7.jar'我也运行过:%%bash export GSC_JAR=/Users/sarahewing/Documents/OnBoarding/python/greenplum-spark_2.11-1.4.0.jar export POSTGRES_JAR=/Users/sarahewing/Documents/OnBoarding/python/postgresql- 42.2.4.jre7.jar spark-shell --jars $GSC_JAR, $POSTGRES_JAR【参考方案2】:也许这会有所帮助。我使用 pyspark 从 greenplum-spark 连接器文档中尝试了现有的 scala 示例。这与您的示例略有不同(我没有使用 SQLContext),但在某一时刻,以下内容对我有用:
user@spark-client$ pyspark --jars
gscPythonOptions = "url": "jdbc:postgresql://gpmaster.domain/tutorial", “用户”:“用户 2”, “密码”:“关键”, "dbschema": "faa", “dbtable”:“otp_c”, "partitionColumn": "airlineid"
gpdf = spark.read.format("greenplum").options(**gscPythonOptions).load()
之后这些命令对我有用:
gpdf.printSchema()
gpdf.count()
【讨论】:
【参考方案3】:请验证 greenplum-spark 连接器 jar 是否由 pyspark 加载
行动: 使用 sc.getConf().getAll() 函数来验证 spark.repl.local.jars 是否引用了适当的 jar。
>>> sc.getConf().getAll()
[('spark.app.id', 'app-20180718183929-0000'), ('spark.jars',
'file:///code/usecase1/data/greenplum-spark_2.11-1.4.0.jar'),
('spark.master', 'spark://master:7077'), ('spark.rdd.compress',
'True'), ('spark.driver.host', 'master'),
('spark.serializer.objectStreamReset', '100'),
('spark.repl.local.jars', 'file:///code/usecase1/data/greenplum-
spark_2.11-1.4.0.jar'), ('spark.driver.port', '38611'),
('spark.executor.id', 'driver'), ('spark.submit.deployMode',
'client'), ('spark.app.name', 'PySparkShell'),
('spark.ui.showConsoleProgress', 'true')]
【讨论】:
【参考方案4】:感谢大家的帮助和建议。 pyspark 连接器/加载器在终端之外的任何接口中都不起作用。即 Jupyter Notebook。
【讨论】:
以上是关于尝试使用 pyspark 访问 greenplum 表时出错的主要内容,如果未能解决你的问题,请参考以下文章