查询手摇表仅与次级全局索引 [英] Query Dynamo table with only the secondary global index

查看:261
本文介绍了查询手摇表仅与次级全局索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着使用辅助全局索引来查询Dynamodb表,我得到java.lang.IllegalArgumentException:如果非法查询EX pression:没有哈希关键条件是在查询中。所有我想要做的是让有时间戳大于值的所有项目,而不考虑的关键。时间戳是不是一个键或一系列键的一部分,所以我创造了一个全球指数吧。

有没有人有什么线索我可能会丢失?

表定义:

  {
   AttributeDefinitions:
      {
         AttributeName的:时间戳,
         属性类型:N
      },
      {
         AttributeName的:URL,
         属性类型:•
      }
   ]
   表名:SitePageIndexed,
   KeySchema:
      {
         AttributeName的:URL,
         关键字类型:HASH
      }
   ]
   TableStatus:ACTIVE,
   CreationDateTime:周一5月12日18时45分57秒美国东部时间2014年,
   ProvisionedThroughput:{
      NumberOfDecreasesToday:0,
      ReadCapacityUnits:8,
      WriteCapacityUnits:4
   },
   TableSizeBytes:0,
   ItemCount中:0,
   GlobalSecondaryIndexes:
      {
         INDEXNAME:TimestampIndex,
         KeySchema:
            {
               AttributeName的:时间戳,
               关键字类型:HASH
            }
         ]
         投影:{
            ProjectionType:ALL,

         },
         IndexStatus:ACTIVE,
         ProvisionedThroughput:{
            NumberOfDecreasesToday:0,
            ReadCapacityUnits:8,
            WriteCapacityUnits:4
         },
         IndexSizeBytes:0,
         ItemCount中:0
      }
   ]
}
 

code

 条件条件1 =新的条件()withComparisonOperator(ComparisonOperator.GE).withAttributeValueList(新的AttributeValue()withN(Long.toString(开始))。)。
DynamoDBQueryEx pression< SitePageIndexed> EXP =新DynamoDBQueryEx pression< SitePageIndexed>()withRangeKeyCondition(时间戳,条件1)。
exp.setScanIndexForward(真正的);
exp.setLimit(100);
exp.setIndexName(TimestampIndex);

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

解决方案
  

所有我想要做的是让有时间戳大于值的所有项目,而不考虑的关键

这是不如何的全局辅助索引的(GSI)在亚马逊DynamoDB工作。要查询GSI您必须指定的哈希键的值,然后你可以过滤/排序范围内的关键 - 就像你做的主键。这正是异常想告诉你,而且你会发现上的 <对查询 API STRONG>文档页

  

查询操作直接访问使用该表的主键从一个表中的项目,或者使用索引键索引。 您必须提供一个特定的散列键值。

想想一个GSI作为的只是另一个重要的,其行为几乎完全一样的主键(主要区别在于它是异步更新,而你只能在GSIS执行最终一致性读取)。

创建GSIS时,请参照亚马逊DynamoDB全局二级索引文档页面的准则和最佳做法:的 http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

一种可能的方式来实现你想要的是将有一个的虚拟的属性限制在一个有限的,小的可能的值,创建一个GSI与在该虚拟属性和范围键散列键您的时间戳。查询时,您将需要发出一个查询API调用你的虚拟散列键属性的每个可能的值,然后整合在您的应用程序的结果。通过限制虚拟属性为单(即设置一个单一的元素,也就是说,一个恒定值),你可以只发送一个查询API调用,你直接得到你的结果数据集 - 但要记住,这将导致与热分区你的问题,你可能有性能问题!此外,参考上面链接最好的学习实践和一些图案的文档。

Im trying to query a Dynamodb table using a secondary global index and I'm getting java.lang.IllegalArgumentException: Illegal query expression: No hash key condition is found in the query. All I'm trying to do is to get all items that have a timestamp greater than a value without considering the key. The timestamp is not part of a key or range key, so i created a global index for it.

Does anyone have a clue what i might be missing?

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,
            ReadCapacityUnits:8,
            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);

解决方案

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

One possible way to achieve what you want would be to have a dummy attribute constrained to a finite, small set of possible values, create a GSI with hash key on that dummy attribute and range key on your timestamp. When querying, you would need to issue one Query API call for each possible value on your dummy hash key attribute, and then consolidate the results on your application. By constraining the dummy attribute to a singleton (i.e., a Set with a single element, i.e., a constant value), you can send only one Query API call and you get your result dataset directly -- but keep in mind that this will cause you problems related to hot partitions and you might have performance issues! Again, refer to the document linked above to learn the best practices and some patterns.

这篇关于查询手摇表仅与次级全局索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆