spring Java DynamoDB:在实体类中添加项而不声明字段

vcirk6k6  于 12个月前  发布在  Spring
关注(0)|答案(2)|浏览(132)

下面的示例将Map并添加条目到DynamoDB表中。我认为NoSQL的目的是允许字段,而无需在数据结构实体中指定结构/类型。
如何在DynamoDB中添加字段而不指定实际的Entity Type?(在本例中,是“Person”Entity类)。建议设置Postman以允许随机的JSON元素。
我想在这个Json中添加任何字段,这些字段不在Entity类中。例如,“StreetAddress”,“Car”,“phoneNumber”,

DynamoDBConfig

@RequiredArgsConstructor
@Configuration
public class DynamoDbConfig {

    @Bean
    public DynamoDBMapperConfig dynamoDBMapperConfig() {
        return DynamoDBMapperConfig.DEFAULT;
    }

    @Bean
    public DynamoDBMapper mapper() {
        return new DynamoDBMapper(amazonDynamoDBConfig());
    }

    @Bean
    public AmazonDynamoDB amazonDynamoDBConfig() {
        return AmazonDynamoDBClientBuilder.standard()
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("dynamodb.us-west-2.amazonaws.com", "us-west-2"))
                .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("TestExample", "ExampleExample")))
                .build();
    }
}

字符串

实体

@Data
@AllArgsConstructor
@NoArgsConstructor
@DynamoDBTable(tableName="person")
public class Person implements Serializable {
    @DynamoDBHashKey(attributeName = "personId")
    @DynamoDBAutoGeneratedKey
    private String personId;

    @DynamoDBAttribute
    private String name;

    @DynamoDBAttribute
    private int age;

    @DynamoDBAttribute
    private String email;
}

控制器:

@RestController
@RequestMapping("/api")
@CrossOrigin
public class PersonController {
    private PersonRepository repository;

    @Autowired
    public PersonController(PersonRepository repository) {
        this.repository = repository;
    }

    @PostMapping("/savePerson")
    public Person savePerson(@RequestBody Person person) {
        return repository.addPerson(person);
    }
}

仓库:

@Repository
public class PersonRepository {
    private DynamoDBMapper mapper;

    @Autowired
    public PersonRepository(DynamoDBMapper mapper) {
        this.mapper = mapper;
    }

    public Person addPerson(Person person) {
        mapper.save(person);
        return person;
    }
}

Postman测试:

我想在这个Json中添加任何字段,这些字段不在Entity类中。例如,StreetAddress,Car,phoneNumber,
x1c 0d1x的数据

83qze16e

83qze16e1#

目前还不清楚你到底想要什么样的行为,但我相信DynamoDBIgnore会满足你的要求:
向DynamoDBMapper示例指示应忽略关联的属性。将数据保存到表时,DynamoDBMapper不会将此属性保存到表。
应用于非建模属性的getter方法或类字段。如果注解直接应用于类字段,则相应的getter和setter必须在同一个类中声明
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.Annotations.html#DynamoDBMapper.Annotations.DynamoDBIgnore

编辑

如果要向DynamoDB项添加未在类中建模的属性,则必须使用较低级别的客户端。或者Document客户端可以给予一些较高级别的抽象,同时仍然允许灵活性。

ehxuflar

ehxuflar2#

您可以通过动态AttributeValue将值插入到表中

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;

@Service
public class DynamoService {

    @Autowired AmazonDynamoDB amazonDynamoClient;

    public void putItemByMap(String nameTable, Map<String,String> values){

        Map<String,AttributeValue> mapValuesToBePut = 
               values.entrySet()
                     .stream()
                     .collect(Collectors.toMap(
                        Map.Entry::getKey, entry -> new AttributeValue(entry.getValue()))
                      );

        amazonDynamoClient.putItem(nameTable, mapValuesToBePut);
    }
}

字符串

相关问题