DynamoDBMapper表生成仅创建索引

时间:2016-09-03 20:06:12

标签: java amazon-dynamodb

我正在尝试使用Java API在DynamoDB中创建表。

问题:它只在表中创建散列键索引而没有其他属性。

private void createTable(DynamoDBMapper mapper, AmazonDynamoDBClient amazonDynamoDBClient) {
    CreateTableRequest createTableRequest = mapper.generateCreateTableRequest(InsuranceData.class);
    createTableRequest.setProvisionedThroughput(new ProvisionedThroughput(25L, 25L));
    amazonDynamoDBClient.createTable(createTableRequest);
}


 private void run(){
    AmazonDynamoDBClient amazonDynamoDBClient = getDynamoDBLocalClient();
    DynamoDBMapper mapper = new DynamoDBMapper(amazonDynamoDBClient);
    InsuranceData insuranceData = new InsuranceData();
    createTable(mapper,amazonDynamoDBClient);
 }

POJO class

@DynamoDBTable(tableName = "InsuranceData")
public class InsuranceData {


private Integer siteId;
private String lob;
private InsuranceLobData multiItemLobData;
private InsuranceLobData standaloneLobData;

@DynamoDBHashKey(attributeName = "siteId")
public Integer getSiteId() {
    return siteId;
}

public void setSiteId(Integer siteId) {
    this.siteId = siteId;
}

@DynamoDBAttribute(attributeName = "F")
public String getLob() {
    return lob;
}

public void setLob(String lob) {
    this.lob = lob;
}

@DynamoDBTypeConverted(converter = InsuranceLobConverter.class)
public InsuranceLobData getMultiItemLobData() {
    return multiItemLobData;
}

public void setMultiItemLobData(InsuranceLobData multiItemLobData) {
    this.multiItemLobData = multiItemLobData;
}

@DynamoDBTypeConverted(converter = InsuranceLobConverter.class)
public InsuranceLobData getStandaloneLobData() {
    return standaloneLobData;
}

public void setStandaloneLobData(InsuranceLobData standaloneLobData) {
    this.standaloneLobData = standaloneLobData;
}
}

Converter

public class InsuranceLobConverter implements DynamoDBTypeConverter<String, InsuranceLobData> {
  private final static Gson gson = new Gson();


@Override
public String convert(InsuranceLobData object) {
    return gson.toJson(object);
}

@Override
public InsuranceLobData unconvert(String object) {
    return gson.fromJson(object, InsuranceLobData.class);
}

}

有人能在这看到问题吗?

此问题已解决

我存储复杂对象InsuranceLobData的方式存在问题。

@DynamoDBDocument
public class InsuranceLobData   {

private boolean isActive;
private Set<String> locale;
private AbacusData abacusData;
private boolean isActiveForMobile;
private boolean isActiveForDesktop;

@Override
public String toString() {
    return "InsuranceLobData{" +
            "isActive=" + isActive +
            ", locale=" + locale +
            ", abacusData=" + abacusData +
            ", isActiveForMobile=" + isActiveForMobile +
            ", isActiveForDesktop=" + isActiveForDesktop +
            '}';
}

public InsuranceLobData(){}


public InsuranceLobData(boolean isActive, Set<String> locale, AbacusData abacusData, boolean isActiveForMobile, boolean isActiveForDesktop) {
    this.isActive = isActive;
    this.locale = locale;
    this.abacusData = abacusData;
    this.isActiveForMobile = isActiveForMobile;
    this.isActiveForDesktop = isActiveForDesktop;
}

@DynamoDBAttribute(attributeName = "isActive")
public boolean isActive() {
    return isActive;
}

public void setActive(boolean active) {
    isActive = active;
}

@DynamoDBAttribute(attributeName = "locale")
public Set<String> getLocale() {
    return locale;
}

public void setLocale(Set<String> locale) {
    this.locale = locale;
}

@DynamoDBTypeConvertedJson
public AbacusData getAbacusData() {
    return abacusData;
}

public void setAbacusData(AbacusData abacusData) {
    this.abacusData = abacusData;
}

@DynamoDBAttribute(attributeName = "mobile")
public boolean isActiveForMobile() {
    return isActiveForMobile;
}
public void setActiveForMobile(boolean activeForMobile) {
    isActiveForMobile = activeForMobile;
}

@DynamoDBAttribute(attributeName = "desktop")
public boolean isActiveForDesktop() {
    return isActiveForDesktop;
}


public void setActiveForDesktop(boolean activeForDesktop) {
    isActiveForDesktop = activeForDesktop;
}

}

无需添加DynamoDBDocument注释。 <删除> @DynamoDBDocument

2 个答案:

答案 0 :(得分:1)

static void Main(string[] args)
{
    string bigString, littleString;
    littleString = null;
    bigString = "word " + littleString.ToString() + " word";
}

足以让DynamoDB了解我想要在数据库中存储哪种数据。可能会将注释@DynamoDBTypeConverted(converter = InsuranceLobConverter.class) 添加到@DynamoDBDocument,这让人很难理解它存储数据的方式。

因此,删除注释InsuranceLobData对我有用。

希望它也有助于其他人。

答案 1 :(得分:0)

如果我理解正确,你的问题是 - 创建表后为什么你只看到“siteId”属性而不是表中的“F”?这是预料之中的。由于DynamoDB是NoSQL数据库,因此空表仅显示分区键和排序键(如果存在)作为属性。

如果表格中至少有一个项目,您将能够看到“F”属性。