Skip to content

Instantly share code, notes, and snippets.

@bohdanszymanik
Created July 23, 2013 07:25
Show Gist options
  • Save bohdanszymanik/6060464 to your computer and use it in GitHub Desktop.
Save bohdanszymanik/6060464 to your computer and use it in GitHub Desktop.
F# script example retrieving data from Azure Table Storage - use case is an imaginary store of transactions in Json encoded document format indexed with a partitionkey of year-customer and rowkey of month-account
#r @"C:\wd\AzureTxnUploader\packages\WindowsAzure.Storage.2.0.6.0\lib\net40\Microsoft.WindowsAzure.Storage.dll"
#r @"C:\wd\AzureTxnUploader\packages\Microsoft.Data.OData.5.2.0\lib\net40\Microsoft.Data.OData.dll"
#r @"C:\wd\AzureTxnUploader\packages\Microsoft.Data.Edm.5.2.0\lib\net40\Microsoft.Data.Edm.dll"
#r @"C:\wd\AzureTxnUploader\packages\System.Spatial.5.2.0\lib\net40\System.Spatial.dll"
#r "Microsoft.WindowsAzure.ServiceRuntime"
open System
open Microsoft.WindowsAzure
open Microsoft.WindowsAzure.Storage
// Account name and key. Modify for your account.
let accountName = "someAccountName"
let accountKey = "someAccountKey"
//Get a reference to the storage account, with authentication credentials
let credentials = new Auth.StorageCredentials(accountName, accountKey)
let storageAccount = new CloudStorageAccount(credentials, true)
storageAccount.TableEndpoint = Uri("http://bohszy.table.core.windows.net/")
let tableClient = storageAccount.CreateCloudTableClient()
// start by retrieving a specific example entity
let txnTable = tableClient.GetTableReference("someTable")
let tableOperation = Table.TableOperation.Retrieve("2007-1", "1-1")
let tableResult = txnTable.Execute(tableOperation)
let properties = (tableResult.Result :?> Table.DynamicTableEntity).Properties // returns a dictionary from which we need to extract txnJson key-value
let json = properties.Item("txnJson").StringValue
// that worked, so lets retrieve a bunch more
[1 .. 1000] |> List.map(fun i -> txnTable.Execute(Table.TableOperation.Retrieve((sprintf "2007-%i" i), "1-1")))
// and what about a query that brings back multiple results...
let dynQuery = (new Table.TableQuery()).Where("PartitionKey lt '2013'").Select([|"txnJson"|] :> System.Collections.Generic.IList<string> )
let results = txnTable.ExecuteQuery(dynQuery)
// interesting to run results.GetType()
results
//|> Seq.take 4
|> Seq.iter (fun i -> printfn "Json: %s" (i.Properties.Item("txnJson").StringValue) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment