![](/img/trans.png)
[英]Read files from multiple folders from ADLS gen2 storage via databricks and create single target file
[英]Read a file as byte string from a Databricks ADLS mount point
目标是从 ADLS 挂载点读取文件作为 Databricks 中的字节字符串。
首先,使用dbutils.fs.mounts()
确认具有以下内容:
... MountInfo(mountPoint='/mnt/ftd', source='abfss://ftd@omitted.dfs.core.windows.net/', encryptionType=''), ...
有问题的文件名为TruthTable.csv
,已使用以下命令确认其下落:
dbutils.fs.ls('/mnt/ftd/TruthTable.csv')
返回:
[FileInfo(path='dbfs:/mnt/ftd/TruthTable.csv', name='TruthTable.csv', size=156)]
为了确认可以读取文件,我们可以运行以下代码段。
filePath = '/mnt/ftd/TruthTable.csv'
spark.read.format('csv').option('header','true').load(filePath)
成功返回
DataFrame[p: string, q: string, r: string, s: string]
由于目标是能够将文件作为字节字符串读取,因此以下代码段应该是成功的,但事实并非如此。
filePath = '/mnt/ftd/TruthTable.csv'
with open(filePath, 'rb') as fin:
contents = fin.read()
print(contents)
执行以下代码段输出:
FileNotFoundError: [Errno 2] No such file or directory: '/mnt/ftd/TruthTable.csv'
Databricks 团队在以下链接中提供的文档 [https://docs.databricks.com/data/databricks-file-system.html#local-file-apis][https://docs.databricks.com/data /databricks-file-system.html#local-file-apis] 仅适用于/tmp/
文件夹中的文件,但是,要求是直接从挂载点读取文件。
请添加 dbfs 前缀:
filePath = '/dbfs/mnt/ftd/TruthTable.csv'
with open(filePath, 'rb') as fin:
contents = fin.read()
print(contents)
对于本机数据块 function(如 dbutils)dbfs 用作默认位置。 当您直接访问文件系统时,您需要添加默认挂载目录 /dbfs。 或者,您可以使用“dbfs:/mnt/ftd/TruthTable.csv”。 如果您使用免费的社区版,它根本无法工作,因为无法访问底层文件系统。 对于 Azure、Aws 和 Google 版本,它应该可以工作。
我能够通过将s3a://和存储桶前缀替换为相应的/dbfs/mnt/来读取文件。
s3a://s3-bucket/lake/output/dept/2022/09/16/20220916_1643_764250.csv /dbfs/mnt/output/dept/2022/09/16/20220916_1643_764250.csv
我用这个:
_path = _path.replace('s3a://s3-bucket/lake', '/dbfs/mnt')
希望能帮助到你。
-ed
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.