2012-02-15 96 views

回答

26

不明確,但是,顯然需要訂購了許多現實世界的使用情況,並通過相應的Hash and Range Type Primary Key來建模:

在這種情況下,主鍵由兩個屬性。第一個 屬性是散列屬性,第二個屬性是範圍 屬性。 Amazon DynamoDB在散列 主鍵屬性和排序範圍索引上創建一個無序散列索引,範圍主要爲 關鍵屬性[重點礦山]

然後可以使用該範圍內的索引經由Query APIRangeKeyCondition參數可選地請求項和向前指定或通過指數(即排序方向)的向後遍歷ScanIndexForward參數。

更新:您可以通過屬性與相同的方式local secondary index訂購。

+13

的ScanIndexForward PARAM似乎只適用於[查詢](http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/API_Query.html),而不是[掃描](HTTP://文檔。amazonwebservices.com/amazondynamodb/latest/developerguide/API_Scan.html)是否正確?如何使用Query返回表中所有項目的有序分頁列表?掃描似乎是返回「*」的方式,但它似乎沒有用於排序結果的參數。 – case2000 2012-02-15 20:20:22

+0

我沒有使用過這個功能,只是閱讀了它,但是Query支持指定一個[Limit](限制)收到的結果數量,以及在達到限制時是否有更多與您的查詢匹配的項目,您將收到一個_LastEvaluatedKey_,可用於執行另一個查詢並繼續檢索結果。 – fernio 2014-12-10 15:59:26

6

您可以使用排序鍵並應用query中的ScanIndexForward參數以升序或降序排序。在這裏,我限制的項目回到1

var params = { 
    TableName: 'Events', 
    KeyConditionExpression: 'Organizer = :organizer', 
    Limit: 1, 
    ScanIndexForward: false, // true = ascending, false = descending 
    ExpressionAttributeValues: { 
     ':organizer': organizer 
    } 
}; 

docClient.query(params, function(err, data) { 
    if (err) { 
     console.log(JSON.stringify(err, null, 2)); 
    } else { 
     console.log(JSON.stringify(data, null, 2)); 
    } 
}); 
+2

問題是如果你想返回*所有*項目。從本質上講,這意味着您必須創建一個新的虛擬列,將相同的值分配給所有行,在該列上創建一個GSI,然後調用查詢而不是掃描。 – JHH 2016-06-02 08:42:47

0

如果您正在使用boto2,你必須在你的表中的列的一個排序關鍵字,您可以排序你在順序或按相反的順序檢索什麼他說:

result = users.query_2(
    account_type__eq='standard_user', 
    reverse=True) 

如果您正在使用boto3,你必須對要由結果排序的列的排序鍵,你可以說你排序檢索數據

記住boto3如果ScanIndexForward爲true,那麼DynamoDB會按照它們的存儲順序(按排序鍵值)返回結果。這是默認行爲。如果ScanIndexForward爲false,則DynamoDB會按排序鍵值的相反順序讀取結果,然後將結果返回給客戶端。

2

使用ScanIndexForward(true爲升序,false爲降序),也可以使用Query Expression的setLimit值限制結果。

請在下面找到使用QueryPage查找單個記錄的代碼。

public void fetchLatestEvents() { 
    EventLogEntitySave entity = new EventLogEntitySave(); 
    entity.setId("1C6RR7JM0JS100037_contentManagementActionComplete"); 

    DynamoDBQueryExpression<EventLogEntitySave> queryExpression = new DynamoDBQueryExpression<EventLogEntitySave>().withHashKeyValues(entity); 
    queryExpression.setScanIndexForward(false); 
    queryExpression.withLimit(1); 
    queryExpression.setLimit(1); 

    List<EventLogEntitySave> result = dynamoDBMapper.queryPage(EventLogEntitySave.class, queryExpression).getResults(); 
    System.out.println("size of records = "+result.size()); 
} 

@DynamoDBTable(tableName = "PROD_EA_Test") 
public class EventLogEntitySave { 

     @DynamoDBHashKey 
     private String id; 
     private String reconciliationProcessId; 
     private String vin; 
     private String source; 
} 

public class DynamoDBConfig { 
    @Bean 
    public AmazonDynamoDB amazonDynamoDB() { 

      String accesskey = ""; 
      String secretkey = ""; 
      // 
      // creating dynamo client 
      BasicAWSCredentials credentials = new BasicAWSCredentials(accesskey, secretkey); 
      AmazonDynamoDB dynamo = new AmazonDynamoDBClient(credentials); 
      dynamo.setRegion(Region.getRegion(Regions.US_WEST_2)); 
      return dynamo; 
     } 

    @Bean 
    public DynamoDBMapper dynamoDBMapper() { 
     return new DynamoDBMapper(amazonDynamoDB()); 
    } 
} 
+0

使用ScanIndexForward(true爲升序,false爲降序) – 2017-12-18 18:19:19

相關問題