pysparkDataframe从s3加载,保留行顺序

cunj1qz1  于 2021-07-12  发布在  Spark
关注(0)|答案(0)|浏览(124)

我想使用pyspark加载s3csv文件,并在加载的Dataframe中保留与s3文件相同的行顺序。请分享向Dataframe添加索引的方法,以维护s3的行顺序。
我尝试过这种方法,但是像s3这样的精确行顺序有时会得到维护,有时则不会(可能是由于默认分区)。注意:我不想订购任何可乐。
前任:

df=spark.read.option("header", True).option("delimiter", "|").option("escape", "").csv("s3path")
df.createOrReplaceTempView("df")

df_rw=spark.sql("select col1,col2,row_number() over (order by (SELECT NULL)) as rownum  from df")
df_rw.createOrReplaceTempView("df_rw")

前任:

s3 file order
cont|name|age
ind|kohli|30
ind|rohit|30
ind|rishabh|26

预期订单:

cont name age rownum
ind kohli 30 1
ind rohit 30 2
ind rishabh 26 3

有时得到命令:

cont name age rownum
ind rishabh 26 1
ind kohli 30 2
ind rohit 30 3

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题