查询特定类别的产品

时间:2015-04-24 15:13:37

标签: amazon-dynamodb

我在DynamoDb中有一个简单的“产品”表。每个产品都有一个categories属性,这是一组类别ID,如下所示:

[{ "N" : "4" },{ "N" : "5" },{ "N" : "6" },{ "N" : "8" }]

产品表格包含id(哈希键)和accountId(范围键)

是否可以进行查询以查找类别6中的所有产品和accountId 1 而不进行扫描?或者我可以通过其他方式对其进行建模吗?

如果它是关系数据库,我会有一个产品到类别表,并加入产品。如果我在Dynamo中有一个类似的表,那么我需要为产品表中的每个产品制作一个GetItem,这感觉好像一个坏主意?

2 个答案:

答案 0 :(得分:2)

根据您的描述,听起来最好的方法是使用GSI

您的表格结构如下:

  • hashKey:id
  • rangeKey:accountId
  • 属性 - categories

您将使用以下结构创建全局二级索引:

  • hashKey:accountId
  • rangeKey:id
  • attribute- categories

然后,您可以使用您提到的条件查询此索引:

  1. accountId = 1
  2. categories contains 6
  3. 这是我编写的针对DynamoDB local的快速​​示例,该示例投影索引上的所有属性。

    import com.amazonaws.auth.BasicAWSCredentials;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
    import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
    import com.amazonaws.services.dynamodbv2.document.DynamoDB;
    import com.amazonaws.services.dynamodbv2.document.Index;
    import com.amazonaws.services.dynamodbv2.document.Item;
    import com.amazonaws.services.dynamodbv2.document.QueryFilter;
    import com.amazonaws.services.dynamodbv2.document.Table;
    import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
    import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
    import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
    import com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex;
    import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
    import com.amazonaws.services.dynamodbv2.model.KeyType;
    import com.amazonaws.services.dynamodbv2.model.Projection;
    import com.amazonaws.services.dynamodbv2.model.ProjectionType;
    import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
    import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
    import com.amazonaws.services.dynamodbv2.util.Tables;
    public class StackOverflow {
    
        private static final String EXAMPLE_TABLE_NAME = "example_table";
        private static final String HASH_KEY = "id";
        private static final String RANGE_KEY = "accountId";
        private static final String GSI = "accountIdToId";
        private static final String CATEGORIES = "categories";
    
        public static void main(String[] args) throws InterruptedException {
            AmazonDynamoDB
                client =
                new AmazonDynamoDBClient(new BasicAWSCredentials("accessKey", "secretKey"));
            client.setEndpoint("http://localhost:4000");
            DynamoDB dynamoDB = new DynamoDB(client);
            if (Tables.doesTableExist(client, EXAMPLE_TABLE_NAME)) {
                client.deleteTable(EXAMPLE_TABLE_NAME);
            }
    
            CreateTableRequest createTableRequest = new CreateTableRequest();
            createTableRequest.withTableName(EXAMPLE_TABLE_NAME);
            createTableRequest.withKeySchema(new KeySchemaElement(HASH_KEY, KeyType.HASH),
                                             new KeySchemaElement(RANGE_KEY, KeyType.RANGE));
            createTableRequest.withAttributeDefinitions(
                new AttributeDefinition(HASH_KEY, ScalarAttributeType.S),
                new AttributeDefinition(RANGE_KEY, ScalarAttributeType.S));
            createTableRequest.withProvisionedThroughput(new ProvisionedThroughput(15l, 15l));
            // GSI definition
            final GlobalSecondaryIndex
                accountIdToId =
                new GlobalSecondaryIndex().withIndexName(GSI).withKeySchema(
                    new KeySchemaElement(RANGE_KEY, KeyType.HASH),
                    new KeySchemaElement(HASH_KEY, KeyType.RANGE)).withProvisionedThroughput(
                    new ProvisionedThroughput(10l, 10l)).withProjection(
                    new Projection().withProjectionType(ProjectionType.ALL));
            createTableRequest.withGlobalSecondaryIndexes(accountIdToId);
    
            final Table table = dynamoDB.createTable(createTableRequest);
            table.waitForActive();
    
            table.putItem(new Item()
                              .withPrimaryKey(HASH_KEY, "1", RANGE_KEY, "6")
                              .withNumberSet(CATEGORIES, 1, 2, 5, 6));
    
            table.putItem(new Item()
                                     .withPrimaryKey(HASH_KEY, "2", RANGE_KEY, "6")
                                     .withNumberSet(CATEGORIES, 5, 6));
    
            table.putItem(new Item()
                                     .withPrimaryKey(HASH_KEY, "5", RANGE_KEY, "6")
                                     .withNumberSet(CATEGORIES, 1, 2));
    
            table.putItem(new Item()
                                     .withPrimaryKey(HASH_KEY, "5", RANGE_KEY, "8")
                                     .withNumberSet(CATEGORIES, 1, 2, 6));
    
            System.out.println("Scan the table, no filters");
            table.scan().forEach(System.out::println);
            System.out.println();
    
            final Index gsi = table.getIndex(GSI);
    
            System.out.println("Scan the GSI without filter");
            gsi.scan().forEach(System.out::println);
            System.out.println();
            System.out.println("Query the GSI with range key condition and contains");
    
            final QuerySpec querySpec = new QuerySpec()
                .withHashKey(RANGE_KEY, "6")
                .withQueryFilters(new QueryFilter(CATEGORIES).contains(6));
            gsi.query(querySpec).forEach(System.out::println);
            System.out.println();
        }
    }
    

    输出:

    Scan the table, no filters
    { Item: {accountId=6, id=1, categories=[1, 2, 5, 6]} }
    { Item: {accountId=6, id=5, categories=[1, 2]} }
    { Item: {accountId=8, id=5, categories=[1, 2, 6]} }
    { Item: {accountId=6, id=2, categories=[5, 6]} }
    
    Scan the GSI without filter
    { Item: {accountId=6, id=1, categories=[1, 2, 5, 6]} }
    { Item: {accountId=6, id=5, categories=[1, 2]} }
    { Item: {accountId=8, id=5, categories=[1, 2, 6]} }
    { Item: {accountId=6, id=2, categories=[5, 6]} }
    
    Query the GSI with range key condition and contains
    { Item: {accountId=6, id=1, categories=[1, 2, 5, 6]} }
    { Item: {accountId=6, id=2, categories=[5, 6]} }
    

答案 1 :(得分:0)

创建另一个表,并在更新主表时更新它。实际上,这就是RDBMS中发生的事情,它只是在背景中。当亚马逊为表格设置二级索引时,它们基本上只是自动化了人们一直在做的事情。