Java – create domain in Amazon simpledb
I'm using Amazon simpledb and trying to create a database using the following tutorial Basically, it throws an error: Java Lang.string cannot be cast to org.string apache. http. HttpHost. The complete stack trace is as follows:
My code is as follows (note that I have replaced AWS access ID and key with actual values):
public static void main(String[] args) { String awsAccessId = "My aws access id"; String awsSecretKey = "my aws secret key"; SimpleDB sdb = new SimpleDB(awsAccessId,awsSecretKey,true); try { Domain domain = sdb.createDomain("cars"); System.out.println(domain); } catch (com.xerox.amazonws.sdb.SDBException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Any idea of why the above error occurred
I appreciate any help
Solution
It seems that you are using the typica client library, which has hardly been maintained since mid-2011. For example, the latest one seems to belong to you. See ClassCastException using Apache httpclient 4.2:
>According to the reporter, once we downgrade back to Apache httpclient 4.1, things seem to work, so this may be the final temporary solution
Either way, I strongly recommend switching to the official AWS SDK for Java (or other language SDKs), which not only supports and maintains the normal way, but also closely tracks all AWS API changes (admittedly, this is not so critical. For Amazon simpledb, this is basically a technically wise freeze, but you can more easily use AWS products & services for too much time in the future
>In addition, if you are using an IDE, you can benefit from the AWS toolkit for eclipse
The SDK contains some examples (also available through the eclipse toolkit wizard), including simpledb – here is a concise code excerpt from your example:
BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials( awsAccessId,awsSecretKey); AmazonSimpleDB sdb = new AmazonSimpleDBClient(basicAWSCredentials); Region usWest2 = Region.getRegion(Regions.US_WEST_2); sdb.setRegion(usWest2); try { // Create a domain String myDomain = "MyStore"; System.out.println("Creating domain called " + myDomain + ".\n"); sdb.createDomain(new CreateDomainRequest(myDomain)); // ... // Delete a domain System.out.println("Deleting " + myDomain + " domain.\n"); sdb.deleteDomain(new DeleteDomainRequest(myDomain)); } catch (AmazonServiceException ase) { // ... } catch (AmazonClientException ace) { // ... }