Home > OS >  How to query blob storage data using python
How to query blob storage data using python

Time:11-15

Assuming I set up my blob_client correctly (with the right storage account key, url, container name, and blob name) why is my python query code failing? The file in blob is a JSON.

query_expression = "SELECT COUNT(*) from blobdata"
input_format = DelimitedJsonDialect(delimiter=',')
reader = blob_client.query_blob(query_expression, on_error=on_error, blob_format=input_format)
content = reader.readall()
print(content)

Error: azure.core.exceptions.HttpResponseError: Invalid path for JSON-formatted input.

additionally what should the table in the query string be?

CodePudding user response:

I was able to dig this up from the Azure documentation

https://learn.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.blobclient?view=azure-python#azure-storage-blob-blobclient-query-blob

query_expression = "SELECT _2 from BlobStorage"
input_format = DelimitedTextDialect(delimiter=',', quotechar='"', lineterminator='\n', escapechar="", has_header=False)
reader = blob_client.query_blob(query_expression, on_error=on_error, blob_format=input_format, output_format=output_format)

I think you need to at least add that "DelimitedTextDialect" input, and the example was querying from BlobStorage.

I find the azure python docs pretty hard to parse, but I think you can import the "DelimitedTextDialect" from azure.storage.blob.delimitedtextdialect

see https://learn.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.delimitedtextdialect?view=azure-python

  • Related