运行 spark.sql 在 jupyter 中查询

Running spark.sql query in jupyter

我正在使用

启动 jupyter notebook
pyspark --driver-class-path /home/statspy/postgresql-42.2.23.jar --jars /home/statspy/postgresql-42.2.23.jar

我在 jupyter 中运行宁此:

import os
jardrv = '/home/statspy/postgresql-42.2.23.jar'
from pyspark.sql import SparkSession
spark = SparkSession.builder.config('spark.driver.extraClassPath', jardrv).getOrCreate()
url = 'jdbc:postgresql://127.0.0.1/dbname'
properties = {'user':'postgres', 'password':'secret'}
df = spark.read.jdbc(url=url, table='tbname', properties=properties)

那我可以运行:

df.printSchema()

我得到了架构。

但是我想 运行 这样的查询:

spark.sql("""select * from tbname""")

我收到一条错误消息 table or view tbname not found

我需要将使用 spark.sql 而不是 df 的查询更改为 运行 什么?

使用spark.sql前需要将dataframe保存为tempview。

df.createOrReplaceTempView("tbname")