Java - 在Amazon SimpleDB中创建域

时间:2013-04-03 17:01:36

标签: java amazon-simpledb

我正在使用Amazon SimpleDB并尝试使用以下教程创建数据库。基本上它会抛出一个错误,即出现错误:java.lang.String无法强制转换为org.apache.http.HttpHost。完整的堆栈跟踪如下:

  

发生错误:java.lang.String无法强制转换为org.apache.http.HttpHost   java.lang.ClassCastException:java.lang.String无法强制转换为org.apache.http.HttpHost       在org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:416)       at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)       在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)       在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)       在com.xerox.amazonws.common.AWSQueryConnection.makeRequest(AWSQueryConnection.java:474)       在com.xerox.amazonws.sdb.SimpleDB.makeRequestInt(SimpleDB.java:231)       在com.xerox.amazonws.sdb.SimpleDB.createDomain(SimpleDB.java:155)       在com.amazonsimpledb.SDBexample1.main(SDBexample1.java:19)

我的代码如下(注意我已将AWS访问ID和密钥替换为实际值):

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();
    }
}

有关上述错误发生原因的任何想法。

我感谢任何帮助。

2 个答案:

答案 0 :(得分:7)

您似乎正在使用Typica客户端库,自2011年中期以来几乎没有维护,请参阅例如rare commmitssteady growing unresolved issues,其中最新的一个看起来与您的完全一致,请参阅ClassCastException using Apache HttpClient 4.2

  • 根据记者的说法,一旦我们降级回Apache HttpClient 4.1 ,似乎正常运行,因此最终可能是暂时的解决方法。

无论哪种方式,我强烈建议切换到官方AWS SDK for Java(或其他语言SDKs之一),这不仅支持和维护,而且还密切跟踪所有AWS API更改(诚然,这对Amazon SimpleDB来说并不重要,这基本上是冻结技术,但是稍后使用过多的AWS Products & Services会更容易。

SDK包含一些示例(也可以通过Eclipse Toolkit向导获得),其中包括SimpleDB - 这里是关于您的示例的简要代码摘录:

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) {
    // ...
}

答案 1 :(得分:0)

请尝试使用服务器和端口创建SimpleDB实例,并告诉我它是否有效。

public SimpleDB objSimpleDB = null;
private String awsAccessKeyId = "access key";
private String awsSecretAccessKey = "secret key";
private boolean isSecure= true;
private String server = "sdb.amazonaws.com";
private int port=443;

try{
SimpleDB objSimpleDB = new SimpleDB(awsAccessKeyId, awsSecretAccessKey, isSecure, server, port);
Domain domain = objSimpleDB .createDomain("cars");
} catch (com.xerox.amazonws.sdb.SDBException e) {
      //handle error
}