Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:使用Mapper查询DynamoDB表中范围键的所有值_Java_Amazon Web Services_Amazon Dynamodb - Fatal编程技术网

Java:使用Mapper查询DynamoDB表中范围键的所有值

Java:使用Mapper查询DynamoDB表中范围键的所有值,java,amazon-web-services,amazon-dynamodb,Java,Amazon Web Services,Amazon Dynamodb,我有一张DynamoDb桌子。它有一个哈希键“ID”和范围键“Category” 我试图找到一个“类别”的所有条目,例如喜剧 这是我的映射类: @DynamoDBTable(tableName = "Videos") public class VideoDynamoMappingAdapter { private String videoID; private String videoCategory; private String videoArtist; p

我有一张DynamoDb桌子。它有一个哈希键“ID”和范围键“Category”

我试图找到一个“类别”的所有条目,例如喜剧

这是我的映射类:

@DynamoDBTable(tableName = "Videos")
public class VideoDynamoMappingAdapter {

    private String videoID;
    private String videoCategory;
    private String videoArtist;
    private String videoTitle;

    @DynamoDBHashKey(attributeName = "ID")
    public String getVideoID() {
        return videoID;
    }

    public void setVideoID(String videoID) {
        this.videoID = videoID;
    }
    @DynamoDBRangeKey(attributeName = "Category")
    public String getVideoCategory() {
        return videoCategory;
    }

    public void setVideoCategory(String videoCategory) {
        this.videoCategory = videoCategory;
    }

    @DynamoDBAttribute(attributeName = "ArtistName")
    public String getVideoArtist() {
        return videoArtist;
    }

    public void setVideoArtist(String videoArtist) {
        this.videoArtist = videoArtist;
    }

    @DynamoDBAttribute(attributeName = "VideoTitle")
    public String getVideoTitle() {
        return videoTitle;
    }

    public void setVideoTitle(String videoTitle) {
        this.videoTitle = videoTitle;
    }

}
我一直在尝试这样的问题:

    DynamoDBQueryExpression<VideoDynamoMappingAdapter> queryExpression = new DynamoDBQueryExpression<VideoDynamoMappingAdapter>()
            .withRangeKeyCondition()


    List<VideoDynamoMappingAdapter> itemList = mapper.query(VideoDynamoMappingAdapter.class, queryExpression);
VideoDynamoMappingAdapter videosToFind = new VideoDynamoMappingAdapter();
        videosToFind.setVideoCategory("Other");

        DynamoDBQueryExpression<VideoDynamoMappingAdapter> queryExpression = new DynamoDBQueryExpression<VideoDynamoMappingAdapter>()
                .withIndexName("CategoryIndex")
                .withHashKeyValues(videosToFind)
                .withConsistentRead(false);

        List<VideoDynamoMappingAdapter> itemList = mapper.query(VideoDynamoMappingAdapter.class, queryExpression);
        System.out.println("test" + itemList.size());
DynamoDBQueryExpression queryExpression=newdynamodbqueryexpression()
.withRangeKeyCondition()
List itemList=mapper.query(VideoDynamoMappingAdapter.class,queryExpression);
我一直在看指南,但我不明白,我本以为这是一个一直都在做的简单查询,所以可能我遗漏了一些东西:


提前感谢您的帮助

如果没有哈希键,DynamoDB表无法仅通过范围键进行查询。但是,可以仅通过哈希键查询,而不使用范围键

当DynamoDB表仅通过范围键而不使用哈希键进行查询时,它将抛出
ValidationException

Unable to read item. Error JSON: {
  "message": "Query condition missed key schema element",
  "code": "ValidationException"
}
另一种选择是使用范围键扫描数据库。请注意,DynamoDB扫描是一项成本高昂的操作,因为它将扫描整个表

按类别扫描视频表:-

请注意,“或”条件用作“中的”仅支持某些数据类型

public List<VideoDynamoMappingAdapter> getVidoesByCategory(String[] categories) {

        ScanResultPage<VideoDynamoMappingAdapter> videoResultPage = null;
        List<VideoDynamoMappingAdapter> videoList = new ArrayList<>();

        try {

            do {
                DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(dynamoDBClient);

                Map<String, AttributeValue> expressionAttributeValues = new LinkedHashMap<String, AttributeValue>();

                StringBuilder filterExpression = new StringBuilder(); 
                int i = 1;

                for (String category : categories) {
                    if (i==1) {
                        filterExpression.append("Category = " + ":videoCategory" + i);  
                    } else {
                        filterExpression.append(" OR Category = " + ":videoCategory" + i);
                    }

                    expressionAttributeValues.put(":videoCategory" + i, new AttributeValue().withS(category));
                    i++;
                }

                System.out.println("Filterexpression =====>" + filterExpression.toString());

                DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()                
                        .withFilterExpression(filterExpression.toString())
                        .withExpressionAttributeValues(expressionAttributeValues);

                if (videoResultPage != null) {
                    scanExpression.setExclusiveStartKey(videoResultPage.getLastEvaluatedKey());
                }

                videoResultPage = dynamoDBMapper.scanPage(VideoDynamoMappingAdapter.class, scanExpression);

                System.out.println("Results ==========>" + videoResultPage.getResults());

                videoList.addAll(videoResultPage.getResults());

            } while (videoResultPage.getLastEvaluatedKey() != null);

        } catch (Exception db) {

            db.printStackTrace();

        }

        System.out.println(videoList.toString());
        return videoList;

    }
公共列表getVidoesByCategory(字符串[]类别){
ScanResultPage videoResultPage=null;
List videoList=new ArrayList();
试一试{
做{
DynamoDBMapper DynamoDBMapper=新的DynamoDBMapper(dynamoDBClient);
Map expressionAttributeValues=新建LinkedHashMap();
StringBuilder filterExpression=新建StringBuilder();
int i=1;
用于(字符串类别:类别){
如果(i==1){
filterExpression.append(“Category=“+”:videoCategory”+i);
}否则{
filterExpression.append(“或Category=“+”:videoCategory”+i);
}
expressionAttributeValue.put(“:videoCategory”+i,新的AttributeValue().with(category));
i++;
}
System.out.println(“Filterexpression====>”+Filterexpression.toString());
DynamoDBScanExpression scanExpression=新的DynamoDBScanExpression()
.withFilterExpression(filterExpression.toString())
.带有expressionAttributeValues(expressionAttributeValues);
如果(videoResultPage!=null){
scanExpression.setExclusiveStartKey(videoResultPage.getLastEvaluatedKey());
}
videoResultPage=dynamoDBMapper.scanPage(VideoDynamoMappingAdapter.class,scanExpression);
System.out.println(“结果===========>”+videoResultPage.getResults());
videoList.addAll(videoResultPage.getResults());
}while(videoResultPage.getLastEvaluatedKey()!=null);
}捕获(异常数据库){
db.printStackTrace();
}
System.out.println(videoList.toString());
返回视频列表;
}

如果没有哈希键,DynamoDB表不能仅通过范围键进行查询。但是,可以仅通过哈希键查询,而不使用范围键

当DynamoDB表仅通过范围键而不使用哈希键进行查询时,它将抛出
ValidationException

Unable to read item. Error JSON: {
  "message": "Query condition missed key schema element",
  "code": "ValidationException"
}
另一种选择是使用范围键扫描数据库。请注意,DynamoDB扫描是一项成本高昂的操作,因为它将扫描整个表

按类别扫描视频表:-

请注意,“或”条件用作“
中的”仅支持某些数据类型

public List<VideoDynamoMappingAdapter> getVidoesByCategory(String[] categories) {

        ScanResultPage<VideoDynamoMappingAdapter> videoResultPage = null;
        List<VideoDynamoMappingAdapter> videoList = new ArrayList<>();

        try {

            do {
                DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(dynamoDBClient);

                Map<String, AttributeValue> expressionAttributeValues = new LinkedHashMap<String, AttributeValue>();

                StringBuilder filterExpression = new StringBuilder(); 
                int i = 1;

                for (String category : categories) {
                    if (i==1) {
                        filterExpression.append("Category = " + ":videoCategory" + i);  
                    } else {
                        filterExpression.append(" OR Category = " + ":videoCategory" + i);
                    }

                    expressionAttributeValues.put(":videoCategory" + i, new AttributeValue().withS(category));
                    i++;
                }

                System.out.println("Filterexpression =====>" + filterExpression.toString());

                DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()                
                        .withFilterExpression(filterExpression.toString())
                        .withExpressionAttributeValues(expressionAttributeValues);

                if (videoResultPage != null) {
                    scanExpression.setExclusiveStartKey(videoResultPage.getLastEvaluatedKey());
                }

                videoResultPage = dynamoDBMapper.scanPage(VideoDynamoMappingAdapter.class, scanExpression);

                System.out.println("Results ==========>" + videoResultPage.getResults());

                videoList.addAll(videoResultPage.getResults());

            } while (videoResultPage.getLastEvaluatedKey() != null);

        } catch (Exception db) {

            db.printStackTrace();

        }

        System.out.println(videoList.toString());
        return videoList;

    }
公共列表getVidoesByCategory(字符串[]类别){
ScanResultPage videoResultPage=null;
List videoList=new ArrayList();
试一试{
做{
DynamoDBMapper DynamoDBMapper=新的DynamoDBMapper(dynamoDBClient);
Map expressionAttributeValues=新建LinkedHashMap();
StringBuilder filterExpression=新建StringBuilder();
int i=1;
用于(字符串类别:类别){
如果(i==1){
filterExpression.append(“Category=“+”:videoCategory”+i);
}否则{
filterExpression.append(“或Category=“+”:videoCategory”+i);
}
expressionAttributeValue.put(“:videoCategory”+i,新的AttributeValue().with(category));
i++;
}
System.out.println(“Filterexpression====>”+Filterexpression.toString());
DynamoDBScanExpression scanExpression=新的DynamoDBScanExpression()
.withFilterExpression(filterExpression.toString())
.带有expressionAttributeValues(expressionAttributeValues);
如果(videoResultPage!=null){
scanExpression.setExclusiveStartKey(videoResultPage.getLastEvaluatedKey());
}
videoResultPage=dynamoDBMapper.scanPage(VideoDynamoMappingAdapter.class,scanExpression);
System.out.println(“结果===========>”+videoResultPage.getResults());
videoList.addAll(videoResultPage.getResults());
}while(videoResultPage.getLastEvaluatedKey()!=null);
}捕获(异常数据库){
db.printStackTrace();
}
System.out.println(videoList.toString());
返回视频列表;
}

他的回答是正确的,我无法查询哈希键的所有值