加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 仅使用辅助全局索引查询Dynamo表

发布时间:2020-12-15 04:20:14 所属栏目:Java 来源:网络整理
导读:我试图使用辅助全局索引查询Dynamodb表,我得到 java.lang.IllegalArgumentException:非法查询表达式:在查询中找不到散列键条件.我所要做的就是在不考虑密钥的情况下获取时间戳大于值的所有项目.时间戳不是键或范围键的一部分,因此我为它创建了一个全局索引
我试图使用辅助全局索引查询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,WriteCapacityUnits:4
         },IndexSizeBytes:0,ItemCount:0
      }
   ]
}

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);

解决方法

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

这不是Amazon DynamoDB上的全局二级索引(GSI)的工作方式.要查询GSI,您必须为其散列键指定一个值,然后您可以按范围键进行过滤/排序 – 就像您使用主键一样.这正是异常试图告诉您的内容,以及您在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.

可以将GSI视为另一个与主键几乎完全相同的键(主要区别在于它是异步更新的,并且您只能在GSI上执行最终一致的读取).

有关创建GSI时的指南和最佳实践,请参阅Amazon DynamoDB全局二级索引文档页面:http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读