查询仅具有辅助全局索引的Dynamo表

 Jiaquan_Sun_106 发布于 2023-01-15 14:37

我正在尝试使用辅助全局索引查询Dynamodb表,并且得到了java.lang.IllegalArgumentException:查询表达式非法:在查询中找不到哈希键条件。我要做的就是在不考虑键的情况下获取所有时间戳大于某个值的项目。时间戳记不是键或范围键的一部分,因此我为其创建了全局索引。

有人知道我可能会缺少什么吗?

表定义:

{
   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,
            ReadCapacityUnits:8,
            WriteCapacityUnits:4
         },
         IndexSizeBytes:0,
         ItemCount:0
      }
   ]
}

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

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

Bruno Reis.. 6

All I'm trying to do is to get all items that have a timestamp greater than a value without considering the key.

This is not how Global Secondary Indexes (GSI) on Amazon DynamoDB work. To query a GSI you must specify a value for its hash key and then you may filter/sort by the range key -- just like you'd do with the primary key. This is exactly what the exception is trying to tell you, and also what you will find on the documentation page for the Query API:

A Query operation directly accesses items from a table using the table primary key, or from an index using the index key. You must provide a specific hash key value.

Think of a GSI as just another key that behaves almost exactly like the primary key (the main differences being that it is updated asynchronously, and you can only perform eventually consistent reads on GSIs).

Please refer to the Amazon DynamoDB Global Secondary Index documentation page for guidelines and best practices when creating GSIs: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

实现所需目标的一种可能方法是将虚拟属性限制为有限的少量可能值,在该虚拟属性上创建带有哈希键的GSI,并在时间戳上创建范围键。查询时,您需要为虚拟哈希键属性上的每个可能值发出一个Query API调用,然后将结果合并到应用程序中。通过将dummy属性限制为单例(即,具有单个元素的Set,即常量值),您只能发送一个Query API调用,并且直接获取结果数据集-但请记住,这会导致您遇到与热分区有关的问题,并且可能会遇到性能问题!同样,请参考上面链接的文档,以了解最佳做法和一些模式。

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有