Skip to content

Commit f085372

Browse files
committed
Copy Files recursively to AIDP from Databricks Folder.
1 parent 9bcd86d commit f085372

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"metadata": {
3+
"kernelspec": {
4+
"name": "notebook"
5+
},
6+
"language_info": {
7+
"file_extension": ".py",
8+
"mimetype": "text/x-python",
9+
"name": "python"
10+
},
11+
"Last_Active_Cell_Index": 5
12+
},
13+
"nbformat_minor": 5,
14+
"nbformat": 4,
15+
"cells": [
16+
{
17+
"id": "aedb2989-04d1-4e7d-894d-aff632ce0297",
18+
"cell_type": "markdown",
19+
"source": "Oracle AI Data Platform v1.0\n\nCopyright © 2025, Oracle and/or its affiliates.\n\nLicensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/",
20+
"metadata": {
21+
"type": "markdown"
22+
}
23+
},
24+
{
25+
"id": "a7f00219-2c74-4cba-a12b-67d2eb829168",
26+
"cell_type": "markdown",
27+
"source": "### Sample Code: Exporting Databricks Files to AIDP.\n\nThis example demonstrates how to export files recursively from databricks workspace using `databricks-sdk` Library and write to an **AIDP**.\n\n**Note:** \n\n- Replace all placeholders (e.g., `<DATABRICKS_WORKSPACE_URL>`, `<DATABRICKS_TOKEN>`, `<DATABRICKS_PATH>`, `<AIDP_PATH>` etc.) with values specific to your environment before running the notebook. \n- Provide Source to Target String replacement if you wish to do while importing to AIDP.\n- Use with caution: The notebook is designed for exporting notebooks & code related files only.",
28+
"metadata": {
29+
"type": "markdown"
30+
}
31+
},
32+
{
33+
"id": "6df91459-a48a-4920-9d16-973c61bee150",
34+
"cell_type": "code",
35+
"source": "import os\nimport base64\nfrom databricks.sdk import WorkspaceClient\nfrom databricks.sdk.service import workspace",
36+
"metadata": {
37+
"type": "python",
38+
"trusted": true
39+
},
40+
"outputs": [],
41+
"execution_count": null
42+
},
43+
{
44+
"id": "4a233ed1-458c-47bd-9444-63622ea8cf6b",
45+
"cell_type": "code",
46+
"source": "#Databricks Workspace URL\ndatabricks_workspace_url = \"DATABRICKS_WORKSPACE_URL\"\n#Databricks Token\ndatabricks_token = \"DATABRICKS_TOKEN\"\n# Define the Databricks folder you want to export\ndatabricks_path = \"DATABRICKS_PATH\"\n# Define the local AIDP directory to write the exported content\naidp_path = \"AIDP_PATH\"",
47+
"metadata": {
48+
"type": "python",
49+
"trusted": true
50+
},
51+
"outputs": [],
52+
"execution_count": null
53+
},
54+
{
55+
"id": "4b8ab52e-0885-4ae2-8864-5b7563d20b79",
56+
"cell_type": "code",
57+
"source": "#Provide Comma Seperated mapping to replace Source String with Target String. These are just string replacement so mapping should be provided carefully.\ndbx_to_aidp_replacement_mappings = {\n \"SOURCE_STR_1\": \"TARGET_STR_1\",\n \"SOURCE_STR_2\": \"TARGET_STR_2\"\n}",
58+
"metadata": {
59+
"type": "python",
60+
"trusted": true
61+
},
62+
"outputs": [],
63+
"execution_count": null
64+
},
65+
{
66+
"id": "c9ae41ae-4e57-4fd9-8a59-f360a3cb60ad",
67+
"cell_type": "code",
68+
"source": "#Recursively exports a Databricks workspace folder to a local directory, preserving the nested folder structure and exporting notebooks as .ipynb files.\n\ndef export_folder_recursively(databricks_path: str , aidp_path: str , w: WorkspaceClient):\n\n try:\n # List contents of the current workspace path\n contents = w.workspace.list(path=databricks_path)\n except Exception as e:\n print(f\"Failed to list contents of Databricks path {databricks_path}: {e}\")\n return\n\n for item in contents:\n dbx_item_path = item.path\n\n # Determine the relative path to maintain the nested structure\n dbx_relative_path = os.path.relpath(dbx_item_path , databricks_path)\n aidp_full_path = os.path.join(aidp_path , dbx_relative_path)\n\n if item.object_type == workspace.ObjectType.DIRECTORY:\n # Create the local directory and recurse into it\n os.makedirs(aidp_full_path , exist_ok=True)\n print(f\"Created local directory: {aidp_full_path}\")\n export_folder_recursively(dbx_item_path , aidp_full_path , w)\n elif item.object_type == workspace.ObjectType.FILE or item.object_type == workspace.ObjectType.NOTEBOOK:\n file_name = os.path.basename(dbx_item_path)\n if item.object_type == workspace.ObjectType.NOTEBOOK:\n local_file_path = os.path.join(os.path.dirname(aidp_full_path) , f\"{file_name}.ipynb\")\n format = workspace.ExportFormat.JUPYTER\n else:\n local_file_path = os.path.join(os.path.dirname(aidp_full_path) , file_name)\n format = workspace.ExportFormat.SOURCE\n\n try:\n # Export the file/notebook content\n print(f\"Exporting File/Notebook: {dbx_item_path} to {local_file_path}\")\n dbx_file_content = w.workspace.export(\n path=dbx_item_path ,\n format=format\n )\n\n \n binary_content = base64.b64decode(dbx_file_content.content)\n code_string = binary_content.decode('utf-8')\n \n # Iterate through the mapping and replace content\n for dbx_str, aidp_str in dbx_to_aidp_replacement_mappings.items():\n code_string = code_string.replace(dbx_str, aidp_str)\n \n modified_binary_content = code_string.encode('utf-8')\n\n with open(local_file_path , \"wb\") as f:\n f.write(modified_binary_content)\n\n print(f\"Downloaded File: {file_name} as {local_file_path}\")\n\n except Exception as export_error:\n print(f\"Failed to export notebook {dbx_item_path}: {export_error}\")\n\n else:\n print(f\"Skipping unsupported object type: {item.object_type} at {dbx_item_path}\")",
69+
"metadata": {
70+
"type": "python",
71+
"trusted": true
72+
},
73+
"outputs": [],
74+
"execution_count": null
75+
},
76+
{
77+
"id": "adaeed13-c355-4503-90bc-9aa8262c30cb",
78+
"cell_type": "code",
79+
"source": "# Initialize the WorkspaceClient\nw = WorkspaceClient(\n host=databricks_workspace_url ,\n token=databricks_token ,\n)\n\nprint(f\"Starting export from Databricks path '{databricks_path}' to local path '{aidp_path}'\")\n\n# Create AIDP local directory if not exists.\nos.makedirs(aidp_path , exist_ok=True)\n\n# Start the recursive export\nexport_folder_recursively(databricks_path , aidp_path , w)\n\nprint(\"\\nExport process finished.\")",
80+
"metadata": {
81+
"type": "python",
82+
"trusted": true
83+
},
84+
"outputs": [],
85+
"execution_count": null
86+
}
87+
]
88+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
databricks-sdk

0 commit comments

Comments
 (0)