Apache spark 向spark数据帧添加新列

Apache spark 向spark数据帧添加新列,apache-spark,pyspark,pyspark-sql,Apache Spark,Pyspark,Pyspark Sql,我想向已注册为表的spark数据帧添加一列。此列需要具有自动递增的long df = spark.sql(query) df.createOrReplaceTempView("user_stories") df = spark.sql("ALTER TABLE user_stories ADD COLUMN rank int AUTO_INCREMENT") df.show(5) 这会引发以下错误 Py4JJavaError: An error occurred while calling o

我想向已注册为表的spark数据帧添加一列。此列需要具有自动递增的long

df = spark.sql(query)
df.createOrReplaceTempView("user_stories")
df = spark.sql("ALTER TABLE user_stories ADD COLUMN rank int AUTO_INCREMENT")
df.show(5)
这会引发以下错误

Py4JJavaError: An error occurred while calling o72.sql.
: org.apache.spark.sql.catalyst.parser.ParseException: 
no viable alternative at input 'ALTER TABLE user_stories ADD COLUMN'(line 1, pos 29)

== SQL ==
ALTER TABLE user_stories ADD COLUMN rank int AUTO_INCREMENT
-----------------------------^^^

我在这里遗漏了什么?

如果您想向DF添加新的增量列,可以通过以下方式进行操作

df.show()
+-------+
|   name|
+-------+
|gaurnag|
+-------+   
from pyspark.sql.functions import monotonically_increasing_id
new_df = df.withColumn("id", monotonically_increasing_id())
new_df.show()
+-------+---+
|   name| id|
+-------+---+
|gaurnag|  0|
+-------+---+

这个问题怎么会是重复的,我需要在新的专栏中有一个自动递增的值,我看不出你所引用的问题中有这个问题。spark中没有自动递增。你想干什么?