8

我在本地窗口上使用独立集群,并尝试使用以下代码从我们的一台服务器加载数据 -

from pyspark.sql import SQLContext
sqlContext = SQLContext(sc)
df = sqlContext.load(source="jdbc", url="jdbc:postgresql://host/dbname", dbtable="schema.tablename")

我已将 SPARK_CLASSPATH 设置为 -

os.environ['SPARK_CLASSPATH'] = "C:\Users\ACERNEW3\Desktop\Spark\spark-1.3.0-bin-hadoop2.4\postgresql-9.2-1002.jdbc3.jar"

执行 sqlContext.load 时,它会抛出错误,提示“找不到适合 jdbc:postgresql 的驱动程序”。我已经尝试搜索网络,但无法找到解决方案。

4

2 回答 2

5

可能会有所帮助。

在我的环境中,SPARK_CLASSPATH 包含 postgresql 连接器的路径

from pyspark import SparkContext, SparkConf
from pyspark.sql import DataFrameReader, SQLContext
import os

sparkClassPath = os.getenv('SPARK_CLASSPATH', '/path/to/connector/postgresql-42.1.4.jar')

# Populate configuration
conf = SparkConf()
conf.setAppName('application')
conf.set('spark.jars', 'file:%s' % sparkClassPath)
conf.set('spark.executor.extraClassPath', sparkClassPath)
conf.set('spark.driver.extraClassPath', sparkClassPath)
# Uncomment line below and modify ip address if you need to use cluster on different IP address
#conf.set('spark.master', 'spark://127.0.0.1:7077')

sc = SparkContext(conf=conf)
sqlContext = SQLContext(sc)

url = 'postgresql://127.0.0.1:5432/postgresql'
properties = {'user':'username', 'password':'password'}

df = DataFrameReader(sqlContext).jdbc(url='jdbc:%s' % url, table='tablename', properties=properties)

df.printSchema()
df.show()

这段代码允许在需要的地方使用 pyspark。例如,我在 Django 项目中使用过它。

于 2017-09-22T08:42:25.830 回答
3

我在使用 mysql 时遇到了同样的问题,并且无法让它与 SPARK_CLASSPATH 方法一起使用。但是我确实让它与额外的命令行参数一起工作,请参阅这个问题的答案

为避免必须单击以使其正常工作,您必须执行以下操作:

pyspark --conf spark.executor.extraClassPath=<jdbc.jar> --driver-class-path <jdbc.jar> --jars <jdbc.jar> --master <master-URL>
于 2015-06-20T00:02:21.133 回答