OLD | NEW |
(Empty) | |
| 1 .. simpledb_tut: |
| 2 |
| 3 ============================================ |
| 4 An Introduction to boto's SimpleDB interface |
| 5 ============================================ |
| 6 This tutorial focuses on the boto interface to AWS' SimpleDB_. This tutorial |
| 7 assumes that you have boto already downloaded and installed. |
| 8 |
| 9 .. _SimpleDB: http://aws.amazon.com/simpledb/ |
| 10 |
| 11 Creating a Connection |
| 12 --------------------- |
| 13 The first step in accessing SimpleDB is to create a connection to the service. |
| 14 To do so, the most straight forward way is the following:: |
| 15 |
| 16 >>> import boto |
| 17 >>> conn = boto.connect_sdb(aws_access_key_id='<YOUR_AWS_KEY_ID>',aws_secret
_access_key='<YOUR_AWS_SECRET_KEY>') |
| 18 >>> conn |
| 19 SDBConnection:sdb.amazonaws.com |
| 20 >>> |
| 21 |
| 22 Bear in mind that if you have your credentials in boto config in your home |
| 23 directory, the two keyword arguments in the call above are not needed. Also |
| 24 important to note is that just as any other AWS service, SimpleDB is |
| 25 region-specific and as such you might want to specify which region to connect |
| 26 to, by default, it'll connect to the US-EAST-1 region. |
| 27 |
| 28 Creating Domains |
| 29 ---------------- |
| 30 Arguably, once you have your connection established, you'll want to create one o
r more dmains. |
| 31 Creating new domains is a fairly straight forward operation. To do so, you can p
roceed as follows:: |
| 32 |
| 33 >>> conn.create_domain('test-domain') |
| 34 Domain:test-domain |
| 35 >>> |
| 36 >>> conn.create_domain('test-domain-2') |
| 37 Domain:test-domain |
| 38 >>> |
| 39 |
| 40 Please note that SimpleDB, unlike its newest sibling DynamoDB, is truly and comp
letely schema-less. |
| 41 Thus, there's no need specify domain keys or ranges. |
| 42 |
| 43 Listing All Domains |
| 44 ------------------- |
| 45 Unlike DynamoDB or other database systems, SimpleDB uses the concept of 'domains
' instead of tables. |
| 46 So, to list all your domains for your account in a region, you can simply do as
follows:: |
| 47 |
| 48 >>> domains = conn.get_all_domains() |
| 49 >>> domains |
| 50 [Domain:test-domain, Domain:test-domain-2] |
| 51 >>> |
| 52 |
| 53 The get_all_domains() method returns a :py:class:`boto.resultset.ResultSet` cont
aining |
| 54 all :py:class:`boto.sdb.domain.Domain` objects associated with |
| 55 this connection's Access Key ID for that region. |
| 56 |
| 57 Retrieving a Domain (by name) |
| 58 ----------------------------- |
| 59 If you wish to retrieve a specific domain whose name is known, you can do so as
follows:: |
| 60 |
| 61 >>> dom = conn.get_domain('test-domain') |
| 62 >>> dom |
| 63 Domain:test-domain |
| 64 >>> |
| 65 |
| 66 The get_domain call has an optional validate parameter, which defaults to True.
This will make sure to raise |
| 67 an exception if the domain you are looking for doesn't exist. If you set it to f
alse, it will return a |
| 68 :py:class:`Domain <boto.sdb.domain.Domain>` object blindly regardless of its exi
stence. |
| 69 |
| 70 Getting Domain Metadata |
| 71 ------------------------ |
| 72 There are times when you might want to know your domains' machine usage, aprox.
item count and other such data. |
| 73 To this end, boto offers a simple and convenient way to do so as shown below:: |
| 74 |
| 75 >>> domain_meta = conn.domain_metadata(dom) |
| 76 >>> domain_meta |
| 77 <boto.sdb.domain.DomainMetaData instance at 0x23cd440> |
| 78 >>> dir(domain_meta) |
| 79 ['BoxUsage', 'DomainMetadataResponse', 'DomainMetadataResult', 'RequestId',
'ResponseMetadata', |
| 80 '__doc__', '__init__', '__module__', 'attr_name_count', 'attr_names_size', '
attr_value_count', 'attr_values_size', |
| 81 'domain', 'endElement', 'item_count', 'item_names_size', 'startElement', 'ti
mestamp'] |
| 82 >>> domain_meta.item_count |
| 83 0 |
| 84 >>> |
| 85 |
| 86 Please bear in mind that while in the example above we used a previously retriev
ed domain object as the parameter, you |
| 87 can retrieve the domain metadata via its name (string). |
| 88 |
| 89 Adding Items (and attributes) |
| 90 ----------------------------- |
| 91 Once you have your domain setup, presumably, you'll want to start adding items t
o it. |
| 92 In its most straight forward form, you need to provide a name for the item -- th
ink of it |
| 93 as a record id -- and a collection of the attributes you want to store in the it
em (often a Dictionary-like object). |
| 94 So, adding an item to a domain looks as follows:: |
| 95 |
| 96 >>> item_name = 'ABC_123' |
| 97 >>> item_attrs = {'Artist': 'The Jackson 5', 'Genera':'Pop'} |
| 98 >>> dom.put_attributes(item_name, item_attrs) |
| 99 True |
| 100 >>> |
| 101 |
| 102 Now let's check if it worked:: |
| 103 |
| 104 >>> domain_meta = conn.domain_metadata(dom) |
| 105 >>> domain_meta.item_count |
| 106 1 |
| 107 >>> |
| 108 |
| 109 |
| 110 Batch Adding Items (and attributes) |
| 111 ----------------------------------- |
| 112 You can also add a number of items at the same time in a similar fashion. All yo
u have to provide to the batch_put_attributes() method |
| 113 is a Dictionary-like object with your items and their respective attributes, as
follows:: |
| 114 |
| 115 >>> items = {'item1':{'attr1':'val1'},'item2':{'attr2':'val2'}} |
| 116 >>> dom.batch_put_attributes(items) |
| 117 True |
| 118 >>> |
| 119 |
| 120 Now, let's check the item count once again:: |
| 121 |
| 122 >>> domain_meta = conn.domain_metadata(dom) |
| 123 >>> domain_meta.item_count |
| 124 3 |
| 125 >>> |
| 126 |
| 127 A few words of warning: both batch_put_attributes() and put_item(), by default,
will overwrite the values of the attributes if both |
| 128 the item and attribute already exist. If the item exists, but not the attributes
, it will append the new attributes to the |
| 129 attribute list of that item. If you do not wish these methods to behave in that
manner, simply supply them with a 'replace=False' |
| 130 parameter. |
| 131 |
| 132 |
| 133 Retrieving Items |
| 134 ----------------- |
| 135 To retrieve an item along with its attributes is a fairly straight forward opera
tion and can be accomplished as follows:: |
| 136 |
| 137 >>> dom.get_item('item1') |
| 138 {u'attr1': u'val1'} |
| 139 >>> |
| 140 |
| 141 Since SimpleDB works in an "eventual consistency" manner, we can also request a
forced consistent read (though this will |
| 142 invariably adversely affect read performance). The way to accomplish that is as
shown below:: |
| 143 |
| 144 >>> dom.get_item('item1', consistent_read=True) |
| 145 {u'attr1': u'val1'} |
| 146 >>> |
| 147 |
| 148 Retrieving One or More Items |
| 149 ---------------------------- |
| 150 Another way to retrieve items is through boto's select() method. This method, at
the bare minimum, requires a standard SQL select query string |
| 151 and you would do something along the lines of:: |
| 152 |
| 153 >>> query = 'select * from `test-domain` where attr1="val1"' |
| 154 >>> rs = dom.select(query) |
| 155 >>> for j in rs: |
| 156 ... print 'o hai' |
| 157 ... |
| 158 o hai |
| 159 >>> |
| 160 |
| 161 This method returns a ResultSet collection you can iterate over. |
| 162 |
| 163 Updating Item Attributes |
| 164 ------------------------ |
| 165 The easiest way to modify an item's attributes is by manipulating the item's att
ributes and then saving those changes. For example:: |
| 166 |
| 167 >>> item = dom.get_item('item1') |
| 168 >>> item['attr1'] = 'val_changed' |
| 169 >>> item.save() |
| 170 |
| 171 |
| 172 Deleting Items (and its attributes) |
| 173 ----------------------------------- |
| 174 Deleting an item is a very simple operation. All you are required to provide is
either the name of the item or an item object to the |
| 175 delete_item() method, boto will take care of the rest:: |
| 176 |
| 177 >>>dom.delete_item(item) |
| 178 >>>True |
| 179 |
| 180 |
| 181 |
| 182 Deleting Domains |
| 183 ----------------------------------- |
| 184 To delete a domain and all items under it (i.e. be very careful), you can do it
as follows:: |
| 185 |
| 186 >>> conn.delete_domain('test-domain') |
| 187 True |
| 188 >>> |
OLD | NEW |