问题,同时试图阅读一个文本文件在databricks使用地文件API的,而不是火花API

0

的问题

我想读一小txt文件,该文件是加入作为表的默认数据库上Databricks. 在试图读取文件的通过地方文件API,我得到一个 FileNotFoundError但我能读相同的文件作为 火花RDD 使用 SparkContext.

请找到代码如下:

with open("/FileStore/tables/boringwords.txt", "r") as f_read:
  for line in f_read:
    print(line)

这给了我错误:

FileNotFoundError                         Traceback (most recent call last)
<command-2618449717515592> in <module>
----> 1 with open("dbfs:/FileStore/tables/boringwords.txt", "r") as f_read:
      2   for line in f_read:
      3     print(line)

FileNotFoundError: [Errno 2] No such file or directory: 'dbfs:/FileStore/tables/boringwords.txt'

在那里,我有没有问题,阅读该文件,使用 SparkContext:

boring_words = sc.textFile("/FileStore/tables/boringwords.txt")
set(i.strip() for i in boring_words.collect())

并且正如预期的,我得到的结果对于上述块代码:

Out[4]: {'mad',
 'mobile',
 'filename',
 'circle',
 'cookies',
 'immigration',
 'anticipated',
 'editorials',
 'review'}

我还指 出文件 来了解当地的文件API的限制,但没有导致对这个问题。 任何帮助,将不胜感激。 谢谢!

apache-spark databricks pyspark sparkapi
2021-11-24 06:16:55
3
0

问题是,你正在使用的 open 功能工作只有当地文件,并且不知道任何有关出,或其他文件的系统。 得到这个工作,你需要使用 出当地的文件API 和追加的 /dbfs 前缀文件路径: /dbfs/FileStore/....:

with open("/dbfs/FileStore/tables/boringwords.txt", "r") as f_read:
  for line in f_read:
    print(line)
2021-11-24 07:56:14
0

或者你可以简单地使用内csv方法:

df = spark.read.csv("dbfs:/FileStore/tables/boringwords.txt")
2021-11-24 08:51:27
0

或者我们可以使用 dbutils

files = dbutils.fs.ls('/FileStore/tables/')
li = []
for fi in files: 
  print(fi.path)

例,

enter image description here

2021-11-24 18:26:17

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................