在Bluemix上从Spark as a Service Python笔记本访问Compose PostgreSQL数据库中的数据

2024-10-01 17:37:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在postgres数据库中有数据,我正试图通过ibmbluelmix上的Spark作为服务访问(使用python笔记本)。这是我的代码:

from pyspark.sql import SQLContext
sqlContext = SQLContext(sc)

df = sqlContext.load(source="jdbc",\
                 url="jdbc:postgresql://[publichost]:[port]/compose",\
                 dbtable="[tablename]")
df.take(2)

我得到的错误(在df=行中)是:

^{pr2}$

我能更新这个驱动程序吗?任何建议或一个有效的例子将不胜感激!在


Tags: 数据代码from数据库dfsql笔记本postgres
1条回答
网友
1楼 · 发布于 2024-10-01 17:37:12

这是因为默认情况下,spark服务实例中没有安装postgresql驱动程序。在

您需要先添加它才能使用它。在

Change the kernel to Scala from the menu to execute below statement, you only need to execute this once per spark instance and then subsequent use postgres driver irrespective of kernel type(Python,Scala,R), you can simply import it
In [1]:
%Addjar -f https://jdbc.postgresql.org/download/postgresql-9.4.1207.jre7.jar
Starting download from https://jdbc.postgresql.org/download/postgresql-9.4.1207.jre7.jar
Finished download of postgresql-9.4.1207.jre7.jar
In [5]:
#Now change the kernel back to Python
In [1]:
from pyspark.sql import SQLContext
sqlContext = SQLContext(sc)
In [3]:
#Ignore the Connection Error which is because of the invalid connection details
#Just simply change the publichost to your hostname and port number and databasename and
#tablename
In [4]:
df = sqlContext.load(source="jdbc",\
                 url="jdbc:postgresql://[publichost]:[port]/databasename",\
                 dbtable="[tablename]")

完整的可导入笔记本见下文 https://github.com/charles2588/bluemixsparknotebooks/raw/master/Python/python_postgres.ipynb

谢谢, 查尔斯。在

相关问题 更多 >

    热门问题