创建从倍数大的JSON响应QuerySpec AWS Dynamodb

问题描述:

我使用QuerySpec倍数查询,从而倍数ItemCollection:创建从倍数大的JSON响应QuerySpec AWS Dynamodb

  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对象或对象中,以便我可以用它创建一个大对象?

下面是示例代码查询两个不同的表Movies & post并返回JSONObject(即org.json.JSONObject)的列表。

商品分类有一个toJSON()将商品数据作为Json字符串返回。可以使用toJSON()方法将项目数据存储为List<String>List<JSONObject>

在下面的代码示例中,我返回List<JSONObject>

示例代码: -

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; 

输出: -

[{ 
    "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"] 
}] 

//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