Getting started with SimpleDB using Cloudfork
The Cloudfork-AWS project has classes to use the Amazon Web Services Simple Database (SimpleDB) directly from Smalltalk. Using these classes, you can create domains, put items, and query items even using regular Smalltalk blocks. Cloudfork contains the CFSimpleDB class that makes the generic calls available as Smalltalk methods. Calls that are related to a domain are implemented in the CFSimpleDBDomain class.
SimpleDB
In short, SimpleDB items are stored in a domain which has a name. Each item has a name and a collection of attributes. Each attribute has a name and one or more values. Values can be String only ; the application must take care of conversion. SimpleDB provides a database in the cloud that supports large volumes of data that can be accessed anywhere on the Internet. Amazon Web Services (AWS) takes care of high availability, consistency, indexing and performance.
Before throwing away your current persistency, it is important to realize that SimpleDB is not a relational database. It does not provide relational consistency (constraints), does not have a schema and “records” contain String values only. Queries on items require expressions that have limited operators; no joins, no subselects (see documentation).
Use case
Besides being a simple object database, the service can be used to store reference data (e.g. zipcode tables, gps locations, currencies) or logging information (audits). Another example is storing social user profiles which typically have a variable set of properties. SimpleDB can also be used for storing metadata and references to S3 objects such as images,video and documents. Because a S3 object can contain any data, it can be used to store large attribute values that do not fit into a SimpleDB item.
Smalltalk
After subscribing to the SimpleDB services with your AWS account, you must create the credentials object:
awsCredentials := CFAWSCredentials newWith: '<your access key>' andSecret: '<your secret access key>'.
Create a SimpleDB domain
To store items we need a domain, let us create one. Every api call returns a CFAWSResponse which must be checked for errors before using its result.
sdb := CFSimpleDB newWith: awsCredentials. response := sdb createDomain: 'zipcodes'. response isError ifFalse:[domain := response result]
Add items
The variable “domain” will be an instance of CFSimpleDBDomain that has various methods to access its items. For convenience, class CFSimpleDBItem can be used to encapsulate the name of the item and its attributes (name,value pairs).
item := CFSimpleDBItem newNamed: '3768GX'. item valueAt: 'city' put: 'Soest'. item valueAt: 'country' put: 'Netherlands'. domain putItem: item.
Query items using expressions
AWS SimpleDB offers two sets of api calls that support criteria-based retrieval. You can use the query syntax:
domain query: '[''country'' = ''Netherlands'']'.
Or the select expressions:
sdb select: 'select city from zipcodes'.
More details on quering using “select” and “query” can be found at the AWS SimpleDB documentation.
Query items using Block
Cloudfork has classes that support the use of normal Smalltalk blocks to define the select condition. See the documentation for all possible operators and functions and class CFSSWOperand for the Smalltalk counterpart.
domain selectAllWhere: [:each | each country = 'Netherlands'].
Delete a SimpleDB domain
Deleting a domain will also delete all its items. No warning here.
sdb deleteDomain: 'zipcodes'.
This post showed you the basic usage of the Cloudfork SimpleDB services. Browse the classes in this package to see how other SimpleDB API services are mapped to Smalltalk messages. Also have a look at the CFSimpleDBEmulator which can used for Unit testing classes that use Cloudfork-AWS SimpleDB.
If you are planning to use SimpleDB as an object database, then have a look at Cloudfork-ActiveItem. It is a framework that can help in mapping your objects to SimpleDB items and takes care of String conversions, data sharding and can handle associations similar to Rails ActiveRecord.
Tags: activeitem, aws, database, dictionary, relational, simpledb
You can comment below, or link to this permanent URL from your own site.