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

Compile all the scripts under the workspace folder

thushar
Contributor

In workspace one folder I have around 100+ pyspark scripts, all these scripts need to be compiled before running the main program. In order to compile all these files, we are using the %run magic command like %run ../prod/netSales. Since we have 100+ such files, we wrote 100+ magic commands like in a notebook to compile all 100+ files.

Question is, is there any way to compile all the files under one folder in ADB workspace instead of one by one? Are there any iterative methods are available to go through eah the file and compile it.

1 ACCEPTED SOLUTION

Accepted Solutions

Hubert-Dudek
Esteemed Contributor III

Problem is that you can list all files in workspace only via API call and than you can run every one of them using:

dbutils.notebook.run()

This is the script to list files from workspace (probably you need to add some filterning):

import requests
ctx = dbutils.notebook.entry_point.getDbutils().notebook().getContext()
host_name = ctx.tags().get("browserHostName").get()
host_token = ctx.apiToken().get()
 
response = requests.post(
    f'https://{host_name}/api/2.0/workspace/list',
    headers={'Authorization': f'Bearer {host_token}'},
    data = {'path': '<your-path>'}
  ).json()

View solution in original post

9 REPLIES 9

Kaniz
Community Manager
Community Manager

Hi @ thushar! My name is Kaniz, and I'm the technical moderator here. Great to meet you, and thanks for your question! Let's see if your peers in the community have an answer to your question first. Or else I will get back to you soon. Thanks.

Hubert-Dudek
Esteemed Contributor III

Problem is that you can list all files in workspace only via API call and than you can run every one of them using:

dbutils.notebook.run()

This is the script to list files from workspace (probably you need to add some filterning):

import requests
ctx = dbutils.notebook.entry_point.getDbutils().notebook().getContext()
host_name = ctx.tags().get("browserHostName").get()
host_token = ctx.apiToken().get()
 
response = requests.post(
    f'https://{host_name}/api/2.0/workspace/list',
    headers={'Authorization': f'Bearer {host_token}'},
    data = {'path': '<your-path>'}
  ).json()

When I tried the above code I got the below error.

{'error_code': 'ENDPOINT_NOT_FOUND', 'message': "No API found for 'POST /workspace/list'"}

Hubert-Dudek
Esteemed Contributor III

what distribution are you using (community, Azure)? so I will update that code as it is quite old

I didn't get you correctly.

I am using 9.1 LTS run time, ADB premium service, DS3_V2 instance... is it will help?

Hubert-Dudek
Esteemed Contributor III

so Azure - just tested , had to change to get and data to json. Please manipulate with path to get data from folder which you need:

import requests
ctx = dbutils.notebook.entry_point.getDbutils().notebook().getContext()
host_name = ctx.tags().get("browserHostName").get()
host_token = ctx.apiToken().get()
 
response = requests.get(
    f'https://{host_name}/api/2.0/workspace/list',
    headers={'Authorization': f'Bearer {host_token}',
            'Accept': 'application/json'},
    json = {'path': '/'}
  )

Hubert-Dudek
Esteemed Contributor III

did it help?

Sorry for the delay, that was not completely fixed

Modified the code which you shared, and now I could able to get the list of files under the folder in the workspace.

In order to compile (not to run) the files, is there any way there? Can we use magic command %run in some loop or something?

Atanu
Esteemed Contributor
Esteemed Contributor

@Thushar R​  does https://docs.databricks.com/notebooks/notebook-workflows.html notebook workflow help you on this scenario ?

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.