PySpark - Read Data from PostgreSQL

news/2025/11/22 21:49:23/文章来源:https://www.cnblogs.com/zhangzhihui/p/19258676

 

jdbc_url = "jdbc:postgresql://localhost:5432/postgres"
properties = {"user": "postgres","password": "postgrespwd","driver": "org.postgresql.Driver"
}table_name = "player_seasons"df = spark.read.jdbc(url=jdbc_url, table=table_name, properties=properties)df.show()

When I ran above code, I got below error:

Py4JJavaError: An error occurred while calling o28.jdbc. : java.lang.ClassNotFoundException: org.postgresql.Driver

 

You need the PostgreSQL JDBC JAR.


Step 1: Download the JDBC driver

Go to the PostgreSQL site, or directly:

wget https://jdbc.postgresql.org/download/postgresql-42.7.2.jar

You can also use any recent 42.x.x version.


Step 2: Make the JAR available to Spark

There are three common ways:

Option 1: Copy to Spark jars/ directory

cp postgresql-42.7.2.jar $SPARK_HOME/jars/

Spark automatically loads all JARs in that folder.


Option 2: Pass the JAR when creating SparkSession

df = (SparkSession.builder.appName("PostgreSQL Test").config("spark.jars", "/home/frank/postgresql-42.7.2.jar").getOrCreate()
)

Now your spark.read.jdbc(...) will find the driver.


Option 3: Use Maven coordinates (auto-download)

spark = (SparkSession.builder.appName("PostgreSQL Test").config("spark.jars.packages", "org.postgresql:postgresql:42.7.2").getOrCreate()
)

Spark will automatically download the JAR from Maven.


Step 3: Retry reading the table

jdbc_url = "jdbc:postgresql://localhost:5432/postgres"
properties = {"user": "postgres","password": "postgrespwd","driver": "org.postgresql.Driver"
}table_name = "player_seasons"df = spark.read.jdbc(url=jdbc_url, table=table_name, properties=properties)df.show()

Now it should work. ✅


⚡ Note

    • Make sure your PostgreSQL is accessible from WSL.
      If PostgreSQL is running on Windows, use the WSL IP of Windows host, e.g., jdbc:postgresql://<Windows-IP>:5432/postgres
      localhost inside WSL points to WSL itself, not Windows.

 

jdbc_url = "jdbc:postgresql://172.26.64.1:5432/postgres"
properties = {"user": "postgres","password": "postgrespwd","driver": "org.postgresql.Driver"
}table_name = "player_seasons"df = spark.read.jdbc(url=jdbc_url, table=table_name, properties=properties)df.show()

 

I chose option 1, do I need to restart Spark after copying the jar file? I have changed 'localhost' to Windows-IP, the code still got the same error.
 
ChatGPT said:

Yes — you must restart Spark after copying the JAR into $SPARK_HOME/jars/.

Here’s why:

  • When you start Spark, it loads all JARs in $SPARK_HOME/jars/ into the JVM classpath.

  • If you copy a new JAR after Spark is already running, the running JVM cannot see it.

  • This is exactly why your code still shows:

java.lang.ClassNotFoundException: org.postgresql.Driver

✅ Correct steps after copying the JAR

  1. Stop any running Spark master or workers:

  1. Start Spark master & worker again:

  1. Restart your Jupyter kernel (or Python process):

  1. Then run your JDBC code again.

 
[Stage 0:>                                                          (0 + 1) / 1]
+-----------+---+------+------+-------+-------+----------+-----------+------------+---+---+---+---+------+--------+--------+-------+------+-------+------+
|player_name|age|height|weight|college|country|draft_year|draft_round|draft_number| gp|pts|reb|ast|netrtg|oreb_pct|dreb_pct|usg_pct|ts_pct|ast_pct|season|
+-----------+---+------+------+-------+-------+----------+-----------+------------+---+---+---+---+------+--------+--------+-------+------+-------+------+
+-----------+---+------+------+-------+-------+----------+-----------+------------+---+---+---+---+------+--------+--------+-------+------+-------+------+

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/973444.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

单部电梯调度总结

第一次作业: 作业内容: 基础配置:支持自定义最小(≥1)、最大楼层(> 最小楼层),默认停靠 1 层; 请求处理:区分电梯内(纯楼层)、电梯外(楼层 + 方向)请求,支持上行 / 下行方向,过滤无效请求; 调度规…

MySQL的 索引类型 normal、 unique 和 索引方法 btree 、 hash

MySQL的 索引类型 normal、 unique 和 索引方法 btree 、 hash 一、索引类型:Normal(普通索引) vs Unique(唯一索引) 核心差异是 是否对字段值施加「唯一性约束」,其他特性均围绕这一点展开:对比维度Normal(普…

精读 GitHub - servo 浏览器(一)

精读 GitHub - servo 浏览器(一)一、简介项目地址:https://github.com/servo/servo这一期的精读 GitHub 系列是 servo 浏览器,我们将从源码的角度去拆解 servo 浏览器,逐步厘清整个浏览器的工作原理。 servo 是一…

50040_基于微信小程序的项目管理系统

1.项目包含 项目源码、项目文档、数据库脚本、软件工具等资料; 带你从零开始部署运行本套系统。 2.项目介绍 在信息化浪潮的推动下,以互联网和信息技术为代表的现代科技正深刻重塑人类社会的发展格局,这种变革使得基…

hyper v linux

你提到的 "Hyper-V Linux" 可能是指在 Linux 系统上使用 Hyper-V 技术进行虚拟化。Hyper-V 是 Microsoft 提供的虚拟化平台,主要用于 Windows 操作系统。然而,Linux 系统本身并不直接支持 Hyper-V 虚拟化技…

hyper for linux

“Hyper for Linux” 是一个基于 Electron 的轻量级跨平台桌面应用程序,主要用于在 Linux 系统上运行 Hyper(一个基于 Node.js 的高性能 Web 服务器)。Hyper 是一个高性能的 Web 服务器,常用于开发和测试 Web 应用…

https linux

您提到的“https linux”可能是指与 Linux 系统 相关的 HTTPS 服务 或 HTTPS 安全协议 的使用。以下是一些与 Linux 系统和 HTTPS 相关的常见问题和解决方案,供您参考:1. 如何在 Linux 上安装 HTTPS 服务?常用 HTTP…

页面

在word里面,逻辑单位是1页文档是A4尺寸单面打印的话,3页word,就需要3张A4纸双面打印的话,4页word,就需要2张A4纸,正反文档是A4尺寸需要打印到A3纸4页A4 word, 双面打印 1张A3纸的2个面2页A4 word ,单面打印 …

Trick——数据结构

Part1 你真的以为树状数组只能止步于区修区查了吗? 实际上有这样一种特殊的最值:前缀最值查询。 代码: struct BIT{int tr[N];inline int lowbit(int x){ return (x&(-x)); }void add(int x,int val){for(int i…

锂矿及其投资机会

锂矿公司 锂辉石矿 天齐锂业 矿产资源介绍:控股澳大利亚泰利森公司,手握全球最大硬岩锂矿 —— 格林布什锂辉石矿,权益储量达 1614 万吨 LCE,同时还布局了四川措拉锂辉石矿,资源自给率达 100%,其电池级氢氧化锂技…

电梯调度迭代编程作业复盘:从问题剖析到能力进阶

一、对三次电梯调度题目集的整体认知 (一)知识点覆盖与考察重点 三次电梯调度题目集聚焦面向对象编程核心知识点与工程化开发能力,核心考察内容可归纳为三类: 设计原则:核心是单一职责原则(SRP)。从第一次的 “…

MORL | Envelope Q-Learning:有收敛性保证的 MORL 算法

EQL 将单目标的 bellman 算子拓展到多目标 RL 上,并复刻了 value iteration 的收敛性保证。论文标题:A Generalized Algorithm for Multi-Objective Reinforcement Learning and Policy Adaptatio。 NeurIPS 2019。也…

获深圳人才集团认可!「张张讲AI」AI资讯公众号解读AI动态,讲师提供定制化咨询

获深圳人才集团认可!「张张讲AI」AI资讯公众号解读AI动态,讲师提供定制化咨询在人工智能快速发展的时代,各行各业都在努力融入这一新兴技术,以谋求更好的发展。而张张讲AI作为人工智能应用培训领域的重要力量,凭借…

多重背包 二进制拆分这个向左移动以为是2也是被我写出来了

include <bits/stdc++.h> using namespace std; int dp[105];int v[2000];int w[2000]; int main() { int c;cin>>c; while(c--) { int n,m;cin>>n>>m; memset(v,0,sizeof(v)); memset(w,0,si…

NOIP2025

NOIP2025前情提要:CSP-S 2025 \(100+80+50+24\)。。

why exams are bad

"i know many things, but professors just check the things I dont know!" it will hurt buddys confidence, let one think hes a loser. how much people understand Hebrew, Chinese or Japanese? how…

若依框架源码—2

若依框架——每一个模块的解读 模块分类模块图首先我们可以看到一共有六个模块 通过注释分析,我们可以得出,admin 模块 后端管理模块,项目的启动入口其中还包含了项目的配置文件common 模块 后端通用模块,通用的工…

http linux

您提到的“http linux”可能是指与 HTTP 相关的 Linux 操作系统相关的问题,或者是想了解在 Linux 系统中如何处理 HTTP 请求或相关服务。请提供更多具体问题或需求,以便我为您提供准确的 Linux 命令或脚本建议。例如…

html空间能用于表单吗

HTML空间(HTML Space)可以用于表单。HTML空间主要用于在HTML文档中插入不可见的特殊字符,这些字符在浏览器中不会显示,但可以在源代码中使用。在表单提交时,HTML空间中的字符也会被一起提交。因此,如果需要在表单…

html空间能用于布局吗

HTML空间本身并不直接用于布局,但HTML元素和CSS样式可以用于创建和控制网页的布局。HTML提供了页面结构,而CSS则负责样式和布局。以下是HTML和CSS在网页布局中的具体应用: HTML用于布局表格布局:虽然不推荐用于布局…