Find latest file pyspark

我们两清 提交于 2021-01-27 22:04:16

问题


So I've figured out how to find the latest file using python. Now I'm wondering if I can find the latest file using pyspark. Currently I specify a path but I'd like pyspark to get the latest modified file.

Current code looks like this:

df = sc.read.csv("Path://to/file", header=True, inderSchema=True)

Thanks in advance for your help.


回答1:


I copied the code to get the HDFS API to work with PySpark from this answer: Pyspark: get list of files/directories on HDFS path

URI           = sc._gateway.jvm.java.net.URI
Path          = sc._gateway.jvm.org.apache.hadoop.fs.Path
FileSystem    = sc._gateway.jvm.org.apache.hadoop.fs.s3.S3FileSystem
Configuration = sc._gateway.jvm.org.apache.hadoop.conf.Configuration

fs = # Create S3FileSystem object here

files = fs.listStatus(Path("Path://to/file"))

# You can also filter for directory here
file_status = [(file.getPath().toString(), file.getModificationTime()) for file in files]

file_status.sort(key = lambda tup: tup[1], reverse= True)

most_recently_updated = file_status[0][0]

spark.read.csv(most_recently_updated).option(...)


来源:https://stackoverflow.com/questions/50526076/find-latest-file-pyspark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!