在我的项目中使用java对dynamodb进行分页

时间:2016-10-28 05:26:49

标签: amazon-dynamodb

我正在尝试在我的项目中使用java实现dynamodb的分页,我已经实现了它,我正在努力如何获得no。使用DynamoDBScanExpression的每页结果中的元素数。谁能帮忙。感谢

这是我的代码

package com.morrisons.extendedrange.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig.TableNameOverride;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression;
import com.amazonaws.services.dynamodbv2.datamodeling.PaginatedScanList;
import com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage;
import com.amazonaws.services.dynamodbv2.datamodeling.ScanResultPage;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.amazonaws.services.dynamodbv2.model.ReturnConsumedCapacity;
import com.google.inject.Inject;
import com.morrisons.extendedrange.config.ExtendedRangeServiceConfiguration;
import com.morrisons.extendedrange.entity.ExtendedRangeEntity;
import com.morrisons.extendedrange.exception.ErrorCodes;
 import com.morrisons.extendedrange.exception.ExtendedRangeServiceException;
 import com.morrisons.extendedrange.model.ExtendedRange;
 import com.morrisons.extendedrange.util.Logger;
import com.morrisons.extendedrange.util.LoggerFactory;

/**
* The Class ExtendedRangeDao.
*/

public class ExtendedRangeDao {

private static Logger LOGGER;

@Inject
private LoggerFactory logFactory;

Map<String, AttributeValue> lastEvaluatedKey = null;

@Inject
private void init() {
    LOGGER = logFactory.getLogger(ExtendedRangeDao.class);
}

/** The range service configuration. */
@Inject
private ExtendedRangeServiceConfiguration extendedRangeServiceConfiguration;

/**
 * Gets the range.
 *
 * @param storeId
 *            the store id
 * @param catalogId
 *            the catalog id
 * @return the range
 */

public ExtendedRange getExtendedRange(String catalogId, String productId, String page) {
    LOGGER.debug("ExtendedRangeDao : getExtendedRange start ");

    List<ExtendedRange> extendedRangeList = new ArrayList<ExtendedRange>();



    try {
        AmazonDynamoDBClient client = extendedRangeServiceConfiguration.getDynamoDBClient();
        DynamoDBMapper mapper = new DynamoDBMapper(client);         


        Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
        eav.put(":val1", new AttributeValue().withS(productId));
        eav.put(":val2", new AttributeValue().withS(catalogId));


        DynamoDBScanExpression scanExpression=new DynamoDBScanExpression()


        .withFilterExpression("(productname =:val1 or productid =:val1) and catalogid = :val2 ")
        .withExpressionAttributeValues(eav)
        .withConsistentRead(true)
        .withLimit(5)

        .withExclusiveStartKey(lastEvaluatedKey) ;

        ScanResultPage<ExtendedRangeEntity> queryResultPage = mapper.scanPage(ExtendedRangeEntity.class,
                scanExpression, getDBMapperConfigForFetch());

        List<ExtendedRangeEntity> extendedRangeEntityList = queryResultPage.getResults();
        lastEvaluatedKey = queryResultPage.getLastEvaluatedKey();

        if (extendedRangeEntityList.isEmpty()) {

            String errorMessage = "No Record found or Multiple data found";
            LOGGER.debug(errorMessage);
            throw new ExtendedRangeServiceException(ErrorCodes.NO_RECORDS_FOUND, "ExtendedRangeEntity not found");
        } else {
            for (ExtendedRangeEntity extendedRangeEntity : extendedRangeEntityList) {

                ExtendedRange extendedRange = new ExtendedRange();
                copyProperties(extendedRange, extendedRangeEntity);
                extendedRangeList.add(extendedRange);
                LOGGER.debug("ExtendedRangeDao : getExtendedRange end ");
            }

            ExtendedRange returnExtendedRange = new ExtendedRange();
            returnExtendedRange.setExtendedRangeList(extendedRangeList);

            return returnExtendedRange;
        }
    } catch (AmazonServiceException e) {
        String errorMessage = "Error in retrieving Data in DynamoDB";
        LOGGER.error(errorMessage, e);
        throw new ExtendedRangeServiceException(ErrorCodes.AMAZON_SERVICE_ERROR, errorMessage);
    }

}

private DynamoDBMapperConfig getDBMapperConfigForFetch() {

    String tableName = extendedRangeServiceConfiguration.getTableNameConfig()
            .getTableName(ExtendedRangeEntity.class);
    TableNameOverride tableNameOverride = new TableNameOverride(tableName);
    DynamoDBMapperConfig dbMapperConfig = new DynamoDBMapperConfig(tableNameOverride);
    return dbMapperConfig;
}

public ExtendedRangeServiceConfiguration getExtendedRangeServiceConfiguration() {
    return extendedRangeServiceConfiguration;
}

public void setRangeServiceConfiguration(ExtendedRangeServiceConfiguration extendedRangeServiceConfiguration) {
    this.extendedRangeServiceConfiguration = extendedRangeServiceConfiguration;
}

private void copyProperties(ExtendedRange target, ExtendedRangeEntity src) {

    target.setProductId(src.getProductId());
    target.setLeadTimeUOM(src.getLeadTimeUOM());
    target.setCatalogId(src.getCatalogId());
    target.setProductDesc(src.getProductDesc());
    target.setProductName(src.getProductName());
    target.setLeadTime(src.getLeadTime());
    target.setCanBeOrderedFromDate(src.getCanBeOrderedFromDate());
    target.setCanBeOrderedToDate(src.getCanBeOrderedFromDate());

}

}

1 个答案:

答案 0 :(得分:0)

size()方法应该提供页面中元素的数量。

queryResultPage.getResults().size()