gpt4 book ai didi

java - 如何从 Android 更新 DynamoDB 中的项目?

转载 作者:太空宇宙 更新时间:2023-11-04 10:22:19 26 4
gpt4 key购买 nike

标题描述了三天来围绕我的痛苦的问题。如何从 Android 应用程序更新 DynamoDB 中存储的项目?

下面我留下了我已经检查和实现但结果不成功的链接列表:

1 - https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-dynamodb-items.html结果:它严格适用于java ee,不适用于android

2 - https://docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-nosql-database.html#add-aws-mobile-nosql-database-crud-update结果:即使“unique-user-id”参数显然是唯一的,也会创建一个新项目

3 - https://docs.aws.amazon.com/aws-mobile/latest/developerguide/how-to-nosql-integrate-an-existing-table.html结果:同上,创建一个新的

4 - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.CRUDExample1.html结果:检索但创建一个新的项目

最佳答案

我做错了事情:- 不包括要发送更新的对象中的排序(也称为范围)键。

对于其余的:首先,获取您的 DynamoDBClient,我强烈建议实现单例模式来调用它,在本例中我使用了 configuration.json:

AWSMobileClient.getInstance().initialize(NewPatientActivity.this).execute();
AWSCredentialsProvider credentialsProvider = AWSMobileClient.getInstance().getCredentialsProvider();
AWSConfiguration configuration = AWSMobileClient.getInstance().getConfiguration();
// Add code to instantiate a AmazonDynamoDBClient
AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(credentialsProvider);

然后设置一个 UpdateItemRequest,如下所示:

//SET THE KEYS VALUES
HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put("your-partition-key", new AttributeValue().withS(value));
key.put("your-sort-key-if-exists", new AttributeValue().withS(value));
//SET VALUES TO REPLACE OLD ONES
Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
expressionAttributeValues.put(":val1",new AttributeValue().withN(value1));
expressionAttributeValues.put(":val2", new AttributeValue().withS(value2));
/*
IF YOU WANT TO READ THE JUST-UPDATED ITEM, HAVE THIS READY
ReturnValue returnValues = ReturnValue.ALL_NEW;

*/
//SET THE UPDATEITEMREQUEST
UpdateItemRequest updateItemRequest = new UpdateItemRequest()
.withTableName("your-table-name")
//KEYS DEFINED ABOVE
.withKey(key)
//SET WHERE TO UPDATE
.withUpdateExpression("set attr1 = :val1, attr2 = :val2")
.withExpressionAttributeValues(expressionAttributeValues)
//INDICATE TO RETURN UPDATED ITEM
.withReturnValues(returnValues);
//GET THE RESULT OF YOUR UPDATE AND EXECUTE IT
UpdateItemResult result = your-dynamo-client.updateItem(updateItemRequest);
//convert it to a string
Gson gson = new Gson();
String x = gson.toJson(result);
//Check it out
Log.e("RESULT :",x);

欲了解更多信息,我详细阐述了this要点。

关于java - 如何从 Android 更新 DynamoDB 中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50980160/

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