开发者

How do I store the contents of an array in tablestorage

开发者 https://www.devze.com 2023-02-18 04:43 出处:网络
I need to store the contents of an array into Azure tablestorage. The array will have between 0 and 10开发者_JAVA技巧0 entries. I don\'t want to have to create 100 different elements so is there a way

I need to store the contents of an array into Azure tablestorage. The array will have between 0 and 10开发者_JAVA技巧0 entries. I don't want to have to create 100 different elements so is there a way I can pack up the array, store it and unpack it later. Any examples would be much appreciated. I just don't know where to start :-(


You need to serialize the array into binary or xml and then use the appropriate column type to store the data (binary object or xml.)

XML will be the most flexible because you can still query the values while they are in storage. (You can't query binary data. Not easily anyway.) Here is an example of serializing and here is one for inserting the value into a table.

Some detail on XML support in Azure:

The xml Data Type

SQL Azure Database supports xml data type that stores XML data. You can store xml instances in a column or in a variable of the xml type.

Support for XML Data Modification Language

The XML data modification language (XML DML) is an extension of the XQuery language. The XML DML adds the following case-sensitive keywords to XQuery and they are supported in SQL Azure Database:

insert (XML DML) delete (XML DML) replace value of (XML DML)

Support for xml Data Type Methods

You can use the xml data type methods to query an XML instance stored in a variable or column of the xml type. SQL Azure Database supports the following xml data type methods:

query() Method (xml data type) value() Method (xml data type) exist() Method (xml data type) modify() Method (xml data type) nodes() Method (xml data type)


If you really are starting out in Azure Table Storage, then there are a few nice "simple" tutorials around - e.g. http://blogs.msdn.com/b/jnak/archive/2008/10/28/walkthrough-simple-table-storage.aspx

Once you are happy with reading/writing entities then there are several ways you can map your array to Table Storage.

  1. If you ever want to access each element of your array separately from the persistent storage, then you should create 0 to 99 separate entities - each with their own entity in the Table store.

  2. If you don't ever want to access them separately, then you can just store the array in a single entity (row) in the table - e.g. using PartitionKey="MyArrays", RowKey="" and having another column which contains the array serialised to e.g. JSON.

  3. As a variation on 2, you could also store the array items - 0 to 99 - in separate columns ("Array_0",..."Array_99") in the row. There are ways you could map this to a nice C# Array property using the Reading/Writing events on the table storage entity - but this might not be the best place to start if you're beginning with Azure.


Be careful, besides the 1MB entity limit there is a per field limit as well (I think it's 64kb)

Your best bet is to use the Lokad Fat Entity

http://code.google.com/p/lokad-cloud/wiki/FatEntities

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号