简体   繁体   English

将不同的 dynamodb 项(来自同一个 dynamodb 表)解组到多个 POJO

[英]Unmarshalling different dynamodb items (from the same dynamodb table) to multiple POJOs

Context: I've 3 java classes that have different attributes and the JSON representation that correspond to these classes vary in structure.上下文:我有 3 个具有不同属性的 java 类,对应于这些类的 JSON 表示在结构上有所不同。 Say the following are the 3 JSON structures说以下是 3 个 JSON 结构

JSON representation for Class A A 类的 JSON 表示

{
  "foo" : ["some string-a", "some string-b", "some string-c"],
  "bar" : 890,
  "maz" : {"x":-11, "y": 0, "z":89} 
}

JSON representation for Class B B 类的 JSON 表示

{
  "abo" : [{"key": "val", "anotherKey": "anotherValue"}]
  "seq" : [4, 7, 1, 3]
}

JSON representation for Class C C 类的 JSON 表示

{
  "zin" : "some nice text",
  "tag" : ["dynamodb", "unmarshalling", "different schemas", "same table"]
}

I'm storing these JSON documents in one table in dynamoDB - let's call it tblStructures.我将这些 JSON 文档存储在 dynamoDB 的一个表中 - 我们称之为 tblStructures。

Why am I storing these different classes in the same DynamoDB table?为什么我将这些不同的类存储在同一个 DynamoDB 表中? Because they belong to the same business concept.因为它们属于同一个经营理念。 And having as many DynamoDB tables as the business concepts doesn't seem to be logical.拥有与业务概念一样多的 DynamoDB 表似乎不合逻辑。

Question: How can I unmarshall (deserialize) these items from the same dynamodb table to multiple Java Objects?问题:如何将这些项目从同一个 dynamodb 表解组(反序列化)到多个 Java 对象?

Borrowing a tip from a module for Mongoose that provides an easy way to work with base schemas and differentiate different types of models in the same collection on a MongoDB database ('collections' are akin to 'tables' in dynamodb), you could add a _type property to each model's schema and use that as a hint to deserialize them with the right class.借用 Mongoose 模块的一个技巧,它提供了一种简单的方法来处理基本模式并区分 MongoDB 数据库上同一集合中不同类型的模型(“集合”类似于 dynamodb 中的“表”),您可以添加一个_type属性添加到每个模型的模式,并将其用作提示,使用正确的类反序列化它们。

For example:例如:

JSON representation for Class A A 类的 JSON 表示

{
  "foo" : ["some string-a", "some string-b", "some string-c"],
  "bar" : 890,
  "maz" : {"x":-11, "y": 0, "z":89},
  "_type" : "modelA"
}

JSON representation for Class B B 类的 JSON 表示

{
  "abo" : [{"key": "val", "anotherKey": "anotherValue"}]
  "seq" : [4, 7, 1, 3],
  "_type" : "modelB"
}

I've used this approach in the past in conjunction with a routine to automatically treat all properties beginning with "_" as private fields and just stripping them when serialising to JSON (eg _password for password hashes in User objects, and for other metadata that I'd don't intent to directly expose).过去,我曾将这种方法与例程结合使用,以自动将所有以“_”开头的属性视为私有字段,并在序列_password JSON 时将它们剥离(例如,用户对象中密码哈希的_password ,以及其他元数据)我不打算直接暴露)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM