如何创建带有列表属性的静态DynamoDB架构

huangapple go评论80阅读模式
英文:

How to create static DynamoDB schema with List attribute

问题

以下是翻译好的部分:

有一个类似这样的 Bean

    @DynamoDbBean
    public class Contact {
        private String id;
        private List<String> pid;
        // 获取器和设置器
    }

当我为它创建一个像这样的 `StaticTableSchema`:

    StaticTableSchema
        .builder(Contact.class)
        .newItemSupplier(Contact::new)
        .addAttribute(List.class, a -> 
            a
                .name("pid")
                .getter(Contact::getPid)
                .setter(Contact::setPid)
        )

然后尝试使用它

    DynamoDbTable<Contact> mappedTable = enhancedClient.table(tableName, Contact.TABLE_SCHEMA);

我得到了异常

java.lang.ExceptionInInitializerError
在 com.childcare.service.ContactServiceIntegrationTest.testFindByEmail(ContactServiceIntegrationTest.java:60) 中
...

如何在 StaticTableSchema 中正确定义一个 List 属性?


<details>
<summary>英文:</summary>

Having a bean like this:

    @DynamoDbBean
    public class Contact {
        private String id;
        private List&lt;String&gt; pid;
        // getters and setters
    }

When I create a `StaticTableSchema` for it like this:    

    StaticTableSchema
        .builder(Contact.class)
        .newItemSupplier(Contact::new)
        .addAttribute(List.class, a -&gt; 
            a
                .name(&quot;pid&quot;)
                .getter(Contact::getPid)
                .setter(Contact::setPid)
        )

And then trying to use it:

    DynamoDbTable&lt;Contact&gt; mappedTable = enhancedClient.table(tableName, Contact.TABLE_SCHEMA);

I get exception:

java.lang.ExceptionInInitializerError
at com.childcare.service.ContactServiceIntegrationTest.testFindByEmail(ContactServiceIntegrationTest.java:60)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0
at java.base/java.util.Collections$EmptyList.get(Collections.java:4553)
at software.amazon.awssdk.enhanced.dynamodb.DefaultAttributeConverterProvider.findConverter(DefaultAttributeConverterProvider.java:153)
at software.amazon.awssdk.enhanced.dynamodb.DefaultAttributeConverterProvider.converterFor(DefaultAttributeConverterProvider.java:133)
at software.amazon.awssdk.enhanced.dynamodb.mapper.ImmutableAttribute.converterFrom(ImmutableAttribute.java:167)
at software.amazon.awssdk.enhanced.dynamodb.mapper.ImmutableAttribute.resolve(ImmutableAttribute.java:163)
at software.amazon.awssdk.enhanced.dynamodb.mapper.StaticImmutableTableSchema.lambda$new$0(StaticImmutableTableSchema.java:153)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1624)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312)
at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:734)
at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658)
at software.amazon.awssdk.enhanced.dynamodb.mapper.StaticImmutableTableSchema.<init>(StaticImmutableTableSchema.java:159)
at software.amazon.awssdk.enhanced.dynamodb.mapper.StaticImmutableTableSchema.<init>(StaticImmutableTableSchema.java:77)
at software.amazon.awssdk.enhanced.dynamodb.mapper.StaticImmutableTableSchema$Builder.build(StaticImmutableTableSchema.java:425)
at software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema.<init>(StaticTableSchema.java:66)
at software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema.<init>(StaticTableSchema.java:64)
at software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema$Builder.build(StaticTableSchema.java:255)
at com.childcare.model.dyanmo.Contact.<clinit>(Contact.java:334)
... 25 more


What is the proper way to define a `List` attribute in `StaticTableSchema`?


</details>


# 答案1
**得分**: 2

```plaintext
对于 SDK v2,请使用以下代码:

    StaticTableSchema
                .builder(Contact.class)
                .newItemSupplier(Contact::new)
                .addAttribute(EnhancedType.listOf(String.class),
                              a -> a.name("pid")
                              .getter(Contact::getPid)
                              .setter(Contact::setPid)
                              .attributeConverter(ListAttributeConverter.builder(EnhancedType.listOf(String.class))
                              .collectionConstructor(ArrayList::new)                           
                              .elementConverter(StringAttributeConverter.create())
                              .build())))

您需要在其中提供 `ListAttributeConverter` 和 `StringAttributeConverter`,以便它知道如何转换其元素。
英文:

For SDK v2 use this:

StaticTableSchema
            .builder(Contact.class)
            .newItemSupplier(Contact::new)
            .addAttribute(EnhancedType.listOf(String.class),
                          a -&gt; a.name(&quot;pid&quot;)
                          .getter(Contact::getPid)
                          .setter(Contact::setPid)
                          .attributeConverter(ListAttributeConverter.builder(EnhancedType.listOf(String.class))
                          .collectionConstructor(ArrayList::new)                           
                          .elementConverter(StringAttributeConverter.create())
                          .build())))

you need to provide the ListAttributeConverter and the StringAttributeConverter inside it so it knows how to converts its elements

huangapple
  • 本文由 发表于 2020年10月20日 12:00:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64438277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定