Databricks中的DBFS(Databricks File System)和对象存储(Object Storage)

What's DBFS and ObjectStorage

在Databricks中,DBFS(Databricks File System)和对象存储(如Amazon S3、Azure Blob Storage等)是两种主要的数据存储选项。它们在数据存储和访问方面各有特点:

  1. DBFS Storage(Databricks文件系统)

    • DBFS是一个分布式文件系统,专为Databricks平台设计,提供了一个高可用性和高吞吐量的存储解决方案。
    • DBFS与Databricks集群紧密集成,可以提供快速的数据访问速度,特别适合频繁访问和处理的数据。
    • DBFS存储可以自动扩展,无需用户管理,并且与Databricks的共享数据访问和协作功能无缝集成。
  2. Object Storage(对象存储)

    • 对象存储是一种高度可扩展的存储解决方案,通常由云服务提供商提供,如Amazon S3、Azure Blob Storage等。
    • 对象存储适合存储大量数据,特别是不经常访问的数据,以及需要跨多个地理位置存储的数据。
    • Databricks可以连接到这些对象存储服务,并使用它们来存储和访问数据。

在Databricks中使用路径(path)和from参数的上下文:

  • Path(路径):在Databricks中,路径用于指定数据存储的位置。无论是DBFS还是对象存储,路径都是用来定位文件或目录的字符串。例如,在读取数据时,你需要提供一个路径来告诉Databricks数据的位置。

  • From(来自):在Databricks的读取操作中,from参数通常用于指定数据源的类型或位置。例如,在读取数据时,你可能需要指定数据是来自DBFS还是来自连接的对象存储。

  • 读取DBFS上的文件:

    df = spark.read.format("csv").option("header", "true").load("/dbfs/mnt/my-data.csv")

  • 从连接的对象存储服务读取数据:

    df = spark.read.format("csv").option("header", "true").load("s3a://my-bucket/my-data.csv")

  • 使用from参数(在某些情况下,如使用Databricks的Delta Lake功能):

    df = spark.read.format("delta").load("dbfs:/data/delta-table")

在实际使用中,你需要根据你的数据存储选项和Databricks配置来确定正确的路径和参数。

How to specify the DBFS path 

When working with Databricks you will sometimes have to access the Databricks File System (DBFS).

Accessing files on DBFS is done with standard filesystem commands, however the syntax varies depending on the language or tool used.

For example, take the following DBFS path:

dbfs:/mnt/test_folder/test_folder1/

Apache Spark

Under Spark, you should specify the full path inside the Spark read command.

spark.read.parquet(“dbfs:/mnt/test_folder/test_folder1/file.parquet”)

DBUtils

When you are using DBUtils, the full DBFS path should be used, just like it is in Spark commands. The language specific formatting around the DBFS path differs depending on the language used.

Bash

%fs
ls dbfs:/mnt/test_folder/test_folder1/

Python

%python

dbutils.fs.ls(‘dbfs:/mnt/test_folder/test_folder1/’)

Scala

%scala

dbutils.fs.ls(“dbfs:/mnt/test_folder/test_folder1/”)

Note

Specifying dbfs: is not required when using DBUtils or Spark commands. The path dbfs:/mnt/test_folder/test_folder1/ is equivalent to /mnt/test_folder/test_folder1/.

Shell commands

Shell commands do not recognize the DFBS path. Instead, DBFS and the files within, are accessed with the same syntax as any other folder on the file system.

Bash

ls /dbfs/mnt/test_folder/test_folder1/
cat /dbfs/mnt/test_folder/test_folder1/file_name.txt

Python

import os
os.listdir('/dbfs/mnt/test_folder/test_folder1/’)

Scala

import java.io.File
val directory = new File("/dbfs/mnt/test_folder/test_folder1/")
directory.listFiles

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值