简体   繁体   English

遍历F#中Azure存储类型提供程序生成的类型

[英]Traverse a type generated by Azure Storage type provider in F#

I am trying to get my head around type providers in F# and what they can be used for. 我试图了解F#中的类型提供者以及它们可以用于什么。 I have the following problem: 我有以下问题:

I have a series of JSON objects in Azure Blob Storage stored as follows: 我在Azure Blob存储中有一系列JSON对象,存储方式如下:

container/YYYY/MM/DD/file.json

I can easily navigate to a specific file for a given date using a type provider. 我可以使用类型提供程序轻松导航到给定日期的特定文件。 For example, I can access the JSON object as a string for the 5th of May as 例如,我可以将JSON对象作为5月5日的字符串访问

type Azure = AzureTypeProvider<"ConnectionString">
let containers  = Azure.Containers.``container``.``2017/``.``05/``.``05/``.``file.json``.Read()

How can I take a user input date string, say "2017-05-05" and get the corresponding JSON object in a type safe way? 如何获取用户输入日期字符串,说“2017-05-05”并以类型安全的方式获取相应的JSON对象? Should I even be using type providers? 我应该使用类型提供商吗?

You're coming up against a common "issue" with the nature of many TPs, particularly ones that offer a schema against actual data - because it blends the line between data and types, you need to be aware of when you're working in a mode that works well with static types (ie you know at compile time the schema of the blob containers you're working with), or working in a way that's inherently dynamic. 你遇到了一个常见的“问题”,它涉及许多TP的性质,特别是那些提供针对实际数据的模式的问题 - 因为它混合了数据和类型之间的界限,你需要知道你什么时候工作一种适用于静态类型的模式(即,您在编译时知道正在使用的blob容器的模式),或者以一种本身动态的方式工作。

You have a few options here. 你有几个选择。

  1. Fall back to the "native" .NET SDK. 回归“原生”.NET SDK。 Every blob / container has associated AsCloudBlob() or AsCloudContainer() methods, so you can use the TP for the bits that you do know eg container name, maybe top level folders etc., and then fall back to the native SDK for the weakly-typed bits. 每个BLOB /容器具有关联AsCloudBlob()AsCloudContainer()方法,这样你就可以使用TP为您知道如容器名称,也许最高级别文件夹等位,然后回落到原生SDK对于弱 - 比特。

  2. Since the latest release of the TP, there's now support for programmatic access in a couple of ways: - 自TP的最新版本发布以来,现在支持以下几种方式进行编程访问: -

    • You can use indexers to get an unsafe handle to a blob eg let blob = Azure.Containers.container.["2017/05/05/file.json"] . 您可以使用索引器来获取blob的不安全句柄,例如, let blob = Azure.Containers.container.["2017/05/05/file.json"] There's no guarantee that the blob exists, so you need to check that yourself etc. 不能保证blob存在,所以你需要检查自己等等。

    • You can use the TryGetBlockBlob() method, which returns a blob option async - behind the scenes, it will do a check if the blob exists or not, and then return either None, or Some blob. 您可以使用TryGetBlockBlob()方法,它返回一个blob option async - 在幕后,它将检查blob是否存在,然后返回None或Some blob。

You can see more examples of all of these alternatives here . 您可以在此处查看所有这些替代方案的更多示例。

  1. If you know up-front the full path you're working with (at compile time - perhaps some well-know paths etc.), you can also use the offline support in the TP to create an explicit blob schema at compile time without needing a real storage account. 如果您事先知道正在使用的完整路径(在编译时 - 可能是一些众所周知的路径等),您还可以使用TP中的脱机支持在编译时创建显式blob模式,而无需一个真正的存储帐户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM