Example usage for com.amazonaws.services.dynamodbv2.document Item withPrimaryKey

List of usage examples for com.amazonaws.services.dynamodbv2.document Item withPrimaryKey

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document Item withPrimaryKey.

Prototype

public Item withPrimaryKey(String hashKeyName, Object hashKeyValue) 

Source Link

Document

Convenient method to set the attributes of this item from the given hash-only primary key name and value.

Usage

From source file:com.eho.dynamodb.DynamoDBConnection.java

public static PutItemOutcome upload_resource_old(String resource) throws Exception {
    String id;//from w  ww  .  j  a  v  a 2s  .  c o  m
    JSONObject json_resource = new JSONObject(resource);
    //does the resource have a primary key?
    if (json_resource.has(PRIMARY_KEY))//if it does not have a primary key, create one using uuid
        id = json_resource.getString(PRIMARY_KEY);
    else
        id = UUID.randomUUID().toString();

    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
    Table table = dynamoDB.getTable(PATIENT_TABLE);

    //lets retreive based on the key. if key invalid (not assigned yet) nullis returned.
    Item retreived_item = table.getItem(PRIMARY_KEY, id);
    if (retreived_item == null)//if null instantiate it
    {
        retreived_item = new Item();
        retreived_item.withPrimaryKey(PRIMARY_KEY, id);
    }

    Integer new_version = retreived_item.getInt("version") + 1;
    retreived_item.withInt("version", new_version);

    Item item_to_upload = Item.fromJSON(retreived_item.toJSONPretty()).withJSON("Document", resource);
    PutItemSpec putItemSpec = new PutItemSpec().withItem(item_to_upload).withReturnValues(ReturnValue.NONE);
    return table.putItem(putItemSpec);
}

From source file:com.eho.dynamodb.DynamoDBConnection.java

public static String upload_resource(BaseResource resource,
        String primary_key /* if no primary key in case of post, send null*/ ) throws Exception {
    String id = add_primary_as_extension(resource, primary_key);
    String resource_string = DynamoDBConnection.fCtx.newJsonParser().setPrettyPrint(true)
            .encodeResourceToString(resource);
    ;/*from  w w  w.j  av a  2  s.com*/
    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
    Table table = dynamoDB.getTable(PATIENT_TABLE);

    //lets retreive based on the key. if key invalid (not assigned yet) nullis returned.
    Item retreived_item = table.getItem(PRIMARY_KEY, id);
    if (retreived_item == null)//if null instantiate it
    {
        retreived_item = new Item();
        retreived_item.withPrimaryKey(PRIMARY_KEY, id);
        retreived_item.withInt("version", -1);
    }

    Integer new_version = retreived_item.getInt("version") + 1;
    retreived_item.withInt("version", new_version);

    Item item_to_upload = retreived_item//Item.fromJSON(retreived_item.toJSONPretty())
            .withString("text" + new_version.toString(), resource_string)
            .withMap("json-document", new ObjectMapper().readValue(resource_string, LinkedHashMap.class));
    PutItemSpec putItemSpec = new PutItemSpec().withItem(item_to_upload);
    table.putItem(putItemSpec);
    return id;
}

From source file:com.eho.dynamodb.DynamoDBConnection.java

public static UpdateItemOutcome update_resource(String resource) throws Exception {
    String id;// w  ww  . j  a va  2 s.  c o  m
    JSONObject json_resource = new JSONObject(resource);
    //does the resource have a primary key?
    if (json_resource.has(PRIMARY_KEY))//if it does not have a primary key, create one using uuid
        id = json_resource.getString(PRIMARY_KEY);
    else
        id = UUID.randomUUID().toString();

    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
    Table table = dynamoDB.getTable(PATIENT_TABLE);

    //lets retreive based on the key. if key invalid (not assigned yet) nullis returned.
    Item retreived_item = table.getItem(PRIMARY_KEY, id);
    if (retreived_item == null)//if null instantiate it
    {
        retreived_item = new Item();
        retreived_item.withPrimaryKey(PRIMARY_KEY, id);
    }

    Integer new_version = retreived_item.getInt("version") + 1;
    retreived_item.withInt("version", new_version);
    String new_version_str = new_version.toString();

    UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(PRIMARY_KEY, id)
            .withUpdateExpression("SET " + new_version_str + "= :newval")
            .withValueMap(new ValueMap().withString(":newval", resource)).withReturnValues(ReturnValue.ALL_NEW);

    return table.updateItem(updateItemSpec);
}