Databricks File System - DBFS

Databricks File System (DBFS) is a distributed file system installed on Databricks Runtime clusters.

DBFS is a layer over Azure Blob Storage and is available in both Python and Scala. Files in DBFS persist to Azure Blob Storage, so you won’t lose data even after you terminate a cluster.

Note

In a Spark cluster you can access DBFS using Databricks Utilities. On your local computer you can access DBFS using the CLI.

Access DBFS with the CLI

The DBFS command-line interface leverages the DBFS API to expose an easy to use command-line interface to DBFS. Using this client, interacting with DBFS is as easy as running:

# List files in DBFS
dbfs ls
# Put local file ./apple.txt to dbfs:/apple.txt
dbfs cp ./apple.txt dbfs:/apple.txt
# Get dbfs:/apple.txt and save to local file ./apple.txt
dbfs cp dbfs:/apple.txt ./apple.txt
# Recursively put local dir ./banana to dbfs:/banana
dbfs cp -r ./banana dbfs:/banana

For more information about the DBFS command-line interface, see Databricks CLI.

Access DBFS with dbutils

This section has several examples of how to write files to and read files from DBFS using dbutils.

Tip

To access the help menu for DBFS, use the dbutils.fs.help() command.

  • Write file to and read files from DBFS as if it were a local filesystem.

    dbutils.fs.mkdirs("/foobar/")
    
    dbutils.fs.put("/foobar/baz.txt", "Hello, World!")
    
    dbutils.fs.head("/foobar/baz.txt")
    
    dbutils.fs.rm("/foobar/baz.txt")
    
  • Use dbfs:/ to access a DBFS path.

    display(dbutils.fs.ls("dbfs:/foobar"))
    
  • Use file:/ to access the local disk.

    dbutils.fs.ls("file:/foobar")
    
  • Filesystem cells provide a shorthand for accessing the dbutils filesystem module. Most dbutils.fs commands are available via the %fs magic command as well.

    %fs rm -r foobar
    

Access DBFS using the Spark API

#python
sc.parallelize(range(0, 100)).saveAsTextFile("/tmp/foo.txt")
//python
sc.parallelize(0 until 100).saveAsTextFile("/tmp/bar.txt")

Access DBFS using local file APIs

You can use local file APIs to read and write to DBFS paths. Azure Databricks configures each node with a fuse mount that allows processes to read and write to the underlying distributed storage layer.

#python
# write a file to DBFS using python i/o apis
with open("/dbfs/tmp/test_dbfs.txt", 'w') as f:
  f.write("Apache Spark is awesome!\n")
  f.write("End of example!")

# read the file
with open("/dbfs/tmp/test_dbfs.txt", "r") as f_read:
  for line in f_read:
    print line
// scala
import scala.io.Source

val filename = "/dbfs/tmp/test_dbfs.txt"
for (line <- Source.fromFile(filename).getLines()) {
  println(line)
}

Warning

  • Local file I/O APIs only support files less than 2GB in size. You might see corrupted files if you use local file I/O APIs to read or write files larger than 2GB. Access it using the DBFS CLI, use dbutils.fs, or use Hadoop Filesystem APIs to access large files instead.

  • If you write a file using the local file I/O APIs and then immediately try to access it using the DBFS CLI, dbutils.fs, or the Hadoop Filesystem APIs, you might encounter a FileNotFoundException, a file of size 0, or stale file contents. That is expected because the OS caches writes by default. To force those writes to be flushed to persistent storage (in our case DBFS), use the standard Unix system call sync.

    // scala
    import scala.sys.process._
    
    // Write a file using the local file I/O API (over the fuse mount).
    dbutils.fs.put("file:/dbfs/tmp/test", "test-contents")
    
    // Unless you call this, the code below might not see the file or its latest contents.
    "sync /dbfs/tmp/test" !
    
    // Read the file using "dbfs:/" instead of the fuse mount.
    dbutils.fs.head("dbfs:/tmp/test")
    

Mount Azure blob storage and Azure Data Lake stores

Mounting Azure blob storage and Data Lake stores directly to DBFS allows you to access files as if they were on the local file system.

For information on how to mount and unmount Azure blob storage and Azure Data Lake stores, see Mount Azure Blob storage containers with DBFS and Mount Azure Data Lake Stores with DBFS.