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

我试图使用辅助全局索引查询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调用并直接获得结果数据集 – 但请记住,这会导致您遇到与热分区相关的问题,您可能会遇到性能问题!再次,请参阅上面链接的文档以了解最佳实践和一些模式.

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...