gpt4 book ai didi

java - 从多个 QuerySpec AWS Dynamodb 创建大型 JSON 响应

转载 作者:行者123 更新时间:2023-11-30 06:27:40 25 4
gpt4 key购买 nike

我使用 QuerySpec 有多个查询,因此有多个 ItemCollection:

            Table table4 = dynamoDB.getTable("MyTable"); 
QuerySpec spec4 = new QuerySpec()
.withKeyConditionExpression("id = :v_id")
.withValueMap(new ValueMap()
.withString(":v_id", SAFETY_CHAPTERS_UUIDS.get(i)));
ItemCollection<QueryOutcome> items4 = table4.query(spec4);
Iterator<Item> iterator4 = items4.iterator();
Item itemsQuizzChapters = null;

我正在尝试找到一种方法来从这 4 个查询构建一个大的 JSON 响应。我如何将 itemCollection 添加到 JSON 对象或对象中,以便我可以使用所有这些对象创建一个大对象?

最佳答案

//import aws-java-sdk-bundle-1.11.85.jar, json-simple-1.1.jar and jackson-all-1.9.9.jar
public JSONObject getItemDynamoDB(String tableName, String primaryKey,String primaryKeyValue) {
Table index = dynamoDB.getTable(tableName);
QuerySpec querySpec = new QuerySpec().withKeyConditionExpression("#primaryKey = :value1")
.withNameMap(new NameMap().with("#primaryKey", primaryKey))
.withValueMap(new ValueMap().withString(":value1", primaryKeyValue));
ItemCollection<QueryOutcome> items = index.query(querySpec);
Iterator<Item> itemIterator = items.iterator();
JSONObject jsonObject = null;
try {
Object obj = jsonParser.parse(itemIterator.next().toJSONPretty());
jsonObject = (JSONObject) obj;

} catch (ParseException e) {
e.printStackTrace();
}
return jsonObject;
}
//call the function multiple time to get big JSON

public JSONArray concatenateMultipleItem(){
JSONArray jsonArray = new JSONArray() ;
jsonArray.add(getItemDynamoDB("TableName", "primaryKey", "1"));
jsonArray.add(getItemDynamoDB("TableName2", "primaryKey2", "2"));
System.out.println(jsonArray);
return jsonArray;
}

reference

关于java - 从多个 QuerySpec AWS Dynamodb 创建大型 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46790634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com