How to find the RELATIVE PATH of a BLOB? - azure

I want to integrate my Blob Storage with Azure Data Bricks, I found this in azure documentation as a part of connection, Can someone help me where can I find blob_relative_path ?
blob_account_name = "azureopendatastorage"
blob_container_name = "citydatacontainer"
blob_relative_path = "Safety/Release/city=Boston"
blob_sas_token = r"?st=2019-02-26T02%3A34%3A32Z&se=2119-02-27T02%3A34%3A00Z&sp=rl&sv=2018-03-
28&sr=c&sig=XlJVWA7fMXCSxCKqJm8psMOh0W4h7cSYO28coRqF2fs%3D"

The relative path of a blob is the path to the file of interest in your blob container. For instance, consider the following hierarchy:
Home > [blob_account_name] > [blob_container_name] > fakeDirectory/fakeSubDirectory/file.csv
The path to your file of interest in the identified container (i.e. fakeDirectory/fakeSubDirectory/file.csv) is the blob_relative_path.
Hope this helps!

Related

List all file names located in an Azure Blob Storage

I want to list in Databricks all file names located in an Azure Blob Storage.
My Azure Blob Storage is structured like this:
aaa
<br/>------bbb
<br/>------------bbb1.xml
<br/>------------bbb2.xml
<br/>------ccc
<br/>------------ccc1.xml
<br/>------------ccc2.xml
<br/>------------ccc3.xml
If I do:
dbutils.fs.ls('wasbs://xxx#xxx.blob.core.windows.net/aaa')
only subfolders bbb and ccc are listed like this:
[FileInfo(path='wasbs://xxx#xxx.blob.core.windows.net/aaa/bbb/', name='bbb/', size=0),
FileInfo(path='wasbs://xxx#xxx.blob.core.windows.net/aaa/ccc/', name='ccc/', size=0)]
I want to deepen to the last subfolder to see all file names located in aaa: bbb1.xml, bbb2.xml, ccc1.xml, ccc2.xml and ccc3.xml.
If I do:
dbutils.fs.ls('wasbs://xxx#xxx.blob.core.windows.net/aaa/*')
an error occurs because the path can not be parameterized.
Any idea to do this in Databricks?
dbutils.fs.ls doesn't support wildcards, that's why you're getting an error. You have few choices:
Use Python SDK for Azure blob storage to list files - it could be faster than using recursive dbutils.fs.ls, but you will need to setup authentication, etc.
You can do recursive calls to dbutils.fs.ls, using function like this, but it's not very performant:
def list_files(path, max_level = 1, cur_level=0):
"""
Lists files under the given path, recursing up to the max_level
"""
d = dbutils.fs.ls(path)
for i in d:
if i.name.endswith("/") and i.size == 0 and cur_level < (max_level - 1):
yield from list_files(i.path, max_level, cur_level+1)
else:
yield i.path
You can use Hadoop API to access files in your container, similar to this answer.

Azure Data Factory - Read in a list of filepaths from a fileshare and save to Blob

What I have is a list of filepaths, saved inside a text file.
eg: filepaths.txt ==
C:\Docs\test1.txt
C:\Docs\test2.txt
C:\Docs\test3.txt
How can I set up a Azure Data Factory pipeline, to essentially loop through each file path and copy it to Azure Blob Storage? So in blob storage, I would have:
\Docs\test1.txt
\Docs\test2.txt
\Docs\test3.txt
Thanks,
You can use the "List of files" option in the copy activity. But to do this in 1 step, your txt file with the list of files needs to be in the same Source as where the actual files are present.

Failed to save a file in azure data lake from azure data bricks

I'm trying to save the string content into azure data lake as XML content.
a string variable contains below mentioned xml content.
<project>
<dateformat>dd-MM-yy</dateformat>
<timeformat>HH:mm</timeformat>
<useCDATA>true</useCDATA>
</project>
i have used the below code to process the file into data lake.
xmlfilewrite = "/mnt/adls/ProjectDataDecoded.xml"
with open(xmlfilewrite , "w") as f:
f.write(project_processed_var)
it throws the following error:
No such file or directory: '/mnt/adls/ProjectDataDecoded.xml"
I'm able to access the data lake by using the above mounting point but unable do with the above function "open".
can anyone help me?
Issue is solved.
In databricks when you have a mount point existing on Azure Data Lake,we need to add "/dbfs" to the path and pass it to OPEN function.
The issue is solved by using below code
xmlfilewrite = "/dbfs/mnt/adls/ProjectDataDecoded.xml"
with open(xmlfilewrite , "w") as f:
f.write(project_processed_var)
You could try using the Spark-XML library. Convert your string to a dataframe where each row denotes one project. Then you can write it to ADLS in this way.
df.select("dateformat", "timeformat","useCDATA").write \
.format('xml') \
.options(rowTag='project', rootTag='project') \
.save('/mnt/adls/ProjectDataDecoded.xml')
Here is how you can include an external library -https://docs.databricks.com/libraries.html#create-a-library

How to rename files, as they are copied to a new location, with adf

I am performing copy activity from cosmosDB to Blob storage, collections will be copied to storage as files. I want those filenames to be renamed with "collectionname-date". The file should have name, followed by date and time as suffix to that. How can I achieve this?
I have to say i can't find any ways to get the collection name dynamically,but i implement other your requirements. Please see my configurations:
1.Cosmos db dataset:
as normal to set
2.Blob Storage dataset:
configure a parameter for it:
Then configure the dynamic file path:
Pass the collection static name(for me is coll) for the fileName param.
3.Output in Blob Storage:

How to access files within subfolders of a bucket GCS using Python?

from google.cloud import storage
import os
bucket = client.get_bucket('path to bucket')
The above code connects me to my bucket but I am struggling to connect with a specific folder within the bucket.
I am trying variants of this code, but no luck:
blob = bucket.get_blob("training/bad")
blob = bucket.get_blob("/training/bad")
blob = bucket.get_blob("path to bucket/training/bad")
I am hoping to get access to a list of images within the bad subfolder, but I can't seem to do so.
I don't even fully understand what a blob is despite reading the docs, and sort of winging it based on tutorials.
Thank you.
What you missed is the fact that in GCS objects in a bucket aren't organized in a filesystem-like directory structure/hierarchy, but rather in a flat structure.
A more detailed explanation can be found in How Subdirectories Work (in the gsutil context, true, but the fundamental reason is the same - the GCS flat namespace):
gsutil provides the illusion of a hierarchical file tree atop the
"flat" name space supported by the Google Cloud Storage service. To
the service, the object gs://your-bucket/abc/def.txt is just an object
that happens to have "/" characters in its name. There is no "abc"
directory; just a single object with the given name.
Since there are no (sub)directories in GCS then /training/bad doesn't really exist, so you can't list its content. All you can do is list all the objects in the bucket and select the ones with names/paths that start with /training/bad.
If you would like to find blobs (files) that exist under a specific prefix (subdirectory) you can specify prefix and delimiter arguments to the list_blobs() function
See the following example taken from the Google Listing Objects example (also GitHub snippet)
def list_blobs_with_prefix(bucket_name, prefix, delimiter=None):
"""Lists all the blobs in the bucket that begin with the prefix.
This can be used to list all blobs in a "folder", e.g. "public/".
The delimiter argument can be used to restrict the results to only the
"files" in the given "folder". Without the delimiter, the entire tree under
the prefix is returned. For example, given these blobs:
/a/1.txt
/a/b/2.txt
If you just specify prefix = '/a', you'll get back:
/a/1.txt
/a/b/2.txt
However, if you specify prefix='/a' and delimiter='/', you'll get back:
/a/1.txt
"""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blobs = bucket.list_blobs(prefix=prefix, delimiter=delimiter)
print('Blobs:')
for blob in blobs:
print(blob.name)
if delimiter:
print('Prefixes:')
for prefix in blobs.prefixes:
print(prefix)

Resources