我正在尝试从AWS DynamoDB数据库中获取数据。我设置了以下代码,但是我遇到了错误。不清楚如何从这里开始以便基于主键查询数据
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.*;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.*;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),
"**********************", // Identity Pool ID
Regions.US_EAST_1 // Region
);
AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(credentialsProvider);
DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);
答案 0 :(得分:1)
首先评论一下,看起来你在一个Activity中尝试这个 - 这肯定会在Android中引发网络主线程异常。
其次,作为一般指南,您需要设置一个单独的类,其中DynamoDB字段已设置并链接到类中的变量。例如:
@DynamoDBTable(tableName = SA_AWSConstants.QUESTIONTABLENAME)
public class SA_Question implements Comparable<SA_Question> {
// fields coming from AWS table
private String questionguid_str; // : STRING, PRIMARY HASH KEY
private String json_str;
private long ordinalposition_int;
...
@DynamoDBHashKey(attributeName = SA_AWSConstants.DBSCHEMA_QUESTIONGUID)
public String getQuestionguid_str() {
return questionguid_str;
}
public void setQuestionguid_str(String questionguid_str) {
this.questionguid_str = questionguid_str;
}
@DynamoDBRangeKey(attributeName = SA_AWSConstants.DBSCHEMA_ORDINALPOS)
public long getOrdinalposition_int() {
return ordinalposition_int;
}
public void setOrdinalposition_int(long ordinalposition_int) {
this.ordinalposition_int = ordinalposition_int;
}
@DynamoDBAttribute(attributeName = SA_AWSConstants.DBSCHEMA_QUESTION_JSONSTR)
public String getJson_str() {
return json_str;
}
public void setJson_str(String json_str) {
this.json_str = json_str;
}
}
然后你可以这样做:
DynamoDBMapper mapper = new DynamoDBMapper(ddb);
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
PaginatedScanList<SA_Question> paginatedq = mapper.scan(SA_Question.class, scanExpression);
ArrayList<SA_Question> awsqlist = new ArrayList<SA_Question>();
for (SA_Question q : paginatedq) {
q.initiateFromJSON();
awsqlist.add(q);
}
您必须注意AWS使用单个查询设置的数据返回限制(因此PaginatedScan - 如果您希望超出限制,则需要检查是否存在更多数据要返回)。 / p>