2016-12-06 50 views
0

我正在嘗試將對象保存到DynamoDB中。我知道最簡單的方法是用@DynamoDBDocument註釋對象。 但是,就我而言,我想要保存的對象屬於我無法修改的包。在java應用程序中將導入的對象保存到DynamoDB中

我正在使用java sdk。

import not.my.package.Outsider; 

@DynamoDBTable(tableName = "DynamoTable") 
public class DynamoTable { 
    private Outsider outsider; 
    //getters... 
    //setters... 
} 

關於如何保存這些對象的任何想法?我們不想將它們保存爲字符串,因爲我們正在使用Dynamo to SQL插件來實現業務目的。 謝謝。

+0

顯示樣本數據。 – notionquest

+0

@notionquest - 我已經添加了一個代碼示例。這有幫助嗎? –

回答

0

首先,OP沒有任何有關分區鍵和排序鍵的信息。以下代碼使用註釋@DynamoDBAutoGeneratedKey自動生成分區鍵。你可以根據你的用例來改變它。

Order類 - 類似DynamoTable

@DynamoDBTable(tableName = "Order") 
public class Order implements Serializable { 

    private static final long serialVersionUID = -3534650012619938612L; 

    private String orderId; 

    private String productName; 

    private Integer createDate; 

    private Outsider outsider; 

    @DynamoDBHashKey(attributeName = "orderId") 
    @DynamoDBAutoGeneratedKey 
    public String getOrderId() { 
     return orderId; 
    } 

    public void setOrderId(String orderId) { 
     this.orderId = orderId; 
    } 

    @DynamoDBAttribute(attributeName = "productName") 
    public String getProductName() { 
     return productName; 
    } 

    public void setProductName(String productName) { 
     this.productName = productName; 
    } 

    @DynamoDBAttribute(attributeName = "createDate") 
    public Integer getCreateDate() { 
     return createDate; 
    } 

    public void setCreateDate(Integer createDate) { 
     this.createDate = createDate; 
    } 

    @DynamoDBAttribute(attributeName = "outsider") 
    public Outsider getOutsider() { 
     return outsider; 
    } 

    public void setOutsider(Outsider outsider) { 
     this.outsider = outsider; 
    } 

} 

局外人類: -

在外人類的屬性將被保存在DynamoDB表地圖屬性。

@DynamoDBDocument 
public class Outsider implements Serializable{ 

    private static final long serialVersionUID = 4449726365885112352L; 

    private String firstName; 
    private String lastName; 

    public String getFirstName() { 
     return firstName; 
    } 
    public String getLastName() { 
     return lastName; 
    } 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 
    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

} 

代碼來保存數據: - 只要你有dynamoDBClient對象

此代碼應工作。我用Spring將對象注入到我的服務類中。有多種方式。

public Boolean createOrderWithOutsider(String productName, Outsider outsider) { 

    DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(dynamoDBClient); 

    Order order = new Order(); 
    order.setProductName(productName); 
    order.setOutsider(outsider); 

    dynamoDBMapper.save(order); 

    System.out.println("Order id : " + order.getOrderId()); 

    return true; 

} 

測試代碼: -

@Test 
public void createOrderWithOutsider() { 

    Outsider outsider = new Outsider(); 
    outsider.setFirstName("John"); 
    outsider.setLastName("Micheal"); 

    Assert.isTrue(tableOperations.createOrderWithOutsider("Pepsi", outsider)); 
} 

連接樣品: -

<bean id="amazonDynamoDB" class="com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient"> 
    <constructor-arg ref="amazonAWSCredentials" /> 
    <property name="endpoint" value="${amazon.dynamodb.endpoint}" /> 
</bean> 

自動裝配Autowired在服務類: -

保存在表

樣本數據: -

Sample item saved in DynamoDB

+0

正如我在我的問題中提到的,我無法修改Outsider對象。在您的回覆中,您將@DynamoDBDocument添加到Outsider類 - 我無法修改的類。 –

相關問題