simple tutorial for using SimpleDB actionscript library

Below are some code snippets illustrating how to use the library I created for accessing Amazon.com’s SimpleDB via Adobe AIR. To use the library you must obtain the excellent AS3 Crypto library.


First let’s start with instantiating the class and configuring it:


private function onCreationComplete():void
{
     var sdbRef:AWSSimpleDB = new AWSSimpleDB();
         sdbRef.setAWSCredentials("yourAWSKey","yourAWS_secretKey");//this only needs to be set once the variables are static
         sdbRef.setSignatureVersion(AWSSimpleDB.SIGNATURE_VERSION_2);//optional, lib. defaults to sig. ver. 2

     //to use M/DB add the following:
     sdbRef.useMDB(true,192.168.254.7);//obviously change the IP address to your environment
}

Declare the following in your class:

private namespace amazonSimpleDBNS = "http://sdb.amazonaws.com/doc/2007-11-07/";

To get list of domains in SimpleDB:

public function getListOfDomainNames(nextToken:String = null):void
{
     var simpleDB:AWSSimpleDB = new AWSSimpleDB();
     simpleDB.addEventListener(SimpleDBEvent.DOMAINS_LISTED,domainListReceived);
     simpleDB.addEventListener(SimpleDBEvent.AWS_SIMPLEDB_ERROR,hanldeSimpleDBRequestError);
     simpleDB.listAllDomains(nextToken);

     function domainListReceived(event:SimpleDBEvent):void
     {
          use namespace amazonSimpleDBNS;
          var domainList:XMLList = event.rawAmazonResponse.ListDomainsResult.DomainName;
          for each(var element:XML in domainList)
          {
                trace(element.toString());
          }
     }

     function hanldeSimpleDBRequestError(event:SimpleDBEvent):void
     {
          trace(event.errorText);
     }

}

To get domain meta data:

public function getDomainMetaData(domainName:String):void
{
     var simpleDB:AWSSimpleDB = new AWSSimpleDB();
     simpleDB.addEventListener(SimpleDBEvent.DOMAIN_METADATA_RECEIVED,domainMetaDataReceviedEvent);
     simpleDB.addEventListener(SimpleDBEvent.AWS_SIMPLEDB_ERROR,hanldeSimpleDBRequestError);
     simpleDB.getDomainMetaData(domainName);

     function domainMetaDataReceviedEvent(event:SimpleDBEvent):void
     {
          var domainMetaDataObj:DomainMetaData = new DomainMetaData();
          use namespace amazonSimpleDBNS;
          var dataList:XMLList = event.rawAmazonResponse.DomainMetadataResult;
          if(dataList != null && dataList.length() > 0)
          {
               domainMetaDataObj.attributeNameCount = dataList[0].AttributeNameCount.toString();
               domainMetaDataObj.attributeNamesSizeBytes = dataList[0].AttributeNamesSizeBytes.toString();
               domainMetaDataObj.attributeValueCount = dataList[0].AttributeValueCount.toString();
               domainMetaDataObj.attributeValuesSizeBytes = dataList[0].AttributeValuesSizeBytes.toString();
               domainMetaDataObj.dateLastUpdated = dataList[0].Timestamp.toString();
              domainMetaDataObj.itemCount = dataList[0].ItemCount.toString();
              domainMetaDataObj.itemNamesSizeBytes = dataList[0].ItemNamesSizeBytes.toString();
          }
     }

     function hanldeSimpleDBRequestError(event:SimpleDBEvent):void
     {
          trace(event.errorText);
     }
}

To add a domain to SimpleDB

     public function addNewDomain(domainName:String):void
     {
          var simpleDB:AWSSimpleDB = new AWSSimpleDB();
          simpleDB.addEventListener(SimpleDBEvent.DOMAIN_CREATED,domainCreatedEvent);
          simpleDB.addEventListener(SimpleDBEvent.AWS_SIMPLEDB_ERROR,hanldeSimpleDBRequestError);
          simpleDB.createDomain(domainName);

          function domainCreatedEvent(event:SimpleDBEvent):void
          {
               //do something useful
          }
          function hanldeSimpleDBRequestError(event:SimpleDBEvent):void
          {
                //similar to examples above
          }
     }

To delete a domain from SimpleDB do the same as above except call deleteDomain(domainName);

To get all the items in a SimpleDB domain:

public function getAllItemsForDomain(domainName:String,nextToken:String = null):void
{
     var simpleDB:AWSSimpleDB = new AWSSimpleDB();
     simpleDB.addEventListener(SimpleDBEvent.QUERY_COMPLETED,itemsReceived);
     simpleDB.addEventListener(SimpleDBEvent.AWS_SIMPLEDB_ERROR,hanldeSimpleDBRequestError);
     simpleDB.queryForItems(domainName,MAX_ITEMS_TO_RETURN,nextToken);

     function itemsReceived(event:SimpleDBEvent):void
     {
          use namespace amazonSimpleDBNS;
          var itemList:XMLList = event.rawAmazonResponse.QueryResult.ItemName;
          for each(var item:XML in itemList)
               trace(item.toString());
     }

     function hanldeSimpleDBRequestError(event:SimpleDBEvent):void
     {
          //similar to examples above
     }

}

To get all the attributes for an item:

public function getItemAttributes(domainName:String,itemName:String):void
{
     var simpleDB:AWSSimpleDB = new AWSSimpleDB();
     simpleDB.addEventListener(SimpleDBEvent.ATTRIBUTES_RECEIVED,attributesReceviedEvent);
     simpleDB.addEventListener(SimpleDBEvent.AWS_SIMPLEDB_ERROR,hanldeSimpleDBRequestError);
     simpleDB.getAttributes(domainName,itemName);

     function attributesReceviedEvent(event:SimpleDBEvent):void
     {
          //returns array of SimpleDBAttributeObject objects
          var arr:Array = event.getAttributesFromRequest();
     }

     function hanldeSimpleDBRequestError...//similar to above
}

To add attributes to a SimpleDB item

//currently, @replace is applied to all attributes in the @attributes array.  more granular control will be added in the future.
//@attributes should be an array of SimpleDBAttributeObject objects
public function addAttributeToItem(domainName:String,itemName:String,attributes:Array,replace:Boolean):void
{
     var simpleDB:AWSSimpleDB = new AWSSimpleDB();
     simpleDB.addEventListener(SimpleDBEvent.ATTRIBUTES_PUT,attributesAddedEvent);
     simpleDB.addEventListener(SimpleDBEvent.AWS_SIMPLEDB_ERROR,hanldeSimpleDBRequestError);
     simpleDB.putAttributes(domainName,itemName,attributes,replace);

     function attributesAddedEvent(event:SimpleDBEvent):void
     {
           //
     }

     function hanldeSimpleDBRequestError...
}

To remove attributes from an item

public function removeAttribute(domainName:String,itemName:String,attributes:Array):void
{
     var simpleDB:AWSSimpleDB = new AWSSimpleDB();
     simpleDB.addEventListener(SimpleDBEvent.ATTRIBUTES_REMOVED,attributesRemovedEvent);
     simpleDB.addEventListener(SimpleDBEvent.AWS_SIMPLEDB_ERROR,hanldeSimpleDBRequestError);
     simpleDB.removeAttributes(domainName,itemName,attributes);

     function attributesRemovedEvent(event:SimpleDBEvent):void
     {}
     function hanldeSimpleDBRequestError(event:SimpleDBEvent):void
     {}

}

To query for items in SimpleDB

//@AttributeName  is the name of the attribute to return. To return multiple attributes,
//you can specify this request parameter multiple times.
//if @attributesToFetch is null, all attributes will be returned.
//@attributesToFetch should be an array of SimpleDBAttributeObject objects
//@queryString should be in the form: AttributeName.1 = ['foo' = 'bar']
//see the Amazon's query documentation.  as of this writing the
//'Select...' query syntax is not supported in ver. 1 of this library
//you can use the SimpleDBQueryObject class as a helper in getting the syntax right
//I don't know why I don't have a method that takes an array of these as a parameter,
//I must have gotten distracted.  I'll add this in a future revision.
public function runQueryWithAttributes(domainName:String,attributesToFetchArr:Array,queryString:String,nextToken:String = null):void
{
     var simpleDB:AWSSimpleDB = new AWSSimpleDB();
     simpleDB.addEventListener(SimpleDBEvent.QUERY_WITH_ATTRIBUTES_COMPLETED,queryFinishedEvent);
     simpleDB.addEventListener(SimpleDBEvent.AWS_SIMPLEDB_ERROR,hanldeSimpleDBRequestError);
     simpleDB.queryForAttributes(domainName,attributesToFetchArr,MAX_ITEMS_TO_RETURN,nextToken,queryString);

     function hanldeSimpleDBRequestError(event:SimpleDBEvent):void
     {}

     function queryFinishedEvent(event:SimpleDBEvent):void
     {
          var boxUsage:String = event.getRequestBoxUsage();

          //you can get the next token using the code below, this applies to other
          //requests not just query requests
          var tokList:XMLList = event.rawAmazonResponse.QueryResult.NextToken;

          use namespace amazonSimpleDBNS;

          var itemList:XMLList = event.rawAmazonResponse.QueryWithAttributesResult.Item;
          for each(var item:XML in itemList)
          {
                trace(item.Name.toString());
               var attribList:XMLList = item.Attribute;
               for each(var attrib:XML in attribList)
               {
                     trace(attrib.Name.toString());
                     trace(attrib.Value.toString());
               }
          }
     }
}

Check out my SimpleDB devlopment tool Bolso.

It is an easy way to see what’s in your simpleDB as well as testing queries.



Download the SimpleDB library for Adobe AIR / Actionscript here.

***UPDATE: The latest source code for this SimpleDB Actionscript library can now be accessed via google code.