cancel
Showing results for 
Search instead for 
Did you mean: 
Administration & Architecture
Explore discussions on Databricks administration, deployment strategies, and architectural best practices. Connect with administrators and architects to optimize your Databricks environment for performance, scalability, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to access UnityCatalog's Volume inside Databricks App?

noklamchan
New Contributor II

I am more familiar with DBFS, which seems to be replaced by UnityCatalog Volume now. When I create a Databricks App, it allowed me to add resource to pick UC volume. How do I actually access the volume inside the app? 

I cannot find any example, the app template did have example to use UC to read Delta table, but not volume. My app need to access some relatively large assets (a few hundred MBs file). Is there some code snippet to show how to access the volume? I checked `/Volumes` and `dbutils` are not available inside the app.

 

#UnityCatalog #Databricks App

2 REPLIES 2

jameshughes
New Contributor III

The following code snippets should be sufficient, assuming you have listed the correct dependencies in your App's requirements.txt file.  You would just need to swap in your catalog and schema names below and then point to the correct file or iterate through all files in a directory.

df = spark.read.csv('/Volumes/catalogname/schemaname/files/sourcedata/FileName.csv', header=False, inferSchema=True)
df.show()

files = dbutils.fs.ls('dbfs:/Volumes/catalogname/schemaname/files/sourcedata/')

for file in files:
    print(file.name)

 

noklamchan
New Contributor II

As I mention, I am not reading a table, so Spark is not the right fit here (plus I don't want to included spark as dependencies to read a csv either). I also don't have dbutils.

I found this works:
```

cfg = Config() #This is available inside app

w = WorkspaceClient(host=cfg.host, token=cfg.token)
volume_path = "some_path"
response = w.files.download(volume_path + "file_name")
```

I am just not sure if this is the right way to do that, it would be great if the docs explains clearly what Databricks App have access to. 

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now