如何使用 Spark Session 列出 S3 存储桶中的文件? [英] How to list files in S3 bucket using Spark Session?

查看:60
本文介绍了如何使用 Spark Session 列出 S3 存储桶中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 SparkSession 对象列出给定 S3 路径(例如:s3://my-bucket/my-folder/*.extension)中的所有文件?

Is it possible to list all of the files in given S3 path (ex: s3://my-bucket/my-folder/*.extension) using a SparkSession object?

推荐答案

您可以使用 Hadoop API 来访问 S3 上的文件(Spark 也使用它):

You can use Hadoop API for accessing files on S3 (Spark uses it as well):

import java.net.URI
import org.apache.hadoop.fs.FileSystem
import org.apache.hadoop.fs.Path
import org.apache.hadoop.conf.Configuration

val path = "s3://somebucket/somefolder"
val fileSystem = FileSystem.get(URI.create(path), new Configuration())
val it = fileSystem.listFiles(new Path(path), true)
while (it.hasNext()) {
  ...
}

这篇关于如何使用 Spark Session 列出 S3 存储桶中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆