BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Exploring Azure with F# Azure Storage Type Provider

Exploring Azure with F# Azure Storage Type Provider

Lire ce contenu en français

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
Style

BT