4

我在对二级索引运行查询时遇到问题,出现异常:

Ex 获取 dynamodb 扫描:java.lang.IllegalArgumentException:尝试执行需要二级索引的操作,而不在表元数据中定义索引属性。索引名称:category-timestamp-index

有人可以指导我如何做错了吗?

我的表是 idIT_RSS_Sources 并且我创建了一个索引类别-时间戳-索引。

附上索引截图

我的代码是:

DynamoDbEnhancedClient enhancedClient = getEnhancedDBClient(region);

 // Create a DynamoDbTable object

logger.debug("getting RSS Source category-timestamp-index");

//this throws the exception         
DynamoDbIndex<RSS_Source> catIndex = 
        enhancedClient.table("idIT_RSS_Sources", 
        TableSchema.fromBean(RSS_Source.class))
         .index("category-timestamp-index");


                logger.debug("building query attributes");

                AttributeValue att = AttributeValue.builder()
                        .s(theCategory)
                        .build();

                Map<String, AttributeValue> expressionValues = new HashMap<>();
                expressionValues.put(":value", att);

                Expression expression = Expression.builder()
                        .expression("category = :value")
                        .expressionValues(expressionValues)
                        .build();


                // Create a QueryConditional object that's used in the query operation
                QueryConditional queryConditional = QueryConditional
                        .keyEqualTo(Key.builder().partitionValue(theCategory)
                        .build());

                logger.debug("calling catIndex.query in getRSS...ForCategory");

                Iterator<Page<RSS_Source>> dbFeedResults =  (Iterator<Page<RSS_Source>>) catIndex.query(
                        QueryEnhancedRequest.builder()
                        .queryConditional(queryConditional)
                        .build());
4

3 回答 3

9

解决了,我没有在我的模型类中使用正确的注释:

@DynamoDbSecondaryPartitionKey(indexNames = { "category-index" }) 
public String getCategory() { return category; } 
public void setCategory(String category) { this.category = category; }
于 2020-05-29T03:52:06.113 回答
2

假设您有一个名为Issues的模型。

package com.example.dynamodb;

import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSecondaryPartitionKey;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey;

@DynamoDbBean
public class Issues {

    private String issueId;
    private String title;
    private String createDate;
    private String description;
    private String dueDate;
    private String status;
    private String priority;
    private String lastUpdateDate;

    @DynamoDbPartitionKey
    public String getId() {

        return this.issueId;
    }

    public void setId(String id) {

        this.issueId = id;
    }

    @DynamoDbSortKey
    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {

        this.title = title;
    }

    public void setLastUpdateDate(String lastUpdateDate) {

        this.lastUpdateDate = lastUpdateDate;
    }

    public String getLastUpdateDate() {
        return this.lastUpdateDate;
    }

    public void setPriority(String priority) {

        this.priority = priority;
    }

    public String getPriority() {
        return this.priority;
    }

    public void setStatus(String status) {

        this.status = status;
    }

    public String getStatus() {
        return this.status;
    }

    public void setDueDate(String dueDate) {

        this.dueDate = dueDate;
    }

    @DynamoDbSecondaryPartitionKey(indexNames = { "dueDateIndex" })
    public String getDueDate() {
        return this.dueDate;
    }


    public String getDate() {
        return this.createDate;
    }

    public void setDate(String date) {

        this.createDate = date;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {

        this.description = description;
    }
}

请注意 getDueDate 上的注释。

@DynamoDbSecondaryPartitionKey(indexNames = { "dueDateIndex" })
    public String getDueDate() {
        return this.dueDate;
    }

这是因为问题表有一个名为dueDateIndex 的二级索引。

在此处输入图像描述

要查询此二级索引,您可以使用使用 Amazon DynamoDB Java API V2 的代码:

public static void queryIndex(DynamoDbClient ddb, String tableName, String indexName) {

        try {
            // Create a DynamoDbEnhancedClient and use the DynamoDbClient object
            DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()
                    .dynamoDbClient(ddb)
                    .build();

            //Create a DynamoDbTable object based on Issues
            DynamoDbTable<Issues> table = enhancedClient.table("Issues", TableSchema.fromBean(Issues.class));

            String dateVal = "2013-11-19";

            DynamoDbIndex<Issues> secIndex =
                    enhancedClient.table("Issues",
                            TableSchema.fromBean(Issues.class))
                           .index("dueDateIndex");


            AttributeValue attVal = AttributeValue.builder()
                    .s(dateVal)
                    .build();

             // Create a QueryConditional object that's used in the query operation
            QueryConditional queryConditional = QueryConditional
                    .keyEqualTo(Key.builder().partitionValue(attVal)
                            .build());

                // Get items in the Issues table
            SdkIterable<Page<Issues>> results =  secIndex.query(
                    QueryEnhancedRequest.builder()
                            .queryConditional(queryConditional)
                            .build());

            AtomicInteger atomicInteger = new AtomicInteger();
            atomicInteger.set(0);
            results.forEach(page -> {

                Issues issue = (Issues) page.items().get(atomicInteger.get());
                System.out.println("The issue title is "+issue.getTitle());
                atomicInteger.incrementAndGet();
            });


        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
    }
于 2020-12-21T20:18:56.707 回答
0

我的工作代码如下: sortKey-index = GSI in dynamo db

List<Flow> flows = new ArrayList<>();
        DynamoDbIndex<Flow> flowBySortKey = table().index("sortKey-index");
        // Create a QueryConditional object that's used in the query operation
        QueryConditional queryConditional = QueryConditional
                        .keyEqualTo(Key.builder()
                        .partitionValue(sortKey)
                        .build());

        SdkIterable<Page<Flow>> dbFeedResults = flowBySortKey.query(
                        QueryEnhancedRequest.builder()
                        .queryConditional(queryConditional)
                        .build());

        dbFeedResults.forEach(flowPage -> {
            flows.addAll(flowPage.items());
        });
于 2020-05-28T15:18:00.253 回答