Pyspark 引用 table 使用 sql 创建
Pyspark refer to table created using sql
当我在 Spark 中使用 SQL 创建一个 table 时,例如:
sql('CREATE TABLE example SELECT a, b FROM c')
如何将 table 拉入 python 命名空间(我想不出更好的术语)以便更新它?假设我想像这样替换 table 中的 NaN
值:
import pyspark.sql.functions as F
table = sql('SELECT * FROM example')
for column in columns:
table = table.withColumn(column,F.when(F.isnan(F.col(column)),F.col(column)).otherwise(None))
此操作是否更新使用 SQL 创建的原始 example
table?如果我 运行 sql('SELECT * FROM example')show()
我会看到更新后的结果吗?当原来的CREATE TABLE example ...
SQL运行时,example
是否自动添加到python命名空间?
sql
函数returns一个新的DataFrame
,所以table没有修改。如果要将 DataFrame
的内容写入 spark 中创建的 table,请这样做:
table.write.mode("append").saveAsTable("example")
但您所做的实际上是在更改 table 的模式,在那种情况下
table.createOrReplaceTempView("mytempTable")
sql("create table example2 as select * from mytempTable");
当我在 Spark 中使用 SQL 创建一个 table 时,例如:
sql('CREATE TABLE example SELECT a, b FROM c')
如何将 table 拉入 python 命名空间(我想不出更好的术语)以便更新它?假设我想像这样替换 table 中的 NaN
值:
import pyspark.sql.functions as F
table = sql('SELECT * FROM example')
for column in columns:
table = table.withColumn(column,F.when(F.isnan(F.col(column)),F.col(column)).otherwise(None))
此操作是否更新使用 SQL 创建的原始 example
table?如果我 运行 sql('SELECT * FROM example')show()
我会看到更新后的结果吗?当原来的CREATE TABLE example ...
SQL运行时,example
是否自动添加到python命名空间?
sql
函数returns一个新的DataFrame
,所以table没有修改。如果要将 DataFrame
的内容写入 spark 中创建的 table,请这样做:
table.write.mode("append").saveAsTable("example")
但您所做的实际上是在更改 table 的模式,在那种情况下
table.createOrReplaceTempView("mytempTable")
sql("create table example2 as select * from mytempTable");