{"id":5172,"date":"2014-12-24T12:00:05","date_gmt":"2014-12-24T03:00:05","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=5172"},"modified":"2018-11-14T16:33:49","modified_gmt":"2018-11-14T07:33:49","slug":"amazon-dynamodb-local-and-aws-sdk-for-java-2","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2014\/12\/24\/amazon-dynamodb-local-and-aws-sdk-for-java-2\/","title":{"rendered":"Amazon DynamoDB Local &#038; AWS SDK for Java \u3092\u4f7f\u3063\u3066\u307f\u3088\u3046\uff08\u5f8c\u7de8\uff09"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002\u677e\u672c\u3067\u3059\u3002<br \/>\n<a title=\"TECHSCORE Advent Calendar 2014\" href=\"http:\/\/www.techscore.com\/blog\/2014\/11\/27\/techscore-advent-calendar-2014\/\">TECHSCORE Advent Calendar 2014<\/a> \u306e 24 \u65e5\u76ee\u306e\u6295\u7a3f\u3067\u3059\u3002<\/p>\n<p><a title=\"Amazon DynamoDB Local &amp; AWS SDK for Java \u3092\u4f7f\u3063\u3066\u307f\u3088\u3046\uff08\u524d\u7de8\uff09\" href=\"http:\/\/www.techscore.com\/blog\/2014\/12\/12\/amazon-dynamodb-local-and-aws-sdk-for-java-1\/\">\u524d\u56de<\/a>\u306f eclipse \u4e0a\u3067 Amazon DynamoDB Local \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3001AWS SDK for Java \u304b\u3089\u30a2\u30a4\u30c6\u30e0\u306e\u4fdd\u5b58\u3092\u884c\u3044\u307e\u3057\u305f\u3002\u4eca\u56de\u306f AWS SDK for Java \u306b\u30d5\u30a9\u30fc\u30ab\u30b9\u3092\u3042\u3066\u57fa\u672c\u7684\u306a API \u3092\u5229\u7528\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>\u74b0\u5883\u306f\u5f15\u304d\u7d9a\u304d<a title=\"Amazon DynamoDB Local &amp; AWS SDK for Java \u3092\u4f7f\u3063\u3066\u307f\u3088\u3046\uff08\u524d\u7de8\uff09\" href=\"http:\/\/www.techscore.com\/blog\/2014\/12\/12\/amazon-dynamodb-local-and-aws-sdk-for-java-1\/\">\u524d\u56de<\/a>\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\u3057\u305f DynamoDB Local \u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<h2>\u9ad8\u30ec\u30d9\u30eb API \u3068\u4f4e\u30ec\u30d9\u30eb API<\/h2>\n<p><code>PutItemRequest<\/code> \u30af\u30e9\u30b9\u3092\u4f7f\u3063\u305f<a title=\"ChannelCreationLowLevelAPI.java\" href=\"http:\/\/www.techscore.com\/blog\/2014\/12\/12\/amazon-dynamodb-local-and-aws-sdk-for-java-1\/#ChannelCreationLowLevelAPI.java\">\u524d\u56de\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/a>\u306f\u300c\u4f4e\u30ec\u30d9\u30eb API\u300d\u3068\u547c\u3070\u308c\u308b API \u3067\u3001AWS SDK for Java \u3067\u306f DynamoDB \u3092\u64cd\u4f5c\u3059\u308b\u305f\u3081\u306b\u300c\u9ad8\u30ec\u30d9\u30eb API\u300d\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<p><a title=\"AWS SDK for Java \u306e\u4f7f\u7528\" href=\"http:\/\/docs.aws.amazon.com\/ja_jp\/amazondynamodb\/latest\/developerguide\/AboutJava.html\" target=\"_blank\">AWS SDK for Java \u306e\u4f7f\u7528<\/a><\/p>\n<blockquote style=\"font-style: italic;\"><p>\u4f4e\u30ec\u30d9\u30eb API<br \/>\n\u4f4e\u30ec\u30d9\u30eb API \u306f\u3001\u57fa\u672c\u7684\u306a DynamoDB \u30aa\u30da\u30ec\u30fc\u30b7\u30e7\u30f3\u306b\u7dca\u5bc6\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059\u3002\u4f4e\u30ec\u30d9\u30eb API \u3092\u4f7f\u7528\u3059\u308b\u3068\u3001DynamoDB \u30aa\u30da\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3057\u3066\u5b9f\u884c\u3067\u304d\u308b\u30aa\u30da\u30ec\u30fc\u30b7\u30e7\u30f3\uff08\u30c6\u30fc\u30d6\u30eb\u306e\u4f5c\u6210\u3001\u66f4\u65b0\u3001\u524a\u9664\u3001\u304a\u3088\u3073\u9805\u76ee\u306e\u4f5c\u6210\u3001\u8aad\u307f\u8fbc\u307f\u3001\u66f4\u65b0\u3001\u524a\u9664\u306a\u3069\uff09\u3068\u540c\u3058\u3082\u306e\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u9ad8\u30ec\u30d9\u30eb API<br \/>\n\u9ad8\u30ec\u30d9\u30eb API \u3067\u306f\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6c38\u7d9a\u6027\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u624b\u6cd5\u3092\u4f7f\u7528\u3057\u3066\u3001Java \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092 DynamoDB \u30c6\u30fc\u30d6\u30eb\u3068\u5c5e\u6027\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3057\u307e\u3059\u3002\u9ad8\u30ec\u30d9\u30eb API \u3092\u4f7f\u7528\u3057\u3066\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u304c\u3001\u30c6\u30fc\u30d6\u30eb\u9805\u76ee\u306e\u4f5c\u6210\u3001\u8aad\u307f\u8fbc\u307f\u3001\u66f4\u65b0\u3001\u304a\u3088\u3073\u524a\u9664\u306f\u53ef\u80fd\u3067\u3059\u3002<\/p><\/blockquote>\n<p>\u666e\u6bb5\u3001\u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3078\u306e\u30a2\u30af\u30bb\u30b9\u306b ORM \u3092\u4f7f\u3046\u6a5f\u4f1a\u304c\u591a\u3044\u65b9\u306b\u306f\u9ad8\u30ec\u30d9\u30eb API \u306e\u65b9\u304c\u99b4\u67d3\u307f\u3084\u3059\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u524d\u56de\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u9ad8\u30ec\u30d9\u30eb API \u3067\u66f8\u304f\u3068\u3069\u3046\u306a\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<h2>\u9ad8\u30ec\u30d9\u30eb API \u3067\u30a2\u30a4\u30c6\u30e0\u3092\u4fdd\u5b58\u3059\u308b<\/h2>\n<p>\u9ad8\u30ec\u30d9\u30eb API \u3067\u306f POJO \u3092 DynamoDB \u306e\u30c6\u30fc\u30d6\u30eb\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3057\u3001<code>DynamoDBMapper<\/code> \u30af\u30e9\u30b9\u3067\u64cd\u4f5c\u3057\u307e\u3059\u3002<\/p>\n<h3>1. \u30de\u30c3\u30d4\u30f3\u30b0<\/h3>\n<p>\u307e\u305a\u306f\u524d\u56de\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u4f7f\u7528\u3057\u305f Channels \u30c6\u30fc\u30d6\u30eb\u3092 <code>Channel<\/code> \u30af\u30e9\u30b9\u306b\u30de\u30c3\u30d4\u30f3\u30b0\u3057\u307e\u3059\u3002<\/p>\n<p><a name=\"Channel.java\"><\/a>com\/techscore\/dynamodb_local_sample\/Channel.java<\/p>\n<pre class=\"lang:java decode:true\">package com.techscore.dynamodb_local_sample;\r\n\r\nimport java.time.OffsetDateTime;\r\nimport com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;\r\nimport com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;\r\nimport com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMarshalling;\r\nimport com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;\r\n\r\n@DynamoDBTable(tableName = \"Channels\")\r\npublic class Channel {\r\n\r\n    @DynamoDBHashKey\r\n    private String channelName;\r\n\r\n    @DynamoDBAttribute\r\n    private long maxMessageNumber;\r\n\r\n    @DynamoDBAttribute\r\n    @DynamoDBMarshalling(marshallerClass = OffsetDateTimeMarshaller.class)\r\n    private OffsetDateTime createdAt;\r\n\r\n    @DynamoDBAttribute\r\n    @DynamoDBMarshalling(marshallerClass = OffsetDateTimeMarshaller.class)\r\n    private OffsetDateTime updatedAt;\r\n\r\n    public String getChannelName() {\r\n        return channelName;\r\n    }\r\n\r\n    public void setChannelName(String channelName) {\r\n        this.channelName = channelName;\r\n    }\r\n\r\n..(snip)..\r\n\r\n}\r\n<\/pre>\n<p>\u5b9f\u969b\u306f\u5404\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u5bfe\u3059\u308b\u30d7\u30ed\u30d1\u30c6\u30a3\u3082\u5fc5\u8981\u3067\u3059\u306e\u3067\u9069\u5b9c\u3001Getter \/ Setter \u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3057\u3066\u4e0b\u3055\u3044\u3002<\/p>\n<p>\u30de\u30c3\u30d4\u30f3\u30b0\u65b9\u6cd5\u306f\u898b\u305f\u307e\u307e\u3002<code>@DynamoDBTable<\/code> \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u30af\u30e9\u30b9\u3068\u30c6\u30fc\u30d6\u30eb\u306e\u30de\u30c3\u30d4\u30f3\u30b0\u3092\u884c\u3044\u3001<code>@DynamoDBHashKey<\/code> \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3067 HashKey \u3092\u3001<code>@DynamoDBAttribute<\/code> \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u5c5e\u6027\u3092\u30de\u30c3\u30d4\u30f3\u30b0\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3082\u3057 RangeKey \u3092\u30de\u30c3\u30d4\u30f3\u30b0\u3057\u305f\u3044\u306a\u3089 <code>@DynamoDBRangeKey<\/code> \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u3001\u30de\u30c3\u30d4\u30f3\u30b0\u3057\u305f\u304f\u306a\u3044\u30d5\u30a3\u30fc\u30eb\u30c9\u3084\u30d7\u30ed\u30d1\u30c6\u30a3\u304c\u3042\u308c\u3070\u3001<code>@DynamoDBIgnore<\/code> \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p><code>@DynamoDBMarshalling<\/code> \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306f\u9ad8\u30ec\u30d9\u30eb API \u3067\u5c5e\u6027\u306e\u30c7\u30fc\u30bf\u578b\u3068\u3057\u3066\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u306a\u3044\u30af\u30e9\u30b9\u3092\u6271\u3046\u305f\u3081\u306e\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u3059\u3002\u4e0a\u8a18\u4f8b\u3067\u306f <code>java.time.OffsetDateTime<\/code> \u3092\u6271\u3046\u305f\u3081\u3001\u72ec\u81ea\u306b\u4f5c\u6210\u3057\u305f <code>OffsetDateTimeMarshaller<\/code> \u30af\u30e9\u30b9\u3092\u4f7f\u3046\u3053\u3068\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><code>OffsetDateTimeMarshaller<\/code> \u30af\u30e9\u30b9\u306f <code>DynamoDBMarshaller<\/code> \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\uff08<a title=\"DynamoDBMarshaller\" href=\"http:\/\/docs.aws.amazon.com\/AWSJavaSDK\/latest\/javadoc\/com\/amazonaws\/services\/dynamodbv2\/datamodeling\/DynamoDBMarshaller.html\" target=\"_blank\">javadoc<\/a>\uff09\u3092\u5b9f\u88c5\u3057\u305f\u6b21\u306e\u3088\u3046\u306a\u30af\u30e9\u30b9\u5b9a\u7fa9\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><a name=\"OffsetDateTimeMarshaller.java\"><\/a>com\/techscore\/dynamodb_local_sample\/OffsetDateTimeMarshaller.java<\/p>\n<pre class=\"lang:java decode:true\">package com.techscore.dynamodb_local_sample;\r\n\r\nimport java.time.OffsetDateTime;\r\nimport com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMarshaller;\r\n\r\npublic class OffsetDateTimeMarshaller implements DynamoDBMarshaller {\r\n\r\n    @Override\r\n    public String marshall(OffsetDateTime getterReturnResult) {\r\n        return getterReturnResult.toString();\r\n    }\r\n\r\n    @Override\r\n    public OffsetDateTime unmarshall(Class clazz, String obj) {\r\n        return OffsetDateTime.parse(obj);\r\n    }\r\n}\r\n<\/pre>\n<h3>2. \u30c7\u30fc\u30bf\u306e\u64cd\u4f5c<\/h3>\n<p>\u9ad8\u30ec\u30d9\u30eb API \u3067\u306e\u30c7\u30fc\u30bf\u64cd\u4f5c\u306f\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u3002\u30a2\u30a4\u30c6\u30e0\u3092\u4fdd\u5b58\u3059\u308b\u3060\u3051\u306a\u3089 <code>DynamoDBMapper<\/code> \u306e <code>save()<\/code> \u30e1\u30bd\u30c3\u30c9\u306b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6e21\u3059\u3060\u3051\u3067\u3059\u3002<\/p>\n<p><a name=\"ChannelCreationHighLevelAPI.java\"><\/a>com\/techscore\/dynamodb_local_sample\/ChannelCreationHighLevelAPI.java<\/p>\n<pre class=\"lang:java decode:true\">package com.techscore.dynamodb_local_sample;\r\n\r\nimport java.time.OffsetDateTime;\r\nimport com.amazonaws.auth.AWSCredentials;\r\nimport com.amazonaws.auth.BasicAWSCredentials;\r\nimport com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;\r\nimport com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;\r\nimport com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression;\r\nimport com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue;\r\n\r\npublic class ChannelCreationHighLevelAPI {\r\n\r\n    public static void main(String[] args) {\r\n        AWSCredentials credentials = new BasicAWSCredentials((\"yourAccessKeyId\", \"yourSecretAccessKey\");\r\n        AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials);\r\n        client.setEndpoint(\"http:\/\/localhost:8000\", \"\", \"local\");\r\n\r\n        DynamoDBMapper mapper = new DynamoDBMapper(client);\r\n\r\n        Channel channel = new Channel();\r\n        channel.setChannelName(\"techscore\");\r\n        channel.setCreatedAt(OffsetDateTime.now());\r\n        channel.setUpdatedAt(OffsetDateTime.now());\r\n        channel.setMaxMessageNumber(0);\r\n\r\n        mapper.save(channel);\r\n    }\r\n\r\n}\r\n<\/pre>\n<p><code>DynamoDBClient<\/code> \u306e\u751f\u6210\u307e\u3067\u306f\u4f4e\u30ec\u30d9\u30eb API \u306e\u6642\u3068\u540c\u3058\u3067\u3059\u3002<\/p>\n<p>\u5c1a\u3001<code>DynamoDBMapper<\/code> \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u30b9\u30ec\u30c3\u30c9\u30bb\u30fc\u30d5\u306a\u306e\u3067\u3001\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u30b9\u30ec\u30c3\u30c9\u9593\u3067\u5171\u6709\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<p><a title=\"DynamoDBMapper\" href=\"http:\/\/docs.aws.amazon.com\/AWSJavaSDK\/latest\/javadoc\/com\/amazonaws\/services\/dynamodbv2\/datamodeling\/DynamoDBMapper.html\" target=\"_blank\">DynamoDBMapper<\/a><\/p>\n<blockquote style=\"font-style: italic;\"><p>This class is thread-safe and can be shared between threads. It's also very lightweight, so it doesn't need to be.<\/p><\/blockquote>\n<h2>Conditional Writes<\/h2>\n<p>DynamoDB \u3067\u306f Conditional Writes \u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u30a2\u30a4\u30c6\u30e0\u3084\u5c5e\u6027\u306e\u66f4\u65b0\u3092\u6761\u4ef6\u4ed8\u304d\u3067\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<p><a title=\"ConditionalUpdate\" href=\"http:\/\/docs.aws.amazon.com\/amazondynamodb\/latest\/developerguide\/WorkingWithItems.html#WorkingWithItems.ConditionalUpdate\" target=\"_blank\">Conditional Writes<\/a><\/p>\n<blockquote style=\"font-style: italic;\"><p>To help clients coordinate writes to data items, DynamoDB supports conditional writes for PutItem, DeleteItem, and UpdateItem operations. With a conditional write, an operation succeeds only if the item attributes meet one or more expected conditions; otherwise it returns an error.<\/p><\/blockquote>\n<p>\u3053\u308c\u306f\u30de\u30eb\u30c1\u30e6\u30fc\u30b6\u30fc\u74b0\u5883\u3067\u540c\u3058\u5c5e\u6027\u306b\u540c\u6642\u30a2\u30af\u30bb\u30b9\u3055\u308c\u305f\u6642\u306e\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u3092\u5236\u5fa1\u3059\u308b\u3082\u306e\u3067\u3059\u3002\u4eca\u56de\u306f\u8eab\u8fd1\u306a\u30b5\u30f3\u30d7\u30eb\u3068\u3057\u3066\u3001\u540c\u3058\u30d7\u30e9\u30a4\u30de\u30ea\u30ad\u30fc\u3092\u6301\u3064\u30a2\u30a4\u30c6\u30e0\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306e\u307f\u30a2\u30a4\u30c6\u30e0\u3092\u65b0\u898f\u4fdd\u5b58\u3059\u308b\u30b3\u30fc\u30c9\u3092\u8003\u3048\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u306f\u4f4e\u30ec\u30d9\u30eb API \u306e <code>PutItemRequest<\/code> \u3084\u9ad8\u30ec\u30d9\u30eb API \u306e <code>save()<\/code> \u30e1\u30bd\u30c3\u30c9\u306f\u4e00\u81f4\u3059\u308b\u30d7\u30e9\u30a4\u30de\u30ea\u30ad\u30fc\u3092\u6301\u3064\u30a2\u30a4\u30c6\u30e0\u304c\u5b58\u5728\u3057\u306a\u3051\u308c\u3070\u30a2\u30a4\u30c6\u30e0\u3092\u65b0\u898f\u4fdd\u5b58\u3057\u3001\u5b58\u5728\u3059\u308c\u3070\u30a2\u30a4\u30c6\u30e0\u3084\u5c5e\u6027\u3092\u66f4\u65b0\u3057\u307e\u3059\u3002\u3053\u308c\u3092\u65b0\u898f\u4fdd\u5b58\u306e\u307f\u306e\u64cd\u4f5c\u306b\u9650\u5b9a\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>1. \u4f4e\u30ec\u30d9\u30eb API \u3067\u306e\u4f8b<\/h3>\n<p>\u4f4e\u30ec\u30d9\u30eb API \u3067\u306e\u30b5\u30f3\u30d7\u30eb\u3068\u3057\u3066<a title=\"ChannelCreationLowLevelAPI.java\" href=\"http:\/\/www.techscore.com\/blog\/2014\/12\/12\/amazon-dynamodb-local-and-aws-sdk-for-java-1\/#ChannelCreationLowLevelAPI.java\">\u524d\u56de\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/a>\u3092\u6d41\u7528\u3057\u307e\u3059\u3002<\/p>\n<p>\u30a2\u30a4\u30c6\u30e0\u4fdd\u5b58\u6642\u306e\u6761\u4ef6\u306b\u300c\u30c6\u30fc\u30d6\u30eb\u5185\u306b\u65b0\u3057\u3044\u30a2\u30a4\u30c6\u30e0\u306e channelName \u5c5e\u6027\u5024\u3068\u4e00\u81f4\u3059\u308b\u30a2\u30a4\u30c6\u30e0\u304c\u7121\u3044\u300d\u3068\u3044\u3046\u6761\u4ef6\u3092\u52a0\u3048\u308b\u306b\u306f <code>PutItemRequest<\/code> \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b <code>addExpectedEntry()<\/code> \u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true\">.addExpectedEntry(\"channelName\", new ExpectedAttributeValue().withExists(false));\r\n<\/pre>\n<p><code>ExpectedAttributeValue<\/code> \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b <code>withExists(false)<\/code> \u3068\u3059\u308b\u3053\u3068\u3067\u300c\u6307\u5b9a\u3057\u305f\u5c5e\u6027\u5024\u304c\u4e00\u81f4\u3059\u308b\u30a2\u30a4\u30c6\u30e0\u304c\u5b58\u5728\u3057\u306a\u3044\u3053\u3068\u300d\u3068\u3044\u3046\u6761\u4ef6\u3092\u8868\u73fe\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u5168\u4f53\u3068\u3057\u3066\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><a name=\"ChannelCreationLowLevelAPI.java\"><\/a>com\/techscore\/dynamodb_local_sample\/ChannelCreationLowLevelAPI.java<\/p>\n<pre class=\"lang:java decode:true\">package com.techscore.dynamodb_local_sample;\r\n\r\nimport java.time.OffsetDateTime;\r\nimport com.amazonaws.auth.AWSCredentials;\r\nimport com.amazonaws.auth.BasicAWSCredentials;\r\nimport com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;\r\nimport com.amazonaws.services.dynamodbv2.model.AttributeValue;\r\nimport com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue;\r\nimport com.amazonaws.services.dynamodbv2.model.PutItemRequest;\r\n\r\npublic class ChannelCreationLowLevelAPI {\r\n\r\n    public static void main(String[] args) {\r\n        AWSCredentials credentials = new BasicAWSCredentials((\"yourAccessKeyId\", \"yourSecretAccessKey\");\r\n        AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials);\r\n        client.setEndpoint(\"http:\/\/localhost:8000\", \"\", \"local\");\r\n\r\n        PutItemRequest request = new PutItemRequest()\r\n                .withTableName(\"Channels\")\r\n                .addItemEntry(\"channelName\", new AttributeValue(\"techscore\"))\r\n                .addItemEntry(\"maxMessageNumber\", new AttributeValue().withN(\"0\"))\r\n                .addItemEntry(\"createdAt\", new AttributeValue(OffsetDateTime.now().toString()))\r\n                .addItemEntry(\"updatedAt\", new AttributeValue(OffsetDateTime.now().toString()))\r\n                .addExpectedEntry(\"channelName\", new ExpectedAttributeValue().withExists(false));\r\n        client.putItem(request);\r\n    }\r\n\r\n}\r\n<\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001Channels \u30c6\u30fc\u30d6\u30eb\u5185\u306b channelName \u5c5e\u6027\u5024\u304c <code>\"techscore\"<\/code> \u3067\u3042\u308b\u30a2\u30a4\u30c6\u30e0\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u3001<code>ConditionalCheckFailedException<\/code> \u304c\u30b9\u30ed\u30fc\u3055\u308c\u30a2\u30a4\u30c6\u30e0\u306e\u4fdd\u5b58\u306b\u5931\u6557\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:shell decode:true\">Exception in thread \"main\" com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException: The conditional request failed (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ConditionalCheckFailedException; Request ID: 6353db04-93b6-48e7-9df8-0be8dd8516a7)\r\n    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1077)\r\n    at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:725)\r\n    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:460)\r\n    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:295)\r\n    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:3106)\r\n    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.putItem(AmazonDynamoDBClient.java:1206)\r\n    at com.techscore.dynamodb_local_sample.ChannelCreationLowLevelAPI.main(ChannelCreationLowLevelAPI.java:25)\r\n<\/pre>\n<h3>2. \u9ad8\u30ec\u30d9\u30eb API \u3067\u306e\u4f8b<\/h3>\n<p>\u9ad8\u30ec\u30d9\u30eb API \u3067\u306f <code>DynamoDBSaveExpression<\/code> \u30af\u30e9\u30b9\u306e <code>withExpectedEntry()<\/code> \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true\">DynamoDBSaveExpression saveExpr = new DynamoDBSaveExpression()\r\n        .withExpectedEntry(\"channelName\", new ExpectedAttributeValue().withExists(false));\r\n<\/pre>\n<p>\u4f4e\u30ec\u30d9\u30eb API \u306e\u6642\u3068\u540c\u69d8\u3001<code>ExpectedAttributeValue<\/code> \u3067 <code>withExists(false)<\/code> \u3068\u3059\u308b\u3053\u3068\u3067\u3001\u6307\u5b9a\u3057\u305f\u5c5e\u6027\u306b\u8a72\u5f53\u3059\u308b\u30a2\u30a4\u30c6\u30e0\u304c\u5b58\u5728\u3057\u306a\u3044\u3053\u3068\u3068\u3044\u3046\u6761\u4ef6\u3092\u8868\u73fe\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001<code>DynamoDBMapper<\/code> \u306e <code>save()<\/code> \u30e1\u30bd\u30c3\u30c9\u306f 2 \u3064\u76ee\u306e\u5f15\u6570\u3068\u3057\u3066 <code>DynamoDBSaveExpression<\/code> \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6e21\u305b\u308b\u3082\u306e\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true\">mapper.save(channel, saveExpr);\r\n<\/pre>\n<p>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u5168\u4f53\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><a href=\"ChannelCreationHighLevelAPI.java.2\">ChannelCreationHighLevelAPI.java<\/a><\/p>\n<pre class=\"lang:java decode:true\">package com.techscore.dynamodb_local_sample;\r\n\r\nimport java.time.OffsetDateTime;\r\nimport com.amazonaws.auth.AWSCredentials;\r\nimport com.amazonaws.auth.BasicAWSCredentials;\r\nimport com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;\r\nimport com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;\r\nimport com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression;\r\nimport com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue;\r\n\r\npublic class ChannelCreationHighLevelAPI {\r\n\r\n    public static void main(String[] args) {\r\n        AWSCredentials credentials = new BasicAWSCredentials(\"yourAccessKeyId\", \"yourSecretAccessKey\");\r\n        AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials);\r\n        client.setEndpoint(\"http:\/\/localhost:8000\", \"\", \"local\");\r\n\r\n        DynamoDBMapper mapper = new DynamoDBMapper(client);\r\n\r\n        Channel channel = new Channel();\r\n        channel.setChannelName(\"techscore\");\r\n        channel.setCreatedAt(OffsetDateTime.now());\r\n        channel.setUpdatedAt(OffsetDateTime.now());\r\n        channel.setMaxMessageNumber(0);\r\n\r\n        DynamoDBSaveExpression saveExpr = new DynamoDBSaveExpression()\r\n                .withExpectedEntry(\"channelName\", new ExpectedAttributeValue().withExists(false));\r\n\r\n        mapper.save(channel, saveExpr);\r\n    }\r\n}\r\n<\/pre>\n<h2>Atomic Counters<\/h2>\n<p>Atomic Counters \u306f\u30a2\u30a4\u30c6\u30e0\u66f4\u65b0\u6642\u306b\u5c5e\u6027\u5024\u3092\u30a2\u30c8\u30df\u30c3\u30af\u306b\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\uff0f\u30c7\u30af\u30ea\u30e1\u30f3\u30c8\u3059\u308b\u6a5f\u80fd\u3067\u3059\u3002<\/p>\n<p><a title=\"AtomicCounters\" href=\"http:\/\/docs.aws.amazon.com\/amazondynamodb\/latest\/developerguide\/WorkingWithItems.html#WorkingWithItems.AtomicCounters\" target=\"_blank\">Atomic Counters<\/a><\/p>\n<blockquote><p>DynamoDB supports atomic counters, where you use the UpdateItem operation to increment or decrement the value of an existing attribute without interfering with other write requests.<\/p><\/blockquote>\n<p>\u3053\u306e\u6a5f\u80fd\u306f\u73fe\u6642\u70b9\u3067\u9ad8\u30ec\u30d9\u30eb API \u306b\u306f\u5b9f\u88c5\u3055\u308c\u3066\u304a\u3089\u305a\u3001\u4f4e\u30ec\u30d9\u30eb API \u3067\u306e\u307f\u5229\u7528\u53ef\u80fd\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u306f <code>UpdateItemRequest<\/code> \u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p><code>UpdateItemRequest<\/code> \u306f <code>PutItemRequest<\/code> \u3068\u9055\u3044\u3001\u66f4\u65b0\u6642\u306b\u30a2\u30a4\u30c6\u30e0\u5168\u4f53\u3092\u7f6e\u304d\u63db\u3048\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u6307\u5b9a\u3057\u305f\u5c5e\u6027\u306e\u307f\u3092\u66f4\u65b0\u3057\u3066\u304f\u308c\u307e\u3059\uff08\u9ad8\u30ec\u30d9\u30eb API \u3067\u3082 <code>save()<\/code> \u30e1\u30bd\u30c3\u30c9\u306b\u5f15\u6570\u3068\u3057\u3066 <code>DynamoDBMapperConfig<\/code> \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6e21\u3059\u3053\u3068\u3067\u4fdd\u5b58\u6642\u306e\u6319\u52d5\u3092\u5909\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff09\u3002<\/p>\n<p>\u307e\u305a\u306f\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u5168\u4f53\u304b\u3089\u3002<\/p>\n<p><a name=\"ChannelModificationLowLevelAPI.java\"><\/a>com\/techscore\/dynamodb_local_sample\/ChannelModificationLowLevelAPI.java<\/p>\n<pre class=\"lang:java decode:true\">package com.techscore.dynamodb_local_sample;\r\n\r\nimport java.time.OffsetDateTime;\r\nimport java.util.Map;\r\nimport com.amazonaws.auth.AWSCredentials;\r\nimport com.amazonaws.auth.BasicAWSCredentials;\r\nimport com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;\r\nimport com.amazonaws.services.dynamodbv2.model.AttributeAction;\r\nimport com.amazonaws.services.dynamodbv2.model.AttributeValue;\r\nimport com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;\r\nimport com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue;\r\nimport com.amazonaws.services.dynamodbv2.model.ReturnValue;\r\nimport com.amazonaws.services.dynamodbv2.model.UpdateItemRequest;\r\nimport com.amazonaws.services.dynamodbv2.model.UpdateItemResult;\r\n\r\npublic class ChannelModificationLowLevelAPI {\r\n\r\n    public static void main(String[] args) {\r\n        AWSCredentials credentials = new BasicAWSCredentials(\"yourAccessKeyId\", \"yourSecretAccessKey\");\r\n        AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials);\r\n        client.setEndpoint(\"http:\/\/localhost:8000\", \"\", \"local\");\r\n\r\n        UpdateItemRequest request = new UpdateItemRequest()\r\n                .withTableName(\"Channels\")\r\n                .addKeyEntry(\"channelName\", new AttributeValue(\"techscore\"))\r\n                .addAttributeUpdatesEntry(\"maxMessageNumber\",\r\n                        new AttributeValueUpdate()\r\n                                .withAction(AttributeAction.ADD)\r\n                                .withValue(new AttributeValue().withN(\"1\")))\r\n                .addAttributeUpdatesEntry(\"updatedAt\",\r\n                        new AttributeValueUpdate()\r\n                                .withValue(new AttributeValue(OffsetDateTime.now().toString())))\r\n                .addExpectedEntry(\"channelName\",\r\n                        new ExpectedAttributeValue()\r\n                                .withExists(true)\r\n                                .withValue(new AttributeValue(\"techscore\")))\r\n                .withReturnValues(ReturnValue.UPDATED_NEW);\r\n\r\n        UpdateItemResult result = client.updateItem(request);\r\n    }\r\n}\r\n<\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u3067\u306f Conditional Writes \u3067\u30d7\u30e9\u30a4\u30de\u30ea\u30ad\u30fc\u304c\u4e00\u81f4\u3059\u308b\u30a2\u30a4\u30c6\u30e0\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u306e\u307f\u5c5e\u6027\u3092\u66f4\u65b0\u3059\u308b\u3088\u3046\u5236\u9650\u3057\u3001Atomic Counters \u3067 maxMessageNumber \u5c5e\u6027\u5024\u3092\u5897\u5206 1 \u3067\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><code>AttributeValueUpdate<\/code> \u30af\u30e9\u30b9\u306e <code>withAction()<\/code> \u30e1\u30bd\u30c3\u30c9\u306b <code>AttributeAction.ADD<\/code> \u3092\u6e21\u3059\u3053\u3068\u3067\u5c5e\u6027\u5024\u3092\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3059\u308b\u3053\u3068\u3092\u6307\u793a\u3057\u307e\u3059\u3002\u305d\u306e\u5f8c\u306e <code>withValue()<\/code> \u306f\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u306e\u5897\u5206\u304c 1 \u3067\u3042\u308b\u3053\u3068\u3092\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><code>PutItemRequest<\/code> \u306e\u6642\u3068\u540c\u69d8\u306b\u3001<code>addExpectedEntry()<\/code> \u30e1\u30bd\u30c3\u30c9\u3067 Conditional Writes \u306b\u95a2\u3059\u308b\u64cd\u4f5c\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><code>withReturnValues()<\/code> \u30e1\u30bd\u30c3\u30c9\u306f\u3001<code>DynamoDBClient<\/code> \u306e <code>updateItem()<\/code> \u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u306b\u95a2\u3059\u308b\u8a2d\u5b9a\u3067\u3001<code>ReturnValue.UPDATED_NEW<\/code> \u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u66f4\u65b0\u5f8c\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u623b\u308a\u5024\u306b\u30bb\u30c3\u30c8\u3059\u308b\u3088\u3046\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u6b21\u306e\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3055\u308c\u305f maxMessageNumber \u5c5e\u6027\u5024\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true\">Map&lt;String, AttributeValue&gt; attributes = result.getAttributes();\r\nString messageNumber = attributes.get(\"maxMessageNumber\").getN();\r\n<\/pre>\n<p><code>AttributeValue<\/code> \u3067\u306e\u5024\u64cd\u4f5c\u3067\u306f\u5168\u822c\u7684\u306b <code>String<\/code> \u3057\u304b\u4f7f\u3048\u306a\u3044\u306e\u304c\u6b8b\u5ff5\u306a\u3068\u3053\u308d\u3067\u3059\u30fb\u30fb\u30fb\u3002<\/p>\n<h2>\u6700\u5f8c\u306b<\/h2>\n<p>\u4eca\u56de\u306f DynamoDB Local \u3068\u306f\u95a2\u4fc2\u306e\u306a\u3044\u8a18\u4e8b\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3002\u677e\u672c\u3067\u3059\u3002<br \/>\nTECHSCORE Advent Calendar 2014 \u306e 24 \u65e5\u76ee\u306e\u6295\u7a3f\u3067\u3059\u3002<\/p>\n<p>\u524d\u56de\u306f eclipse \u4e0a\u3067 Amazon DynamoDB Local \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3001AWS SDK for Java \u304b\u3089\u30a2\u30a4\u30c6\u30e0\u306e\u4fdd\u5b58\u3092\u884c\u3044\u307e\u3057\u305f\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2014\/12\/24\/amazon-dynamodb-local-and-aws-sdk-for-java-2\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[141,119,57],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/5172"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=5172"}],"version-history":[{"count":84,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/5172\/revisions"}],"predecessor-version":[{"id":5608,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/5172\/revisions\/5608"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=5172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=5172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=5172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}