|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <meta charset='utf-8'> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="chrome=1"> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 7 | + <!--link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"--> |
| 8 | + <link rel="stylesheet" href="/gfx/bootstrap.min.css"> |
| 9 | + <link rel="stylesheet" href="/gfx/main.css"> |
| 10 | + <link rel="stylesheet" href="/gfx/code.css"> |
| 11 | + <title>DynamoDB V1 SDK integration</title> |
| 12 | +</head> |
| 13 | +<body class="page"> |
| 14 | +<!-- Google Tag Manager --> |
| 15 | +<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-PMJSKV" |
| 16 | +height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> |
| 17 | +<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': |
| 18 | +new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], |
| 19 | +j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= |
| 20 | +'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); |
| 21 | +})(window,document,'script','dataLayer','GTM-PMJSKV');</script> |
| 22 | +<!-- End Google Tag Manager --> |
| 23 | + |
| 24 | +<header> |
| 25 | + <div class="container"> |
| 26 | + <a href="/">Immutables</a> ← |
| 27 | + |
| 28 | + <h1>DynamoDB V1 SDK integration <iframe src="https://ghbtns.com/github-btn.html?user=immutables&repo=immutables&type=star&count=true&size=large" frameborder="0" scrolling="0" width="160px" height="30px"></iframe> |
| 29 | +</h1> |
| 30 | + </div> |
| 31 | +</header> |
| 32 | +<aside id="toc"></aside> |
| 33 | +<section class="documentation"> |
| 34 | + <h2 id="overview">Overview</h2> |
| 35 | + |
| 36 | +<p><a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.html"><code class="language-plaintext highlighter-rouge">DynamoDBMapper</code></a> provides an easy way to map POJOs to data in <a href="https://docs.aws.amazon.com/dynamodb/index.html"><code class="language-plaintext highlighter-rouge">DynamoDB</code></a>. |
| 37 | +Most of the code POJOs are verbose and it makes a perfect sense to use <em>Immutables</em> to reduce boilerplate.</p> |
| 38 | + |
| 39 | +<h2 id="how-to-use">How to use</h2> |
| 40 | + |
| 41 | +<p><strong>NOTE</strong>: provided snippets show the idea that could be improved and / or modified to address specific needs.</p> |
| 42 | + |
| 43 | +<p>In the project with more than one entity it makes sense to create custom <a href="/style.html"><code class="language-plaintext highlighter-rouge">Style</code></a> that would allow |
| 44 | +to enable <em>Immutables</em> for POJO.</p> |
| 45 | + |
| 46 | +<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nd">@Target</span><span class="o">(</span><span class="nc">ElementType</span><span class="o">.</span><span class="na">TYPE</span><span class="o">)</span> |
| 47 | +<span class="nd">@Retention</span><span class="o">(</span><span class="nc">RetentionPolicy</span><span class="o">.</span><span class="na">CLASS</span><span class="o">)</span> |
| 48 | +<span class="nd">@Value</span><span class="o">.</span><span class="na">Style</span><span class="o">(</span> |
| 49 | + <span class="n">create</span> <span class="o">=</span> <span class="s">"new"</span><span class="o">,</span> |
| 50 | + <span class="n">isInitialized</span> <span class="o">=</span> <span class="s">"wasInitialized"</span><span class="o">,</span> |
| 51 | + <span class="n">allParameters</span> <span class="o">=</span> <span class="kc">true</span><span class="o">,</span> |
| 52 | + <span class="n">beanFriendlyModifiables</span> <span class="o">=</span> <span class="kc">true</span><span class="o">,</span> |
| 53 | + <span class="n">passAnnotations</span> <span class="o">=</span> <span class="o">{</span> |
| 54 | + <span class="nc">DynamoDBTable</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> |
| 55 | + <span class="nc">DynamoDBHashKey</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> |
| 56 | + <span class="nc">DynamoDBRangeKey</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> |
| 57 | + <span class="nc">DynamoDBAttribute</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> |
| 58 | + <span class="nc">DynamoDBTyped</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> |
| 59 | + <span class="nc">DynamoDBTypeConvertedEnum</span><span class="o">.</span><span class="na">class</span> |
| 60 | + <span class="c1">// more DynamoDB annotations to be added</span> |
| 61 | + <span class="o">}</span> |
| 62 | +<span class="o">)</span> |
| 63 | +<span class="kd">public</span> <span class="nd">@interface</span> <span class="nc">DynamoDBStyle</span> <span class="o">{}</span> |
| 64 | +</code></pre></div></div> |
| 65 | + |
| 66 | +<p>Application of the newly created style could look like:</p> |
| 67 | + |
| 68 | +<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nd">@DynamoDBStyle</span> |
| 69 | +<span class="nd">@Value</span><span class="o">.</span><span class="na">Immutable</span> |
| 70 | +<span class="nd">@Value</span><span class="o">.</span><span class="na">Modifiable</span> |
| 71 | +<span class="nd">@DynamoDBTable</span><span class="o">(</span><span class="n">tableName</span> <span class="o">=</span> <span class="s">"people"</span><span class="o">)</span> |
| 72 | +<span class="kd">public</span> <span class="kd">interface</span> <span class="nc">Person</span> <span class="o">{</span> |
| 73 | + <span class="nd">@DynamoDBHashKey</span><span class="o">(</span><span class="n">attributeName</span> <span class="o">=</span> <span class="s">"name"</span><span class="o">)</span> |
| 74 | + <span class="nc">String</span> <span class="nf">getName</span><span class="o">();</span> |
| 75 | + |
| 76 | + <span class="nd">@DynamoDBAttribute</span><span class="o">(</span><span class="n">attributeName</span> <span class="o">=</span> <span class="s">"age"</span><span class="o">)</span> |
| 77 | + <span class="kt">int</span> <span class="nf">getAge</span><span class="o">();</span> |
| 78 | +<span class="o">}</span> |
| 79 | +</code></pre></div></div> |
| 80 | + |
| 81 | +<p>We apply both <code class="language-plaintext highlighter-rouge">@Value.Immutable</code> and <code class="language-plaintext highlighter-rouge">@Value.Modifiable</code>. The former is bread and butter |
| 82 | +of <em>Immutables</em> as it provides all nice features while the latter enables usage |
| 83 | +of generated classes with <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.html"><code class="language-plaintext highlighter-rouge">DynamoDBMapper</code></a>. <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.html"><code class="language-plaintext highlighter-rouge">DynamoDBMapper</code></a> |
| 84 | +expects the POJO to have no-arg ctor with setters and newly created <code class="language-plaintext highlighter-rouge">DynamoDBStyle</code> |
| 85 | +annotation configures <em>Immutables</em> to generated the required structure.</p> |
| 86 | + |
| 87 | +<p>The usage is pretty straightforward with only one caveat: <code class="language-plaintext highlighter-rouge">Modifiable...</code> implementation |
| 88 | +should be used any time data is loaded from the <a href="https://docs.aws.amazon.com/dynamodb/index.html"><code class="language-plaintext highlighter-rouge">DynamoDB</code></a>.</p> |
| 89 | + |
| 90 | +<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// assuming mapper is an instance of `DynamoDBMapper`</span> |
| 91 | + |
| 92 | +<span class="nc">Person</span> <span class="n">john</span> <span class="o">=</span> <span class="nc">ImmutablePerson</span><span class="o">.</span><span class="na">of</span><span class="o">(</span><span class="s">"John"</span><span class="o">,</span> <span class="mi">42</span><span class="o">);</span> |
| 93 | + |
| 94 | +<span class="n">mapper</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="n">john</span><span class="o">);</span> |
| 95 | +<span class="n">john</span> <span class="o">=</span> <span class="n">mapper</span><span class="o">.</span><span class="na">load</span><span class="o">(</span><span class="nc">ModifiablePerson</span><span class="o">.</span><span class="na">class</span><span class="o">,</span> <span class="s">"John"</span><span class="o">);</span> |
| 96 | + |
| 97 | +<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">john</span><span class="o">);</span> <span class="c1">// prints ModifiablePerson{name=John, age=42}</span> |
| 98 | +</code></pre></div></div> |
| 99 | + |
| 100 | + |
| 101 | +</section> |
| 102 | +<footer class="jumbotron"> |
| 103 | + <div class="container"> |
| 104 | + <h2 id="guides">Guides</h2> |
| 105 | + |
| 106 | +<ul> |
| 107 | + <li><a href="/getstarted.html">Get started!</a></li> |
| 108 | + <li><a href="/intro.html">Inception</a></li> |
| 109 | + <li><a href="/immutable.html">Immutable objects</a></li> |
| 110 | + <li><a href="/factory.html">Factory builders</a></li> |
| 111 | + <li><a href="/functional.html">Functions and Predicates (for Java 7)</a></li> |
| 112 | + <li><a href="/style.html">Style customization</a></li> |
| 113 | + <li><a href="/json.html">JSON serialization</a></li> |
| 114 | + <li><a href="/criteria.html">Criteria</a></li> |
| 115 | + <li><a href="/mongo.html">MongoDB repositories</a></li> |
| 116 | + <li><a href="/dynamodb.html">DynamoDB integration</a></li> |
| 117 | + <li><a href="/encoding.html">Encoding: Customizing attributes and builders (experimental)</a></li> |
| 118 | + <li><a href="/apt.html">Using annotation processor in IDE</a></li> |
| 119 | +</ul> |
| 120 | + |
| 121 | +<h2 id="get-involved">Get involved</h2> |
| 122 | + |
| 123 | +<ul> |
| 124 | + <li>Clone source repository, contribute bug reports and fixes on <a href="https://github.com/immutables/immutables">GitHub immutables/immutables</a></li> |
| 125 | + <li>Issue reports, questions and feedback is welcome on issue tracker <a href="https://github.com/immutables/immutables/issues">GitHub immutables/immutables/issues</a></li> |
| 126 | + <li>News and announcements on twitter <a href="https://twitter.com/ImmutablesOrg">@ImmutablesOrg</a></li> |
| 127 | +</ul> |
| 128 | + |
| 129 | +<p><a href="/license.html">Apache License 2.0</a></p> |
| 130 | + |
| 131 | + <!--<div><h2>Posts</h2> |
| 132 | + <ul> |
| 133 | + |
| 134 | + </ul> |
| 135 | + </div>--> |
| 136 | + </div> |
| 137 | +</footer> |
| 138 | +<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> |
| 139 | +<script defer src="/gfx/jquery.toc.min.js"></script> |
| 140 | +<script> |
| 141 | +$(function() { |
| 142 | + $('#toc').toc({ |
| 143 | + container: '.documentation', |
| 144 | + selectors: 'h1,h2,h3,h4', |
| 145 | + anchorName: function(i, heading, prefix) { |
| 146 | + heading = $(heading).text(); |
| 147 | + if (heading.trim) heading = heading.trim(); |
| 148 | + return heading.toLowerCase().replace(/ /g, '-').replace(/[^a-z^\-]+/g, ''); |
| 149 | + }, |
| 150 | + }) |
| 151 | +}) |
| 152 | +</script> |
| 153 | +</body> |
| 154 | +</html> |
0 commit comments