gpt4 book ai didi

amazon-web-services - 使用 DynamoDBMapper 的投影表达式

转载 作者:行者123 更新时间:2023-12-01 13:49:37 25 4
gpt4 key购买 nike

我有 500 个项目要使用 500 个主键属性检索,我想检索每个项目的 4 个属性而不是所有属性。我在 Java 中使用 DynamoDBMapper,但我无法找到将它们投影到特定数量的属性的方法。检索所有属性的成本可能非常高,因为其中一个属性可能非常大。有没有办法使用 DynamoDBMapper 获取所有这 500 个项目的 4 个属性?

最佳答案

是的,DynamoDBMapper支持使用 ProjectionExpressionsqueryscan方法。我在 load 上没有看到任何支持.

这是使用 public <T> PaginatedQueryList<T> query(java.lang.Class<T> clazz, DynamoDBQueryExpression<T> queryExpression) 的示例并提供投影 DynamoDBQueryExpression .看.withProjectionExpression("var1, #projectedVar")它使用 ExpressionAttributeNames对于一个属性。如果您需要检索不是顶级属性的属性,则必须查看 DocumentPaths documentation :

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
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.DynamoDBQueryExpression;
import com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.util.Tables;

import java.util.HashMap;
import java.util.Map;

public class MyTest {

public static final String TABLE_NAME = "test_table";

public static void main(String[] args) throws Exception {
final AmazonDynamoDB dynamodb =
new AmazonDynamoDBClient(new BasicAWSCredentials("aws", "key"));
dynamodb.setEndpoint("http://localhost:8000");
if (Tables.doesTableExist(dynamodb, TABLE_NAME)) {
dynamodb.deleteTable(TABLE_NAME);
}

final DynamoDBMapper mapper = new DynamoDBMapper(dynamodb);
final CreateTableRequest request = mapper.generateCreateTableRequest(MyClass.class)
.withProvisionedThroughput(new ProvisionedThroughput(5L, 5L));

dynamodb.createTable(request);

final MyClass myClass1 = new MyClass();
myClass1.setHash("H1");
myClass1.setRange("R1");
myClass1.setVar1("v1");
myClass1.setVar2("v2");
myClass1.setVar3("v3");
mapper.save(myClass1);
myClass1.setRange("R2");
myClass1.setVar1("v4");
myClass1.setVar2("v5");
myClass1.setVar3("v6");
mapper.save(myClass1);
myClass1.setRange("R3");
myClass1.setVar1("v7");
myClass1.setVar2("v8");
myClass1.setVar3("v9");
mapper.save(myClass1);


final HashMap<String, String> expressionAttributeNames = new HashMap<>();
expressionAttributeNames.put("#myHash", "hash");
expressionAttributeNames.put("#projectedVar", "var3");
final Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":hashValue", new AttributeValue("H1"));
final DynamoDBQueryExpression<MyClass> queryExpression = new DynamoDBQueryExpression<MyClass>()
.withKeyConditionExpression("#myHash = :hashValue")
.withExpressionAttributeNames(expressionAttributeNames)
.withExpressionAttributeValues(expressionAttributeValues)
.withProjectionExpression("var1, #projectedVar");
final DynamoDBMapperConfig mapperConfig =
new DynamoDBMapperConfig(DynamoDBMapperConfig.ConsistentReads.CONSISTENT);
final PaginatedQueryList<MyClass> query = mapper.query(MyClass.class, queryExpression, mapperConfig);

query.forEach(System.out::println);
}
}
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

@DynamoDBTable(tableName = MyTest.TABLE_NAME)
public class MyClass {
private String hash;
private String range;
private String var1;
private String var2;
private String var3;

@DynamoDBHashKey
public String getHash() { return hash; }

public void setHash(String hash) { this.hash = hash; }

@DynamoDBRangeKey
public String getRange() { return range; }

public void setRange(String range) { this.range = range; }

@DynamoDBAttribute
public String getVar1() { return var1; }

public void setVar1(String var1) { this.var1 = var1; }

@DynamoDBAttribute
public String getVar2() { return var2; }

public void setVar2(String var2) { this.var2 = var2; }

@DynamoDBAttribute
public String getVar3() { return var3; }

public void setVar3(String var3) { this.var3 = var3; }

@Override
public String toString() {
return "MyClass{" +
"hash='" + hash + '\'' +
", range='" + range + '\'' +
", var1='" + var1 + '\'' +
", var2='" + var2 + '\'' +
", var3='" + var3 + '\'' +
'}';
}
}

以及显示仅检索到投影属性的输出:

MyClass{hash='null', range='null', var1='v1', var2='null', var3='v3'}
MyClass{hash='null', range='null', var1='v4', var2='null', var3='v6'}
MyClass{hash='null', range='null', var1='v7', var2='null', var3='v9'}

关于amazon-web-services - 使用 DynamoDBMapper 的投影表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32957690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com