Java – query dynamo tables using only secondary global indexes

I tried to query the dynamodb table using the auxiliary global index, and I got Java Lang.illegalargumentexception: illegal query expression: hash key condition not found in query All I have to do is get all items whose timestamp is greater than the value without considering the key The timestamp is not part of the key or range key, so I created a global index for it

Does anyone know what I may lack?

Table definition:

{
   AttributeDeFinitions:[
      {
         AttributeName:timestamp,AttributeType:N
      },{
         AttributeName:url,AttributeType:S
      }
   ],TableName:SitePageIndexed,KeySchema:[
      {
         AttributeName:url,KeyType:HASH
      }
   ],TableStatus:ACTIVE,CreationDateTime:   Mon May 12 18:45:57   EDT 2014,ProvisionedThroughput:{
      NumberOfDecreasesToday:0,ReadCapacityUnits:8,WriteCapacityUnits:4
   },TableSizeBytes:0,ItemCount:0,GlobalSecondaryIndexes:[
      {
         IndexName:TimestampIndex,KeySchema:[
            {
               AttributeName:timestamp,KeyType:HASH
            }
         ],Projection:{
            ProjectionType:ALL,},IndexStatus:ACTIVE,ProvisionedThroughput:{
            NumberOfDecreasesToday:0,WriteCapacityUnits:4
         },IndexSizeBytes:0,ItemCount:0
      }
   ]
}

code

Condition condition1 = new Condition().withComparisonOperator(ComparisonOperator.GE).withAttributeValueList(new AttributeValue().withN(Long.toString(start)));      
DynamoDBQueryExpression<SitePageIndexed> exp = new DynamoDBQueryExpression<SitePageIndexed>().withRangeKeyCondition("timestamp",condition1);
exp.setScanIndexForward(true);
exp.setLimit(100);
exp.setIndexName("TimestampIndex");

PaginatedQueryList<SitePageIndexed> queryList = client.query(SitePageIndexed.class,exp);

Solution

This is not how the global secondary index (GSI) on Amazon dynamodb works To query GSI, you must specify a value for its hash key, and then you can filter / sort by range key - just as you use a primary key This is what the exception tries to tell you and what you will find on the documentation page for the query API:

You can think of GSI as another key that is almost identical to the primary key (the main difference is that it is updated asynchronously, and you can only perform final consistent reads on GSI)

For guidelines and best practices when creating GSI, refer to Amazon dynamodb global secondary index document page: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

One possible way to achieve the required functions is to constrain the virtual attribute to a limited set of possible values, create a GSI with hash key on the virtual attribute, and create a range key on the timestamp When querying, you need to issue a query API call for each possible value on the virtual hash key attribute, and then merge the results on the application By constraining the dummy attribute to a singleton (that is, a set with a single element, that is, a constant value), you can only send one query API call and get the result dataset directly – but remember, this will cause you to encounter problems related to hot partitioning, and you may encounter performance problems! Again, refer to the linked documentation above for best practices and some patterns

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>