-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathcreate_fabric_items.py
More file actions
246 lines (199 loc) · 8.24 KB
/
create_fabric_items.py
File metadata and controls
246 lines (199 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import base64
import json
import requests
import os
from glob import iglob
import zipfile
import time
# credential = ManagedIdentityCredential()
from azure.identity import AzureCliCredential
credential = AzureCliCredential()
cred = credential.get_token('https://api.fabric.microsoft.com/.default')
token = cred.token
fabric_headers = {"Authorization": "Bearer " + token.strip()}
key_vault_name = 'kv_to-be-replaced'
workspaceId = "workspaceId_to-be-replaced"
solutionname = "solutionName_to-be-replaced"
create_workspace = False
# pipeline_notebook_name = 'pipeline_notebook'
pipeline_notebook_name = 'cu_pipeline_notebook'
pipeline_name = 'data_pipeline'
lakehouse_name = 'lakehouse_' + solutionname
print("workspace id: " ,workspaceId)
if create_workspace == True:
workspace_name = 'workspace_' + solutionname
# create workspace
ws_url = 'https://api.fabric.microsoft.com/v1/workspaces'
ws_data = {
"displayName": workspace_name
}
ws_res = requests.post(ws_url, headers=fabric_headers, json=ws_data)
ws_details = ws_res.json()
# print(ws_details['id'])
workspaceId = ws_details['id']
fabric_base_url = f"https://api.fabric.microsoft.com/v1/workspaces/{workspaceId}/"
fabric_items_url = f"https://api.fabric.microsoft.com/v1/workspaces/{workspaceId}/items/"
fabric_create_workspace_url = f"https://api.fabric.microsoft.com/v1/workspaces"
#get workspace name
ws_res = requests.get(fabric_base_url, headers=fabric_headers)
workspace_name = ws_res.json()['displayName']
#create lakehouse
lakehouse_data = {
"displayName": lakehouse_name,
"type": "Lakehouse"
}
lakehouse_res = requests.post(fabric_items_url, headers=fabric_headers, json=lakehouse_data)
# print("lakehouse name: ", lakehouse_name)
# copy local files to lakehouse
from azure.storage.filedatalake import (
DataLakeServiceClient
)
account_name = "onelake" #always onelake
data_path = f"{lakehouse_name}.Lakehouse/Files/"
folder_path = "/"
account_url = f"https://{account_name}.dfs.fabric.microsoft.com"
service_client = DataLakeServiceClient(account_url, credential=credential)
#Create a file system client for the workspace
file_system_client = service_client.get_file_system_client(workspace_name)
directory_client = file_system_client.get_directory_client(f"{data_path}/{folder_path}")
print('uploading files')
# upload audio files
data_folder_path = os.path.join("..", "..", "data", "audio_data")
file_names = zip_files = list(iglob(os.path.join(data_folder_path, "audio*.zip")))
for file_name in file_names:
# Check if the file is a zip file
if file_name.endswith('.zip'):
# print('checking if filename ends in zip')
# Extract files from the zip folder
with zipfile.ZipFile(file_name, 'r') as zip_ref:
# print('zip file')
extract_dir = f"{file_name}_extracted"
zip_ref.extractall(extract_dir)
local_path = extract_dir
# print('ex dir', extract_dir)
# upload extracted folder
file_names = [f for f in iglob(os.path.join(local_path, "**", "*"), recursive=True) if os.path.isfile(f)]
# print('file_names ex', file_names)
for extracted_file in file_names:
upload_file_name = os.path.basename(extracted_file)
file_client = directory_client.get_file_client("cu_audio_files_all/" + upload_file_name)
# with open(file=os.path.join(extract_dir, extracted_file), mode="rb") as data:
with open(file=extracted_file, mode="rb") as data:
# print('data', data)
file_client.upload_data(data, overwrite=True)
# upload content understanding json file
folder_path = '/cu_analyzer_file'
local_path = os.path.join("..", "cu_scripts", "*.json")
file_names = [f for f in iglob(local_path) if os.path.isfile(f)]
for file_name in file_names:
base_name = os.path.basename(file_name)
path = f"{folder_path}/{base_name}"
file_client = directory_client.get_file_client(path)
with open(file=file_name, mode="rb") as data:
file_client.upload_data(data, overwrite=True)
#get environments
try:
fabric_env_url = f"https://api.fabric.microsoft.com/v1/workspaces/{workspaceId}/environments/"
env_res = requests.get(fabric_env_url, headers=fabric_headers)
env_res_id = env_res.json()['value'][0]['id']
# print(env_res.json())
except Exception: # Environments may not be provisioned yet
env_res_id = ''
#create notebook items
# notebook_names =['pipeline_notebook','01_process_data','02_create_calendar_data']
notebook_names = ['cu_pipeline_notebook', 'create_cu_template', 'process_cu_data']
# notebook_names =['process_data_new']
# add sleep timer
time.sleep(120) # 1 minute
for notebook_name in notebook_names:
with open('notebooks/cu/'+ notebook_name +'.ipynb', 'r') as f:
notebook_json = json.load(f)
print("lakehouse_res")
print(lakehouse_res)
print(lakehouse_res.json())
try:
notebook_json['metadata']['dependencies']['lakehouse']['default_lakehouse'] = lakehouse_res.json()['id']
notebook_json['metadata']['dependencies']['lakehouse']['default_lakehouse_name'] = lakehouse_res.json()['displayName']
notebook_json['metadata']['dependencies']['lakehouse']['default_lakehouse_workspace_id'] = lakehouse_res.json()['workspaceId']
except Exception: # Lakehouse metadata may not be available
pass
if env_res_id != '':
try:
notebook_json['metadata']['dependencies']['environment']['environmentId'] = env_res_id
notebook_json['metadata']['dependencies']['environment']['workspaceId'] = lakehouse_res.json()['workspaceId']
except Exception: # Environment metadata may not be available
pass
notebook_base64 = base64.b64encode(json.dumps(notebook_json).encode('utf-8'))
notebook_data = {
"displayName":notebook_name,
"type":"Notebook",
"definition" : {
"format": "ipynb",
"parts": [
{
"path": "notebook-content.ipynb",
"payload": notebook_base64.decode('utf-8'),
"payloadType": "InlineBase64"
}
]
}
}
requests.post(fabric_items_url, headers=fabric_headers, json=notebook_data)
time.sleep(120)
# get wrapper notebook id
fabric_notebooks_url = f"https://api.fabric.microsoft.com/v1/workspaces/{workspaceId}/notebooks"
notebooks_res = requests.get(fabric_notebooks_url, headers=fabric_headers)
notebooks_res.json()
pipeline_notebook_id = ''
print("notebook_res.json.values: ", notebooks_res.json().values())
for n in notebooks_res.json().values():
for notebook in n:
print("notebook displayname", notebook['displayName'])
if notebook['displayName'] == pipeline_notebook_name:
pipeline_notebook_id = notebook['id']
break
print("pipeline_notebook_id: ", pipeline_notebook_id)
# create pipeline item
pipeline_json = {
"name": pipeline_name,
"properties": {
"activities": [
{
"name": "process_data",
"type": "TridentNotebook",
"dependsOn": [],
"policy": {
"timeout": "0.12:00:00",
"retry": 0,
"retryIntervalInSeconds": 30,
"secureOutput": "false",
"secureInput": "false"
},
"typeProperties": {
"notebookId": pipeline_notebook_id,
"workspaceId": workspaceId
}
}
]
}
}
pipeline_base64 = base64.b64encode(json.dumps(pipeline_json).encode('utf-8'))
pipeline_data = {
"displayName":pipeline_name,
"type":"DataPipeline",
"definition" : {
# "format": "json",
"parts": [
{
"path": "pipeline-content.json",
"payload": pipeline_base64.decode('utf-8'),
"payloadType": "InlineBase64"
}
]
}
}
pipeline_response = requests.post(fabric_items_url, headers=fabric_headers, json=pipeline_data)
pipeline_response.json()
# run the pipeline once
job_url = fabric_base_url + f"items/{pipeline_response.json()['id']}/jobs/instances?jobType=Pipeline"
job_response = requests.post(job_url, headers=fabric_headers)