-
Notifications
You must be signed in to change notification settings - Fork 15
/
mountAzureStorage.scala
65 lines (51 loc) · 2.09 KB
/
mountAzureStorage.scala
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
// Databricks notebook source
// MAGIC %md # Mount Azure Storage (one-time)
// COMMAND ----------
// MAGIC %md ## Store Blob Storage Secrets (one-time)
// MAGIC
// MAGIC ```
// MAGIC curl -H "Authorization: Bearer <AUTH_TOKEN>" -X POST \
// MAGIC -k https://<CLUSTER_REGION>.azuredatabricks.net/api/2.0/preview/secret/scopes/create \
// MAGIC -d '{"scope": "azureStorage"}'
// MAGIC ```
// MAGIC
// MAGIC ```
// MAGIC curl -H "Authorization: Bearer <AUTH_TOKEN>" -X POST \
// MAGIC -k https://<CLUSTER_REGION>.azuredatabricks.net/api/2.0/preview/secret/secrets/write \
// MAGIC -d '{"scope": "azureStorage", "key": "accountName", "string_value": "<ACCOUNT_NAME>"}'
// MAGIC ```
// MAGIC
// MAGIC ```
// MAGIC curl -H "Authorization: Bearer <AUTH_TOKEN>" -X POST \
// MAGIC -k https://<CLUSTER_REGION>.azuredatabricks.net/api/2.0/preview/secret/secrets/write \
// MAGIC -d '{"scope": "azureStorage", "key": "accessKey", "string_value": "<ACCESS_KEY>"}'
// MAGIC ```
// COMMAND ----------
// MAGIC %md ## [Mount Blob Storage to DBFS](https://docs.azuredatabricks.net/spark/latest/data-sources/azure/azure-storage.html)
// COMMAND ----------
def mountAzureStorage(containerName:String, mountPoint: String) {
val accountName = dbutils.preview.secret.get(scope = "azureStorage", key = "accountName")
val accessKey = dbutils.preview.secret.get(scope = "azureStorage", key = "accessKey")
try {
dbutils.fs.mount(
source = s"wasbs://$containerName@$accountName.blob.core.windows.net/",
mountPoint = mountPoint,
extraConfigs = Map(s"fs.azure.account.key.$accountName.blob.core.windows.net" -> accessKey))
println(s"Mounting '$containerName' to $mountPoint completed!")
} catch {
case e: java.rmi.RemoteException => {
println(s"$mountPoint is already mounted!")
dbutils.fs.unmount(mountPoint)
mountAzureStorage(containerName, mountPoint)
}
case e: Exception => {
throw e
}
}
}
// COMMAND ----------
mountAzureStorage("parquet", "/mnt/parquet")
// COMMAND ----------
// MAGIC %fs mounts
// COMMAND ----------
// MAGIC %fs ls /mnt/parquet