5

我需要注释一个具有Set<UUID>属性的类。编组一个标量UUID很好,我可以使用@DynamoDBMarshalling注释来指定我的转换器,一切正常。

当尝试在 a 上使用相同的转换器时Set<UUID>,我得到“DynamoDBMappingException: Expected SS in value”。我尝试创建一个期望的自定义​​转换器Set<UUID>,但同样的问题仍然存在。

是否可以自定义编组集?

@DynamoDBTable(tableName="djones-test")
public class UUIDRecommendation {
    private UUID id;
    private Set<UUID> recommendations;

    @DynamoDBHashKey
    @DynamoDBMarshalling(marshallerClass=UuidConverter.class)
    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    //Neither this nor UuidConverter.class work here    
    @DynamoDBMarshalling(marshallerClass=SetUuidConverter.class)
    public Set<UUID> getRecommendations() {
        return recommendations;
    }

    public void setRecommendations(Set<UUID> recommendations) {
        this.recommendations = recommendations;
    }
}

这是堆栈跟踪:

com.amazonaws.services.dynamodb.datamodeling.DynamoDBMappingException: Expected SS in value {SS: [1a841b97-ab9d-4425-a2c0-f9a81bebf0b4, 1a841b97-ab9d-4425-a2c0-f9a81bebf0b4, 1a841b97-ab9d-4425-a2c0-f9a81bebf0b4], } when invoking public void com.company.model.UUIDRecommendation.setRecommendations(java.util.Set)
    at com.amazonaws.services.dynamodb.datamodeling.SUnmarshaller.typeCheck(SUnmarshaller.java:26)
    at com.amazonaws.services.dynamodb.datamodeling.DynamoDBMapper.setValue(DynamoDBMapper.java:329)
    at com.amazonaws.services.dynamodb.datamodeling.DynamoDBMapper.marshallIntoObject(DynamoDBMapper.java:302)
    at com.amazonaws.services.dynamodb.datamodeling.DynamoDBMapper.load(DynamoDBMapper.java:253)
    at com.amazonaws.services.dynamodb.datamodeling.DynamoDBMapper.load(DynamoDBMapper.java:196)
    at com.mendeley.service.data.DynamoRecommendedItemsDataServiceTest.testObjectMapper(DynamoRecommendedItemsDataServiceTest.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
4

3 回答 3

4

试试这个。它处理 getter/setter 内的编组/解组。我使用类似的技术来存储包含 JSON,但在我的代码中将Set<String>其作为 a 访问。Set<CustomObject>我不理会 getter/setter,而是使用方便的方法。

这是修改后的版本:

@DynamoDBTable(tableName="djones-test")
public class UUIDRecommendation {
    private String id;
    // custom object representation in JVM
    private Set<UUID> recommendations;

    private static final JsonMarshaller<UUID> UUID_MARSHALLER = new JsonMarshaller<UUID>();

    @DynamoDBHashKey(attributeName="id")
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    // Set<String> holding JSON in DynamoDB
    @DynamoDBAttribute(attributeName="recommendations")
    public Set<String> getRecommendations() {
        if (recommendations != null) {
            Set<String> jsonSet = new HashSet<String>(recommendations.size());
            for (UUID recommendation : recommendations) {
                String json = UUID_MARSHALLER.marshall(recommendation);
                jsonSet.add(json);
            }
            return jsonSet;
        } else {
            return null;
        }
    }
    public void setRecommendations(Set<String> jsonSet) {
        if (jsonSet != null) {
            recommendations = new HashSet<UUID>(jsonSet.size());
            for (String json : jsonSet) {
                UUID recommendation = UUID_MARSHALLER.unmarshall(UUID.class, json);
                recommendations.add(recommendation);
            }
        }
    }

    // convenience methods
    @DynamoDBIgnore
    public UUID convenienceMethod(UUID recommendation) {
        return null;
    }

    // custom object
    public class UUID {

    }
}
于 2013-03-23T10:26:22.610 回答
2

哦,亲爱的 - 看起来客户编组是不可能的。AWS 开发工具包经过硬编码,仅处理单个字符串值 (SUnmarshaller())。

AWS SDK 1.3.13 中的 DynamoDBReflector,第 185 行:

if ( isCustomMarshaller(getter) ) {
    unmarshaller = new SUnmarshaller() {

        @Override
        public Object unmarshall(AttributeValue value) {
            return getCustomMarshalledValue(toReturn, getter, value);
        }
    };
}

更新

作为一个完全肮脏的黑客,我通过复制/粘贴整个类来拼凑一些东西:(这就是为什么私有静态最终实用程序服务不好,而依赖注入很好

这将起作用,因此您可以将一个客户转换器(UuidConverter在我的情况下)用于返回UUID实例 Set<UUID>实例的 getter。

我添加了一个名为 getCustomMarshalledValueSet 的方法,它遍历 value.getSS() 返回的 List,为每个调用自定义编组器,并将结果添加到它返回的 Set 中。

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private <T> T getCustomMarshalledValueSet(T toReturn, Method getter, AttributeValue value) {
    DynamoDBMarshalling annotation = getter.getAnnotation(DynamoDBMarshalling.class);
    Class<? extends DynamoDBMarshaller<? extends Object>> marshallerClass = annotation.marshallerClass();

    DynamoDBMarshaller marshaller;
    try {
        marshaller = marshallerClass.newInstance();
    } catch (InstantiationException e) {
        throw new DynamoDBMappingException("Couldn't instantiate marshaller of class " + marshallerClass, e);
    } catch (IllegalAccessException e) {
        throw new DynamoDBMappingException("Couldn't instantiate marshaller of class " + marshallerClass, e);
    }

    Set<T> set = new HashSet<T>();

    for (String part : value.getSS()) {
        set.add((T) marshaller.unmarshall(getter.getReturnType(), part));
    }

    return (T) set;
}

此外,更改了 getArgumentUnmarshaller 以在条件之前进行 isCollection 检查以决定使用哪种类型的解组器,并更改了自定义编组器块以选择正确的排序。

            if (isCustomMarshaller(getter)) {
                if (isCollection) {
                    unmarshaller = new SSUnmarshaller() {

                        @Override
                        public Object unmarshall(AttributeValue value) {
                            return getCustomMarshalledValueSet(toReturn, getter, value);
                        }
                    };
                } else {
                    unmarshaller = new SUnmarshaller() {

                        @Override
                        public Object unmarshall(AttributeValue value) {
                            return getCustomMarshalledValue(toReturn, getter, value);
                        }
                    };
                }

            }
于 2012-08-31T15:04:54.733 回答
1

请参阅此拉取请求,我附加了一个修改版本的 DynamoDBReflector,它处理一组任意对象的序列化/反序列化。我已经使用了一段时间,效果很好。

于 2013-05-28T16:42:35.930 回答