Ele*_*les 4 apache-spark pyspark
所以我想出了如何使用 python 查找最新文件。现在我想知道是否可以使用 pyspark 找到最新的文件。目前我指定了一个路径,但我希望 pyspark 获取最新修改的文件。
当前代码如下所示:
df = sc.read.csv("Path://to/file", header=True, inderSchema=True)
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助。
我从这个答案中复制了代码以使 HDFS API 与 PySpark 一起使用: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(...)
Run Code Online (Sandbox Code Playgroud)