我如何在DynamoDB中使用IN条件

时间:2017-01-10 14:12:56

标签: java amazon-dynamodb

我需要用户ID从DynamoDB获取数据,因为我想使用IN条件,但我无法找到它如何使用的说明,你能帮助我吗?我有一些代码,如何将userId = :id1 OR userId = :id2替换为userId IN :ids?如何在userIds中设置:ids

public List<Data> getByUserIds(List<Long> userIds) {
    Table table = dynamoDB.getTable(getTableName());
    ScanSpec scanSpec = new ScanSpec()
            .withFilterExpression("userId = :id1 OR userId = :id2")
            .withValueMap(new ValueMap()
                    .withNumber(":id1", userIds.get(0))
                    .withNumber(":id2", userIds.get(1))
            );
    ItemCollection<ScanOutcome> scanOutcome = table.scan(scanSpec);
    return convertItemToData(scanOutcome.iterator());
}

谢谢!

2 个答案:

答案 0 :(得分:2)

我做了这个,但看起来很糟糕,也许有人知道如何让它变得更好?

valueMap.withList

我认为需要使用valueMap.withNumber代替 def split_base64(uri_str) if uri_str.match(%r{^data:(.*?);(.*?),(.*)$}) uri = Hash.new uri[:type] = $1 # "image/gif" uri[:encoder] = $2 # "base64" uri[:data] = $3 # data string uri[:extension] = $1.split('/')[1] # "gif" return uri else return nil end end def convert_data_uri_to_upload(image_url) image_data = split_base64(image_url) image_data_string = image_data[:data] image_data_binary = Base64.decode64(image_data_string) temp_img_file = Tempfile.new("") temp_img_file.binmode temp_img_file << image_data_binary temp_img_file.rewind img_params = {:filename => "image.#{image_data[:extension]}", :type => image_data[:type], :tempfile => temp_img_file} uploaded_file = ActionDispatch::Http::UploadedFile.new(img_params) end return uploaded_file end ,但这对我不起作用......

答案 1 :(得分:0)

您可以使用BatchGetItem API在同一查询中获取两个用户。