The Wayback Machine - https://web.archive.org/web/20100917060813/http://alan.blog-city.com/simpledb_sqs_java.htm

alanwilliamson

Amazon SimpleDB + SQS : Simple Java POJO Access

I have made some updates to the two classes I released that let you access Amazon's SimpleDB and SQS services.

SimpleDB features

  • No external dependencies
  • Single POJO
  • Full API support; CreateDomain, DeleteDomain, DomainMetaData, select, GetAttributes, PutAttributes, BatchPutAttributes, DeleteAttributes
  • NextToken support
  • Signature2 authentication
  • Error Reporting
  • Last Request ID and BoxUsage reporting

SimpleSQS features

  • No external dependencies
  • Single POJO
  • Full API support; CreateQueue, DeleteQueue, ListQueues, DeleteMessage, SendMessage, ReceiveMessage, GetAttributes, ChangeMessageVisibility, AddPermission, RemovePermission
  • Signature2 authentication
  • Error Reporting
  • Public Domain license

Error Reporting has improved across both classes. I have also changed the HTTP method for SimpleDB when pushing content up to Amazon from GET to a POST. This lets you post more data than the previous GET URL length would permit.

More significantly, I have removed the dependency for Sun's Base64 encoding class, allowing these simple classes to be used within Google's AppEngine infrastructure.

To ease deployment, I have added both classes to a single .ZIP that you can download in its entirety. As before they require no external JAR files, and do not require each other, so you can simple delete the one you are not interested in. As before, all files are in the public domain, so no warranty, no assurances, just a whole bunch of good will and faith. Enjoy.

I will keep this entry updated with all future changes.

download here (September 2009 - 24KB)

Comments

I added a bit of code to the Select method in your SimpleDB.java code:

It is kind of a hack, but I needed to get the token back in my Hashmap without having to make another call.

Again, thanks for sharing your good work!

try { HashMap<String, String>(); List myNextTokenList = getElements(resp, "NextToken"); Integer tokenLength = myNextTokenList.size(); if (tokenLength > 0) { System.out.println("got token"); String myNextToken = myNextTokenList.get(0).toString(); if (!myNextToken.isEmpty()) { String[] myNextTokenStringArray = { myNextToken }; nextTokenHashMap.put("NextToken", myNextTokenStringArray); resultList.add(nextTokenHashMap); } } } catch (Exception ex) { System.out.println("EXCEPTION IN SIMPLEDB-ADD TOKEN");

Here's the complete method: public List select(String selectExpression) throws Exception { return select(selectExpression, null); }

public List select(String selectExpression, String nextToken) throws Exception { TreeMap uriParams = createStandardParams("Select"); uriParams.put("SelectExpression", selectExpression);

if (nextToken != null) uriParams.put("NextToken", nextToken);

uriParams.put("Signature", getSignature(uriParams));

/* Make the call */ URL url = getUrl(uriParams); HttpURLConnection con = (HttpURLConnection) url.openConnection(); if (con.getResponseCode() >= 300) { handleErrorAndThrow(getString(con.getErrorStream(), "utf-8")); }

String resp = getString(con.getInputStream(), "utf-8"); readResponse(resp);

List<HashMap> resultList = new ArrayList<HashMap>();

List itemList = getElements(resp, "Item"); for (int x = 0; x < itemList.size(); x++) { String i = itemList.get(x).toString();

HashMap<String, String>();

List nameId = getElements(i, "Name"); map.put("ItemName", new String[] { nameId.get(0).toString() });

List attributes = getElements(i, "Attribute"); for (int xx = 0; xx < attributes.size(); xx++) { String t = attributes.get(xx).toString();

String key = t.substring(t.indexOf("<Name>") + 6, t.indexOf("</Name>")); String val = t.substring(t.indexOf("<Value>") + 7, t.indexOf("</Value>"));

if (map.containsKey(key)) { String) map.get(key); String; System.arraycopy(oldA, 0, newA, 0, oldA.length); newA = val; map.put(key, newA); } else { map.put(key, new String[] { val }); } }

resultList.add(map); } try { HashMap<String, String>(); List myNextTokenList = getElements(resp, "NextToken"); Integer tokenLength = myNextTokenList.size(); if (tokenLength > 0) { System.out.println("got token"); String myNextToken = myNextTokenList.get(0).toString(); if (!myNextToken.isEmpty()) { String[] myNextTokenStringArray = { myNextToken }; nextTokenHashMap.put("NextToken", myNextTokenStringArray); resultList.add(nextTokenHashMap); } } } catch (Exception ex) { System.out.println("EXCEPTION IN SIMPLEDB-ADD TOKEN"); }

return resultList; }
left by John Middendorf — Sunday, 25 April 2010 12:52 AM — web site

Fantastic! Just what I needed. Thank you! It looks like you can just call this code from a Google App Engine Servlet? Awesome.

left by John Middendorf — Monday, 12 April 2010 3:15 AM — web site

Leave Comment

please note, all comments will be moderated for spam and abuse before being publicly posted.


 

Recent Cloud posts

Recent JAVA posts

Latest CFML posts


 
Site Links
Recommended Sites/Blogs

Follow javachampion on Twitter