How can we save or upload .py file on dbfs/filestore - azure

We have few .py files on my local needs to stored/saved on fileStore path on dbfs. How can I achieve this?
Tried with dbUtils.fs module copy actions.
I tried the below code but did not work, I know something is not right with my source path. Or is there any better way of doing this? please advise
'''
dbUtils.fs.cp ("c:\\file.py", "dbfs/filestore/file.py")
'''

It sounds like you want to copy a file on local to the dbfs path of servers of Azure Databricks. However, due to the interactive interface of Notebook of Azure Databricks based on browser, it could not directly operate the files on local by programming on cloud.
So the solutions as below that you can try.
As #Jon said in the comment, you can follow the offical document Databricks CLI to install the databricks CLI via Python tool command pip install databricks-cli on local and then copy a file to dbfs.
Follow the offical document Accessing Data to import data via Drop files into or browse to files in the Import & Explore Data box on the landing page, but also recommended to use CLI, as the figure below.
Upload your specified files to Azure Blob Storage, then follow the offical document Data sources / Azure Blob Storage to do the operations include dbutils.fs.cp.
Hope it helps.

Related

Unable to access files uploaded to dbfs on Databricks community edition Runtime 9.1. Tried the dbutils.fs.cp workaround which also didn't work

I'm a beginner to Spark and just picked up the highly recommended 'Spark - the Definitive Edition' textbook. Running the code examples and came across the first example that needed me to upload the flight-data csv files provided with the book. I've uploaded the files at the following location as shown in the screenshot:
/FileStore/tables/spark_the_definitive_guide/data/flight-data/csv
I've in the past used Azure Databricks to upload files directly onto DBFS and access them using ls command without any issues. But now in community edition of Databricks (Runtime 9.1) I don't seem to be able to do so.
When I try to access the csv files I just uploaded into dbfs using the below command:
%sh ls /dbfs/FileStore/tables/spark_the_definitive_guide/data/flight-data/csv
I keep getting the below error:
ls: cannot access '/dbfs/FileStore/tables/spark_the_definitive_guide/data/flight-data/csv': No such file or directory
I tried finding out a solution and came across the suggested workaround of using dbutils.fs.cp() as below:
dbutils.fs.cp('C:/Users/myusername/Documents/Spark_the_definitive_guide/Spark-The-Definitive-Guide-master/data/flight-data/csv', 'dbfs:/FileStore/tables/spark_the_definitive_guide/data/flight-data/csv')
dbutils.fs.cp('dbfs:/FileStore/tables/spark_the_definitive_guide/data/flight-data/csv/', 'C:/Users/myusername/Documents/Spark_the_definitive_guide/Spark-The-Definitive-Guide-master/data/flight-data/csv/', recurse=True)
Neither of them worked. Both threw the error: java.io.IOException: No FileSystem for scheme: C
This is really blocking me from proceeding with my learning. It would be supercool if someone can help me solve this soon. Thanks in advance.
I believe the way you are trying to use is the wrong one, use it like this
to list the data:
display(dbutils.fs.ls("/FileStore/tables/spark_the_definitive_guide/data/flight-data/"))
to copy between databricks directories:
dbutils.fs.cp("/FileStore/jars/d004b203_4168_406a_89fc_50b7897b4aa6/databricksutils-1.3.0-py3-none-any.whl","/FileStore/tables/new.whl")
For local copy you need the premium version where you create a token and configure the databricks-cli to send from the computer to the dbfs of your databricks account:
databricks fs cp C:/folder/file.csv dbfs:/FileStore/folder

Databricks DBFS File Browser not showing some DBFS root locations

I have a fresh Azure Databricks instance that I'm doing some experimenting on. Per the Databricks documentation, I activated the DBFS File Browser in the Admin Console.
However, when browsing the DBFS root location, only FileStore, mnt and user folders are showing (see below). Reading this Databricks doc, I expected to also see databricks-datasets, databricks-results and databricks/init, but these are not showing in the GUI.
However, I am able to access e.g. databricks-datasets programatically through a notebook command:
Does anyone know what is going on here? At first I thought it may be different since it's an instance of Azure Databricks, but the Azure Databricks documentation is exactly the same and suggests I should be able to see the same root folders.
Why can I not see some DBFS root folders in the DBFS File Browser GUI, even though I can programatically access them?
I have the same issue. There is no folder/file appearing in the UI of Databricks at the following location: dbfs/FileStore/ even after I do an upload. But it does appear in the notebook when I run dbutils.fs.ls("/FileStore/").
However, the folders and files can be found in the UI at the following location: /FileStore/

External Properties File in Azure Databricks

We have a full fledge Spark Application that is taking a lot off parameter from properties file. Now we want move the application to Azure notebook format .Entire code is working fine and giving expected result with hard coded parameter. But is it possible to use external properties file in Azure Databricks Notebook also ??If we can, then where we need to place properties file??
You may utilize the Databricks DBFS Filestore, Azure Databricks note books can access user's files from here.
To Upload the properties file you have, you can use 2 options
Using wget,
import sys
"wget -P /tmp/ http://<your-repo>/<path>/app1.properties"
dbutils.fs.cp("file:/tmp/app1.properties", "dbfs:/FileStore/configs/app1/")
Using dbfs.fs.put, (may be an one-time activity to create this file)
dbutils.fs.put("FileStore/configs/app1/app1.properties", "prop1=val1\nprop2=val2")
To import the properties file values,
properties = dict(line.strip().split('=') for line in open('/dbfs/FileStore/configs/app1/app1.properties'))
Hope this helps!!
There's a possibility of providing/returning arguments with use of Databricks Jobs REST API, more information can be found e.g. here: https://docs.databricks.com/dev-tools/api/latest/examples.html#jobs-api-example

Databricks File Save

I'm using Databricks on Azure and am using a library called OpenPyXl.
I'm running the sameple cosde shown here: and the last line of the code is:
wb.save('document.xlsx', as_template=False)
The code seems to run so I'm guessing it's storing the file somewhere on the cluster. Does anyone know where so that I can then transfer it to BLOB?
To save a file to the FileStore, put it in the /FileStore directory in DBFS:
dbutils.fs.put("/FileStore/my-stuff/my-file.txt", "Contents of my
file")
Note: The FileStore is a special folder within Databricks File System - DBFS where you can save files and have them accessible to your web browser. You can use the File Store to:
For more detials, refer "Databricks - The FileStore".
Hope this helps.

Access blob file using time stamp in Azure

I want to access a blob file that is getting generated out of azure ml web service along with the ilearner and csv file. The problem is that the file is getting generated automatically with guid as its name, and with no response mentioning the existence of that file. I know that the file is getting generated as i can access it through azure portal. i would like to automatically access the file and the only possibility i can see is by using the time stamp of other file created at the same instance. is there any api or method available to access blobs created at a particular instance using time stamp instead of file name?
According to your description, I guess you used Export Data Module.
As your requirements, it is highly recommended that you could replace Export Data with Execute Python Script in Azure Machine Learning which allows you to customize the blob file name.
For the introduction to Execute Python Script, you could refer to the official documentation here.
Please refer to the following steps to implement:
Step 1: Please use Python virtualenv create Python independent running environment, specific steps please refer to https://virtualenv.pypa.io/en/stable/userguide/, then use the pip install command to download Azure Storage related Scripts.
Compress all of the files in the Lib/site-packages folder into a zip package (I'm calling it azure - storage - package here)
Step 2: Upload the zip package into the Azure Machine Learning WorkSpace DataSet.
specific steps please refer to the Technical Notes.
After success, you will see the uploaded package in the DataSet List, dragging it to the third node of the Execute Python Script.
Step 3 : Customize the blob file name in the python script to the timestamp, you could even add GUID to ensure uniqueness at the end of the file name.
I provided a simple snippet of code:
import pandas as pd
from azure.storage.blob import BlockBlobService
import time
def azureml_main(dataframe1 = None, dataframe2 = None):
myaccount= '****'
mykey= '****'
block_blob_service = BlockBlobService(account_name=myaccount, account_key=mykey)
block_blob_service.create_blob_from_text('test', 'str(int(time.time()))+'.txt', 'upload image test')
return dataframe1,
Also,you could refer to the SO thread Access Azure blog storage from within an Azure ML experiment.
Hope it helps you.

Resources