本文整理匯總了Java中com.amazonaws.services.dynamodbv2.AmazonDynamoDB.putItem方法的典型用法代碼示例。如果您正苦於以下問題:Java AmazonDynamoDB.putItem方法的具體用法?Java AmazonDynamoDB.putItem怎麽用?Java AmazonDynamoDB.putItem使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.amazonaws.services.dynamodbv2.AmazonDynamoDB
的用法示例。
在下文中一共展示了AmazonDynamoDB.putItem方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createItemIfNotExists
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; //導入方法依賴的package包/類
boolean createItemIfNotExists(String key, long currentTimeMillis, Context context) {
LambdaLogger logger = context.getLogger();
AmazonDynamoDB client = createDynamoDBClient(cc);
String functionName = context.getFunctionName();
try {
// Create a record if it does not exist
PutItemRequest req = new PutItemRequest().withTableName(TABLE_NAME)
.addItemEntry(COL_FUNCTION_NAME, new AttributeValue(functionName))
.addItemEntry(COL_KEY, new AttributeValue(key))
.addItemEntry(COL_CREATED_TIME, new AttributeValue().withN(Long.toString(currentTimeMillis)))
.addExpectedEntry(COL_FUNCTION_NAME, new ExpectedAttributeValue().withExists(false))
.addExpectedEntry(COL_KEY, new ExpectedAttributeValue().withExists(false));
client.putItem(req);
return true;
} catch (ConditionalCheckFailedException e) {
logger.log("Record exsited. functionName[" + functionName + "] key[" + key + "]");
return false;
} finally {
client.shutdown();
}
}
示例2: testUpdateETag
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; //導入方法依賴的package包/類
@Test
public void testUpdateETag() {
AmazonDynamoDB dynamoDB = PowerMock.createMock(AmazonDynamoDB.class);
Map<String, AttributeValue> newItem = new HashMap<String, AttributeValue>();
newItem.put(MarsDynamoDBManager.RESOURCE_TABLE_HASH_KEY, new AttributeValue(resource));
newItem.put(DynamoDBWorkerUtils.ETAG_KEY, new AttributeValue(eTag));
dynamoDB.putItem(table, newItem);
PowerMock.expectLastCall().andReturn(null);
PowerMock.replayAll();
DynamoDBWorkerUtils.updateETag(dynamoDB, table, resource, eTag);
PowerMock.verifyAll();
}
示例3: put
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; //導入方法依賴的package包/類
@Override
public Item put(final Map<String, AttributeValue> attributes)
throws IOException {
final AmazonDynamoDB aws = this.credentials.aws();
try {
final PutItemRequest request = new PutItemRequest();
request.setTableName(this.self);
request.setItem(attributes);
request.setReturnValues(ReturnValue.NONE);
request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
final PutItemResult result = aws.putItem(request);
final long start = System.currentTimeMillis();
Logger.info(
this, "#put('%[text]s'): created item in '%s', %s, in %[ms]s",
attributes, this.self,
new PrintableConsumedCapacity(
result.getConsumedCapacity()
).print(),
System.currentTimeMillis() - start
);
return new AwsItem(
this.credentials,
this.frame(),
this.self,
new Attributes(attributes).only(this.keys()),
new Array<String>(this.keys())
);
} catch (final AmazonClientException ex) {
throw new IOException(
String.format(
"failed to put into \"%s\" with %s",
this.self, attributes
),
ex
);
} finally {
aws.shutdown();
}
}
示例4: execute
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; //導入方法依賴的package包/類
@Override
public void execute() throws MetaModelException {
final Map<String, AttributeValue> itemValues = new HashMap<>();
final Column[] columns = getColumns();
final Object[] values = getValues();
for (int i = 0; i < columns.length; i++) {
final Column column = columns[i];
final Object value = values[i];
if (column.isPrimaryKey() && value == null) {
throw new IllegalArgumentException("Value for '" + column.getName() + "' cannot be null");
}
final AttributeValue attributeValue = DynamoDbUtils.toAttributeValue(value);
itemValues.put(column.getName(), attributeValue);
}
final AmazonDynamoDB dynamoDb = getUpdateCallback().getDataContext().getDynamoDb();
dynamoDb.putItem(getTable().getName(), itemValues);
}
示例5: startsAndStops
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; //導入方法依賴的package包/類
/**
* Instances can start and stop.
* @throws Exception If something is wrong
*/
@Test
public void startsAndStops() throws Exception {
final int port = this.reserve();
final Instances instances = new Instances();
instances.start(
new File(InstancesTest.DIST), port,
new File(System.getProperty("java.home")),
Collections.singletonList("-inMemory")
);
try {
final AmazonDynamoDB aws = new AmazonDynamoDBClient(
new BasicAWSCredentials("AWS-key", "AWS-secret")
);
aws.setEndpoint(String.format("http://localhost:%d", port));
final String table = "test";
final String attr = "key";
final CreateTableResult result = aws.createTable(
new CreateTableRequest()
.withTableName(table)
.withProvisionedThroughput(
new ProvisionedThroughput()
.withReadCapacityUnits(1L)
.withWriteCapacityUnits(1L)
)
.withAttributeDefinitions(
new AttributeDefinition()
.withAttributeName(attr)
.withAttributeType(ScalarAttributeType.S)
)
.withKeySchema(
new KeySchemaElement()
.withAttributeName(attr)
.withKeyType(KeyType.HASH)
)
);
MatcherAssert.assertThat(
result.getTableDescription().getTableName(),
Matchers.equalTo(table)
);
aws.putItem(
new PutItemRequest()
.withTableName(table)
.addItemEntry(attr, new AttributeValue("testvalue"))
);
} finally {
instances.stop(port);
}
}
示例6: updateETag
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; //導入方法依賴的package包/類
/**
* Updates the resource table for the specified resource with the specified ETag.
*
* @param dynamoDB
* DynamoDB client configured with a region and credentials
* @param table
* The DynamoDB resource table
* @param resource
* The resource URL
* @param eTag
* The new ETag for the resource
*/
public static void updateETag(final AmazonDynamoDB dynamoDB, final String table, final String resource,
final String eTag) {
// Build item
final Map<String, AttributeValue> newResource = new HashMap<>();
newResource.put(MarsDynamoDBManager.RESOURCE_TABLE_HASH_KEY, new AttributeValue(resource));
newResource.put(DynamoDBWorkerUtils.ETAG_KEY, new AttributeValue(eTag));
dynamoDB.putItem(table, newResource);
}