cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

Read file from dbfs with pd.read_csv() using databricks-connect

hamzatazib96
New Contributor III

Hello all,

As described in the title, here's my problem:

1. I'm using databricks-connect in order to send jobs to a databricks cluster

2. The "local" environment is an AWS EC2

3. I want to read a CSV file that is in DBFS (databricks) with

pd.read_csv()
. Reason for that is that it's too big to do
spark.read.csv()
and then
.toPandas()
(crashes everytime).

4. When I run

pd.read_csv("/dbfs/FileStore/some_file")
I get a
FileNotFoundError
because it points to the local S3 buckets rather than to dbfs. Is there a way to do what I want to do (e.g. change where pandas looks for files with some options)?

Thanks a lot in advance!

28 REPLIES 28

Anonymous
Not applicable

Hi @Kaniz Fatma​ ,

No, I still haven't found the solution and I can't read from dbfs​ (not with pandas.read_csv).

I meant to say that the setup tests pass, so the issue is not in the setup)

Kaniz
Community Manager
Community Manager

Hi @Arturo Amador​ , Can you please test once by changing your DBR version to less than 7?

Anonymous
Not applicable

Hi @Kaniz Fatma​ ,

I will try that and report!​

Kaniz
Community Manager
Community Manager

Thanks!

Anonymous
Not applicable

Hi @Kaniz Fatma​ ,

I can confirm that after downgrading to the DBR 6.4, and passing all the tests in:

databricks-connect test

I am still getting the FileNotFound error when trying to use

pd.read_csv('/dbfs/mnt/datalake_gen2_data/some.csv'')

Anonymous
Not applicable

Hi,

After some research, I have found out that the pandas API reads only local files. This means that even if a read_csv command works in the Databricks Notebook environment, it will not work when using databricks-connect (pandas reads locally from within the notebook environment).

A work around is to use the pyspark spark.read.format('csv') API to read the remote files and append a ".toPandas()" at the end so that we get a pandas dataframe.

df_pandas = spark.read.format('csv').options(header='true').load('path/in/the/remote/dbfs/filesystem/').toPandas()

Student185
New Contributor III

Hi Arturooa,

It seems we are holding a similar conclusion. Just a quick question, what do you mean for 'local files'? I've uploaded my files into dbfs, are they not local files after that?

Thanks

Anonymous
Not applicable

Hi @Yuanyue Liu​ ,

The spark engine is connected to the (remote) workers on Databricks, this is the reason why you can read the data from the dbfs by use of:

spark.read.format('csv').options(header='true').load('path/in/the/remote/dbfs/filesystem/')

The same happens with dbutils, for example. You can read files in the dbfs with for example:

dbutils.fs.ls(files_path)

Pandas does not connect directly to the remote filesystem (dbfs). That is the reason why you have to first read the remote data with spark and then transform to an in-memory dataframe (pandas).

I am using pandas profiling and after I make an HTML report, which is written to the local driver (since pandas_profiling does not connect to the remote filesystem either), I use dbutils to upload data to my mnt drive in dbfs (that comes from a datalake gen2).

I hope this helps!

Anonymous
Not applicable

@Arturo Amador​ - Would you be happy to mark your answer as best if the issue has been resolved by what you found? That will help others find your answer more quickly in the future.

Anonymous
Not applicable

Hi, @Piper Wilson​ ,

it is actually @hamzatazib96​  that needs to mark the answer as best 🙂

Anonymous
Not applicable

WHOOPS! Thank you, @Arturo Amador​!

@hamzatazib96​ - If any of the answers solved the issue, would you be happy to mark it as best?

hamzatazib96
New Contributor III

Done! Thanks all for the answers and help!

Best way I found around this was to simply do an SCP transfer using the databricks exe from DBFS to an S3 bucket. The flow was:

DBFS -> EC2 Local -> S3 bucket

farazanwar
New Contributor II

I am getting the same error I have mounted azure data lake and can see the files but when writing the csv file it gives error for context

imageimageimageStrange thing is that this works for other times

so16
New Contributor II

Please guys I need your help, I got the same issue still after readed all your comments.
I am using Databricks-connect(version 13.1) on pycharm and trying to load file that are on the dbfs storage.

spark = DatabricksSession.builder.remote(
host=host, token=token, cluster_id=c_id).getOrCreate()
path="dbfs:/mnt/storage/file.csv"
df = spark.read.format("csv").option("header", "true").load(path)

Give me the error:

pyspark.errors.exceptions.connect.SparkConnectGrpcException: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.FAILED_PRECONDITION
details = "INVALID_STATE: Unsupported 12.2.x-scala2.12 0611-073104-1kjepouv on Databricks Runtime Version. (requestId=8c278ab3-348a-4fa1-9797-6d58d571eeff)"
debug_error_string = "UNKNOWN:Error received from peer {grpc_message:"INVALID_STATE: Unsupported 12.2.x-scala2.12 0611-073104-1kjepouv on Databricks Runtime Version. (requestId=8c278ab3-348a-4fa1-9797-6d58d571eeff)", grpc_status:9, created_time:"2023-07-19T19:52:47.881727713+00:00"}"