Tuesday, July 23, 2013

Simple example of reading Azure Table Storage within F# script

In case it's useful...

#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) )


Thursday, July 11, 2013

Azure Table Storage F# most basic example

In case anyone else was as confused as I was.... while trying an experiment out with WindowsAzure Table Storage. Searching the net just keeps highlighting old documentation and with the API changing it had me going no where for quite awhile. Basic run through here:

#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"
#r "System.Data.Services.Client"
#r "System.Linq"
open System
open Microsoft.WindowsAzure
open Microsoft.WindowsAzure.Storage
open System.Windows.Forms
// 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.BlobEndpoint = Uri("http://someAccountName.blob.core.windows.net/")
storageAccount.TableEndpoint = Uri("http://someAccountName.table.core.windows.net/")
storageAccount.QueueEndpoint = Uri("http://someAccountName.queue.core.windows.net/")
//Create a new client object.
let blobClient = storageAccount.CreateCloudBlobClient()
let tableClient = storageAccount.CreateCloudTableClient()
// Retrieve a reference to a container.
let container = blobClient.GetContainerReference("mycontainer")
// Create the container if it does not already exist.
container.CreateIfNotExists()
let table = tableClient.GetTableReference("someTable")
table.CreateIfNotExists()
// put some entities into the table
type YearMonthTxns() =
inherit Table.TableEntity()
member val txns = "" with get, set
let someYearMonthTxns = YearMonthTxns( PartitionKey = "2013", RowKey = "06", txns = "test" )
let cmd = Table.TableOperation.Insert(someYearMonthTxns)
let tableResult = table.Execute(cmd)
// Output container URI to debug window.
System.Diagnostics.Debug.WriteLine(container.Uri)