DynamoDB表得到部分更新

时间:2017-03-15 09:22:57

标签: android amazon-web-services runtime-error amazon-dynamodb

尝试更新DynamoDB表时,只更新分区键值。在尝试包含其余值时,会弹出以下错误。指向getter方法。

import android.util.Log;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBHashKey;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.DescribeTableRequest;
import com.amazonaws.services.dynamodbv2.model.DescribeTableResult;
import com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException;

public class FeedbackDynamoDBManager {

private static final String TAG = "FeedbackDynamoDBManager";


public static String getTestTableStatus() {

    try {
        AmazonDynamoDBClient ddb = FeedbackActivity.clientManager
                .ddb();

        DescribeTableRequest request = new DescribeTableRequest()
                .withTableName(Constants.FEEDBACK_TABLE);
        DescribeTableResult result = ddb.describeTable(request);

        String status = result.getTable().getTableStatus();
        return status == null ? "" : status;

    } catch (ResourceNotFoundException e) {
    } catch (AmazonServiceException ex) {
        FeedbackActivity.clientManager
                .wipeCredentialsOnAuthError(ex);
    }
    return "";
}


public static void insertFeedback(FeedbackForm feedbackForm) {
    AmazonDynamoDBClient fdb = FeedbackActivity.clientManager
            .ddb();
    DynamoDBMapper fMapper = new DynamoDBMapper(fdb);

    try {
        Log.d(TAG, "Inserting form");
        fMapper.save(feedbackForm);
        Log.d(TAG, "Form inserted");
    } catch (AmazonServiceException ex) {
        Log.e(TAG, "Error inserting form");
        ex.printStackTrace();
        FeedbackActivity.clientManager
                .wipeCredentialsOnAuthError(ex);
    }catch (Exception e){
        Log.e("Feedback Form: ",e.toString());
        e.printStackTrace();
    }
}

@DynamoDBTable(tableName = Constants.FEEDBACK_TABLE)
public static class FeedbackForm {
        private String formId;
        private String bus;
        private String comments;
        private String ended_at;
        private String route;
        private String started_from;
        private String travel_date;

        @DynamoDBHashKey(attributeName = "form_id")
        public String getFormId() {
            return formId;
        }
        public void setFormId(String formId) {
            this.formId = formId;
        }

        @DynamoDBHashKey(attributeName = "bus")
        public String getBus() {
            return bus;
        }
        public void setBus(String bus) {
            this.bus = bus;
        }

        @DynamoDBHashKey(attributeName = "comments")
        public String getComments() {
            return comments;
        }
        public void setComments(String comments) {
            this.comments = comments;
        }

        @DynamoDBHashKey(attributeName = "ended_at")
        public String getEndedAt() {
            return ended_at;
        }
        public void setEndedAt(String ended_at) {
            this.ended_at = ended_at;
        }

        @DynamoDBHashKey(attributeName = "route")
        public String getRoute() {
            return route;
        }
        public void setRoute(String route) {
            this.route = route;
        }

        @DynamoDBHashKey(attributeName = "started_from")
        public String getStartedFrom() {
            return started_from;
        }
        public void setStartedFrom(String started_from) {
            this.started_from = started_from;
        }

        @DynamoDBHashKey(attributeName = "travel_date")
        public String getTraveDate() {
            return travel_date;
        }
        public void setTraveDate(String travel_date) {
            this.travel_date = travel_date;
        }

    }
}

FeedbackDynamoDBManager.java

    FeedbackDynamoDBManager.FeedbackForm feedbackForm = new FeedbackDynamoDBManager.FeedbackForm();

feedbackForm.setFormId(formId);
feedbackForm.setTraveDate(selectedDate);
feedbackForm.setComments(comments);
feedbackForm.setRoute(selectedRoute);
feedbackForm.setBus(selectedBus);
feedbackForm.setEndedAt(selectedEndLocation);
feedbackForm.setStartedFrom(selectedFromLocation);
FeedbackDynamoDBManager.insertFeedback(feedbackForm);

FeedbackActivity.java

03-15 14:39:47.040 11788-12278/com.iota.bus.institutebus E/AmazonClientManager: Error, wipeCredentialsOnAuthError calledcom.amazonaws.AmazonServiceException: The provided key element does not match the schema (Service: AmazonDynamoDB; Status Code: 400; Error Code: ValidationException; Request ID: 999LSFAJHJS999999D372C3LVVV4KQNSO5A99999999ASUAAJG)
E/FEEDBACK FORM: Data inserted!!!

  [03-15 14:39:47.104  4531: 4531 E/         ]
  [adb] handle_packet() t->online(1) p->msg.arg0(30963) p->msg.arg1(0) OPEN
  [ 03-15 14:39:47.104  4531: 4531 E/         ]
  [adb] handle the adb command, and the command = adb shell:cat /proc/net/xt_qtaguid/stats | grep 10369
  [ 03-15 14:39:47.132  4531: 4531 E/         ]
  [adb] cuurent command is A_CLSE 
  [ 03-15 14:39:47.412  4531: 4531 E/         ]
  [adb] handle_packet() t->online(1) p->msg.arg0(30964) p->msg.arg1(0) OPEN
  [ 03-15 14:39:47.412  4531: 4531 E/         ]
  [adb] handle the adb command, and the command = adb shell:cat /proc/11788/stat
  [ 03-15 14:39:47.430  4531: 4531 E/         ]
  [adb] cuurent command is A_CLSE 
  [ 03-15 14:39:47.450  4531: 4531 E/         ]
  [adb] handle_packet() t->online(1) p->msg.arg0(30965) p->msg.arg1(0) OPEN
  [ 03-15 14:39:47.450  4531: 4531 E/         ]
  [adb] handle the adb command, and the command = adb shell:cat /proc/stat
  [ 03-15 14:39:47.467  4531: 4531 E/         ]
  [adb] cuurent command is A_CLSE 
  [ 03-15 14:39:47.642  4531: 4531 E/         ]
  [adb] handle_packet() t->online(1) p->msg.arg0(30966) p->msg.arg1(0) OPEN
  [ 03-15 14:39:47.642  4531: 4531 E/         ]
  [adb] handle the adb command, and the command = adb shell:cat /proc/net/xt_qtaguid/stats | grep 10369
  [ 03-15 14:39:47.669  4531: 4531 E/         ]
  [adb] cuurent command is A_CLSE 

logcat

我在stackoverflow中发布的经验不多。请宽容:)。

编辑:评论除了form_id之外的getter。数据得到更新。否则logcat错误显示。

AWS console DynamoDB table screenshot

1 个答案:

答案 0 :(得分:1)

第一个问题出在FeedbackForm上。所有属性都使用注释@DynamoDBHashKey定义。请更改非散列键字段下面提到的注释。您应该使用@DynamoDBAttribute作为普通属性。

@DynamoDBAttribute(attributeName = "comments")
        public String getComments() {
            return comments;
        }