Unable to execute Databricks REST API for data copy using Python

Question:

When i am executing the below code to "copy data from databricks –> local" its failing with an error.
Can anyone please help me with how to solve this error.

import os

from databricks_cli.sdk.api_client import ApiClient
from databricks_cli.dbfs.api import DbfsApi
from databricks_cli.dbfs.dbfs_path import DbfsPath

api_client = ApiClient(host  = r"https://azuredatabricks.net/?o=XXXX",token = "121314141")


dbfs_source_file_path      = 'dbfs:/FileStore/datafilename.csv'
local_file_download_path   = 'C:/world.txt'

dbfs_api  = DbfsApi(api_client)
dbfs_path = DbfsPath(dbfs_source_file_path)

# Download the workspace file locally.
dbfs_api.get_file(dbfs_source_file_path,local_file_download_path,overwrite = True)

Error : Failing for AttributeError: ‘str’ object has no attribute ‘absolute_path’

The code is the exact replica of documentation in
https://docs.databricks.com/dev-tools/python-api.html#download-a-file-from-a-dbfs-path

Asked By: AdityaMani

||

Answers:

I have solved it using below code

from databricks_cli.sdk.api_client import ApiClient
from databricks_cli.dbfs.api import DbfsApi

api_client = ApiClient(host  = r"https://azuredatabricks.net/?o=XXXX",token = "121314141")

src1 = 'dbfs:/FileStore/datafilename.csv'
dst1 = 'C:/world.txt'

DbfsApi(api_client).cp(recursive=False, overwrite=True, src=src1, dst=dst1))

But please, let me know if you could solve the above error

Answered By: AdityaMani

I got the same error when I ran your code. After following the error trace, it can be seen that while using dbfs_api.get, the string path is passed instead of the object. Hence the error 'str' object has no attribute 'absolute_path'.

enter image description here

  • To solve the error, you need to pass the DbfsPath object instead of the actual string path. Hence modify your code as following:
import os

from databricks_cli.sdk.api_client import ApiClient
from databricks_cli.dbfs.api import DbfsApi
from databricks_cli.dbfs.dbfs_path import DbfsPath

api_client = ApiClient(host  = r"https://azuredatabricks.net/?o=XXXX",token = "121314141")


dbfs_source_file_path      = 'dbfs:/FileStore/datafilename.csv'
local_file_download_path   = 'C:/world.txt'

dbfs_api  = DbfsApi(api_client)
dbfs_path = DbfsPath(dbfs_source_file_path)

# Download the workspace file locally.
dbfs_api.get_file(dbfs_path,local_file_download_path,overwrite = True) #change here
Answered By: Saideep Arikontham