United Kingdom: +44 (0)208 088 8978

Azure Tables with F#

Akash continues his exploration of Azure Storage, this time using FSI to deploy a table to Azure with Farmer

We're hiring Software Developers

Click here to find out more

The next step in my exploration of Azure Storage was learning how to store structured data using Azure Tables. If you didn’t catch my last post about Azure Blob Storage check it out here! But if you did you know I'm a big fan of using script files so let's create Tables.fsx.

We'll be using Farmer to deploy and interact with a table on Azure.

#r "nuget: Azure.Data.Tables, 12.0.0-beta.6"
#r "nuget: Farmer, 1.5.0-beta3"  // At the moment of writing this table support is in a beta version of Farmer

open Farmer
open Farmer.Builders
open Azure.Data.Tables

Now we need to setup Farmer.

let storageAcc = storageAccount {
    name "blogtablestoragedemo"
    add_table "employees"
}

let deployment = arm {
    location Location.WestEurope
    add_resource storageAcc
    output "storageKey" storageAcc.Key
}

let deployedResources = deployment |> Deploy.execute "blogtabledeployment" []

let connectionString = deployedResources.["storageKey"]

Provided you're logged into Azure, that's all we need!

storageAcc is creating the necessary storage account and container "blogtablestoragedemo" with a table called "employees".

deployment is creating the ARM template and is linking the storageAcc to the template.

Executing the deployment will create our storage account under the resource group "blogtabledeployment".

deployedResources is of type Map<string,string> containing "storageKey" and the value of the storage account key from Azure - we'll use this key to interact with our table without ever seeing the connection string!

Lets add some data.

let tableServiceClient = TableServiceClient connectionString
let tableClient = tableServiceClient.GetTableClient "employees"

let employee = Map.empty

let entity = TableEntity employee

entity.PartitionKey <- "London"
entity.RowKey <- "Akash"
entity.Add("Employer", "CIT")

tableClient.AddEntity(entity)

To confirm it's been added we can read what’s in our table.

let results = tableClient.Query<TableEntity>()
let entities =
    [ for enity in results do
        entity.PartitionKey, entity.RowKey ]
val entities : (string * string) list = [("London", "Akash")]

Deleting an entry is as simple as passing the PartitionKey and Rowkey.

tableClient.DeleteEntity("London", "Akash")

Remember to delete your table once you’re done playing around.

tableServiceClient.DeleteTable "employees"

I've really been enjoying playing around with Azure Storage, the new Azure SDK keeps everything consistent between different services making it a lot easier as new services already feel familiar.