我正在尝试登录服务器并拉回数据以在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方式运行jars,它与错误无关。
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}
答案 0 :(得分:0)
您的pyspark似乎没有加载greenplum-spark_2.11-1.4.0.jar
建议: 检查您的pyspark命令是否将参数“ --jars”和路径传递给greenplum-sparkXXXX.jar。下面的示例使用“ GSC_JAR”环境变量,该变量指向目录为“ / code”的greenplum-spark_2.11-*。jar文件路径。因此,请对GSC_JAR env变量进行适当的更改。
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}"
谢谢, 孔 在Greenplum中使用Pyspark的示例: https://greenplum-spark-connector.readthedocs.io/en/latest/using-pyspark.html
答案 1 :(得分:0)
也许这会有所帮助。我使用pyspark从greenplum-spark连接器文档中试用了现有的scala示例。这与您的示例稍有不同(我未使用SQLContext),但有一点对我有用:
user @ spark-client $ pyspark --jars
gscPythonOptions = { “ url”:“ jdbc:postgresql://gpmaster.domain/tutorial”, “ user”:“ user2”, “ password”:“关键”, “ dbschema”:“ faa”, “ dbtable”:“ otp_c”, “ partitionColumn”:“ airlineid” }
gpdf = spark.read.format(“ greenplum”)。options(** gscPythonOptions).load()
之后这些命令对我有用:
gpdf.printSchema()
gpdf.count()
答案 2 :(得分:0)
请确认pyspark已加载greenplum-spark连接器jar
操作: 使用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')]
答案 3 :(得分:0)
感谢大家的帮助和建议。 pyspark连接器/加载器在除终端之外的任何接口中均不起作用。即Jupyter Notebook。