Voici l'exemple de code pour interroger deux tables différentes Movies
& post
et retourner une liste de JSONObject
(à savoir org.json.JSONObject).
La classe d'élément a un toJSON()
pour renvoyer les données de l'élément en tant que chaîne Json. La méthode toJSON()
peut être utilisée pour stocker les données de l'article en tant que List<String>
ou List<JSONObject>
.
Dans l'exemple de code ci-dessous, je renvoie List<JSONObject>
.
Code de l'échantillon: -
List<JSONObject> resultJson = new ArrayList<>();
DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
Table tableMovies = dynamoDB.getTable("Movies");
QuerySpec querySpecMovies = new QuerySpec();
querySpecMovies.withKeyConditionExpression("yearkey = :yearval")
.withValueMap(new ValueMap().withNumber(":yearval", 2014));
IteratorSupport<Item, QueryOutcome> iterator = tableMovies.query(querySpecMovies).iterator();
while (iterator.hasNext()) {
Item movieItem = iterator.next();
System.out.println("Movie data ====================>" + movieItem.toJSONPretty());
resultJson.add(new JSONObject(movieItem.toJSON()));
}
Table tablePosts = dynamoDB.getTable("post");
QuerySpec querySpecPosts = new QuerySpec();
querySpecPosts.withKeyConditionExpression("postId = :postIdval")
.withValueMap(new ValueMap().withString(":postIdval", "3"));
iterator = tablePosts.query(querySpecPosts).iterator();
while (iterator.hasNext()) {
Item postItem = iterator.next();
System.out.println("Post data ====================>" + postItem.toJSONPretty());
resultJson.add(new JSONObject(postItem.toJSON()));
}
return resultJson;
sortie: -
[{
"country": ["IN", "UK", "US"],
"Description": "Delete and set",
"yearkey": 2014,
"total_val": 0,
"title": "The Big New Movie 2",
"total_new_val": 2
}, {
"postId": "3",
"text": "Hello World",
"tags": ["A", "C"]
}]