postgresql – 无法在pyspark shell中使用jdbc连接到postgres

前端之家收集整理的这篇文章主要介绍了postgresql – 无法在pyspark shell中使用jdbc连接到postgres前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的本地窗口上使用独立群集,并尝试使用以下代码从我们的服务器加载数据 –
from pyspark.sql import sqlContext
sqlContext = sqlContext(sc)
df = sqlContext.load(source="jdbc",url="jdbc:postgresql://host/dbname",dbtable="schema.tablename")@H_404_2@ 
 

我已将SPARK_CLASSPATH设置为 –

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

在执行sqlContext.load时,它会引发错误,提到“找不到适合jdbc:postgresql的驱动程序”.我试过搜索网页,但无法找到解决方案.

可能会有所帮助.

在我的环境中,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()@H_404_2@ 
 

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

原文链接:https://www.f2er.com/postgresql/191886.html

猜你在找的Postgre SQL相关文章