BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Exploring Azure with F# Azure Storage Type Provider

| by Pierre-Luc Maheu on May 11, 2016. Estimated reading time: 2 minutes |

The Azure Storage Type Provider brings statically typed access to Azure storage data sources: Blob, Table and Queue. Isaac Abraham, maintainer of the project, recently presented how to interact with these data sources using the type provider.

The setup of the type provider is the same for all data sources:

open FSharp.Azure.StorageTypeProvider

// Connect to a live account using a two-part name and key.
type Storage = AzureTypeProvider<"name", "key">

Blob
Azure Blob storage is a service that stores file data in the cloud. The storage type provider offers a statically typed access to the containers and files within:

let container = Storage.Containers.samples
let theBlob = container.``folder/``.``childFile.txt``
printfn "Blob '%s' is %d bytes big." theBlob.Name theBlob.Size

let totalSize =
    [ container.``file1.txt``
      container.``file2.txt``
      container.``file3.txt``
      container.``sample.txt`` ]
    |> List.sumBy(fun blob -> blob.Size)

printfn "These files take up %d bytes." totalSize

Table
Azure Table storage is a NoSQL key/value store hosted on Azure. The database is schemaless, meaning table rows don’t need to contain the same properties. Azure Table storage supports querying to a limited extent, allowing to get rows by key and filtering by property. It implements the OData protocol, which is the enpoint for querying.

The storage type provider brings the basic CRUD operations necessary to manage data. There are several ways to make queries, the simpler being a key lookup:

let employeeTable = Storage.Tables.employee
let firstEmployee = employeeTable.Get(Row "1", Partition "women")
let allWomen = employeeTable.GetPartition("women")

To filter by property, the type provider provides an IQueryable implementation. However, this solution is limited due to the restricted set of query operations supported by Azure Table. A possible alternative is to use the conditions auto generated by the type provider:
 

let longerQuery = employeeTable.Query()
  .``Where Years Working Is``.``Greater Than``(14)
  .``Where Name Is``.``Equal To``(“Fred”)
  .``Where Is Manager Is``.True()

Queue
Azure Queue storage is a messaging service, featuring a REST-based API. Queue storage also supports managing asynchronous tasks and building process work flows.

There is not much a type provider can provide over a traditional API for queues as there is not any schema or extensive querying. However, it provides several features for development and debugging purposes. Using F# Interactive, it is possible to explore the queues directly from the IDE.

let queue = Azure.Queues.``sample-queue``

async {
    printfn "Enqueuing a message!"
    do! queue.Enqueue("Hello from Azure Type Provider")
    printfn "Queue length is %d." (queue.GetCurrentLength())

    // Get the message back off the queue
    let dequeuedMessage = (queue.Dequeue() |> Async.RunSynchronously).Value
    printfn "%A" dequeuedMessage

    printfn "Deleting the message."
    do! queue.DeleteMessage dequeuedMessage.Id
    printfn "Queue length is %d." (queue.GetCurrentLength())
} |> Async.RunSynchronously

The Azure Storage Type Provider is an open source project available on GitHub.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and dont miss out on content that matters to you

BT