I want to check whether a file exists in an s3 path and then read it as a spark dataframe. The problem is I do not know the exact path of the file, so I have to use wild characters. For example, my path looks something like this:
path = 's3a://dir1/dir2/someHashString-2021-09-29-14-52-randomStuff.gz'
I have no way of knowing what 'someHashString' or 'randomStuff' is, so, I probably have to use wild characters like this
path = 's3a://dir1/dir2/*-2021-09-29-14-52-*'
I am trying the first solution mentioned here like this :
def path_exists(path):
# spark is a SparkSession
sc = spark.sparkContext
fs = sc._jvm.org.apache.hadoop.fs.FileSystem.get(
sc._jvm.java.net.URI.create("s3a://" + path.split("/")[2]),
sc._jsc.hadoopConfiguration(),
)
return fs.exists(sc._jvm.org.apache.hadoop.fs.Path(path))
path_exists(path='s3a://dir1/dir2/*-2021-09-29-14-52-*')
But it returns False even such a path does exist. How to check if this file path exists?