Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read/Write single file in DataBricks

I have a file which contains a list of names stored in a simple text file. Each row contains one name. Now I need to pro grammatically append a new name to this file based on a users input. For the input itself I use DataBricks widgets - this is working just fine and I have the new name stored in a string object. Now I need to append this name to my file.

the file is mounted in the DataBricks File System (DBFS) under /mnt/blob/myNames.txt

when trying to read the file like this:

f = open("/mnt/blob/myNames.txt", "r") 
print f

it returns an error "No such file or directory"

So I tried to wrap my new name into a dataframe and append it to the existing file but this also did not work as dataframe.write.save is designed to write into folders

what would be the most simple python could that I could use to append this new name to my file?

like image 702
Gerhard Brueckl Avatar asked Mar 16 '18 10:03

Gerhard Brueckl


People also ask

How do I read a text file in Databricks?

You can write and read files from DBFS with dbutils. Use the dbutils. fs. help() command in databricks to access the help menu for DBFS.

How do I display files in Databricks?

You can access the file system using magic commands such as %fs or %sh . You can also use the Databricks file system utility (dbutils. fs). Azure Databricks uses a FUSE mount to provide local access to files stored in the cloud.

How do I read a local file in Databricks?

If you use the Databricks Connect client library you can read local files into memory on a remote Databricks Spark cluster. See details here. The alternative is to use the Databricks CLI (or REST API) and push local data to a location on DBFS, where it can be read into Spark from within a Databricks notebook.

How do I create a CSV file in Databricks?

1. Explore the Databricks File System (DBFS) From Azure Databricks home, you can go to “Upload Data” (under Common Tasks)→ “DBFS” → “FileStore”. DBFS FileStore is where you create folders and save your data frames into CSV format.


2 Answers

You can open the file in append mode using 'a'

with  open("/dbfs/mnt/sample.txt", "a") as f:
  f.write("append values")

Now you can view the contents using

with  open("/dbfs/mnt/sample.txt", "r") as f_read:
for line in f_read:
 print(line)

Solution: Here

like image 50
Unmesha Sreeveni U.B Avatar answered Oct 19 '22 23:10

Unmesha Sreeveni U.B


You can write and read files from DBFS with dbutils. Use the dbutils.fs.help() command in databricks to access the help menu for DBFS.

You would therefore append your name to your file with the following command:

dbutils.fs.put("/mnt/blob/myNames.txt", new_name)

You are getting the "No such file or directory" error because the DBFS path is not being found. Use dbfs:/ to access a DBFS path. This is how you should have read the file:

f = open("/dbfs/mnt/blob/myNames.txt", "r")
like image 23
Elsis Avatar answered Oct 20 '22 01:10

Elsis