使用F# Azure Storage Type Provider探索Azure

Azure Storage Type Provider提供对Azure存储数据资源的静态类型访问:BLOB、表以及队列。项目的维护者Isaac Abraham最近和大家交流了如何使用type provider和这些数据资源交互。

对于所有数据资源来说,对type provider的设置都是相同的:

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是将文件数据存储在云端的一项服务。storage type provider通过下面的示例提供对容器和文件的静态类型访问:

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


Azure Table storage将NoSQL键值存储托管于Azure上。NoSQL数据库是无模式的,表的行不需要包含相同属性。Azure Table storage支持在有限范围内的查询,可以通过查询键或者属性来得到相对应的行。它实现了OData协议,这对查询非常重要。

storage type provider可以提供基础的CRUD操作,对管理数据很有必要。有几种办法进行查询,以下的例子是一个简单的键查询:

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

type provider提供了IQueryable实现来满足属性查询。然而,由于Azure Table支持的查询操作受限,这个解决方案也是有限制的。一种可能的选择是使用type provider自动生成的条件:

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

队列
Azure Queue storage是消息服务,基于REST方式访问。Queue storage还支持管理异步任务和搭建过程工作流。

相比较于一个传统的队列API,type provider并没有太多优势,它不能提供模式或是拓展查询。然而,它为开发和调试提供了几个功能。使用F# Interactive就可以直接从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

Azure Storage Type Provider是一个开源的项目,可以在GitHub上获得。

查看英文原文:Exploring Azure with F# Azure Storage Type Provider

感谢张龙对本文的审校。

给InfoQ中文站投稿或者参与内容翻译工作,请邮件至editors@cn.infoq.com。也欢迎大家通过新浪微博(@InfoQ,@丁晓昀),微信(微信号:InfoQChina)关注我们。

你可能感兴趣的:(使用F# Azure Storage Type Provider探索Azure)