<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Emiya</title>
    <description>The latest articles on DEV Community by Emiya (@emiya).</description>
    <link>https://dev.to/emiya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F131186%2F4aeea284-cac9-48cc-bfce-ce91abda55fb.png</url>
      <title>DEV Community: Emiya</title>
      <link>https://dev.to/emiya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emiya"/>
    <language>en</language>
    <item>
      <title>Querying AWS DynamoDB on Non-key Columns</title>
      <dc:creator>Emiya</dc:creator>
      <pubDate>Wed, 23 Jan 2019 03:04:06 +0000</pubDate>
      <link>https://dev.to/emiya/querying-aws-dynamodb-on-non-key-columns-44cj</link>
      <guid>https://dev.to/emiya/querying-aws-dynamodb-on-non-key-columns-44cj</guid>
      <description>&lt;h1&gt;
  
  
  Your client loves AWS DynamoDB
&lt;/h1&gt;

&lt;p&gt;Yes, the Amazon's NoSQL database service is &lt;em&gt;a key-value and document database&lt;/em&gt;, which anyone who has Googled it should already know. Yet, your client demands querying DynamoDB on non-key columns without the actual keys, as they may have had some constraints on which services to use for their projects, or perhaps just didn't think through.&lt;/p&gt;

&lt;p&gt;Fortunately, AWS DynamoDB provides a very quick way to realise such feature: The Global Secondary Index (GSI), which essentially indexes over desired column(s) so that you can later query on them as though they are keys. To do this, simply navigate to your table via the DynamoDB console and click on the &lt;strong&gt;Indexes&lt;/strong&gt; tab, then create indexes with your desired keys.&lt;/p&gt;

&lt;p&gt;Below is a Python example for querying the generated index using Boto3, the AWS SDK for Python.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;boto3&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;boto3.dynamodb.conditions&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Key&lt;/span&gt;

&lt;span class="n"&gt;dynamodb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;boto3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'dynamodb'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;endpoint_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'https://your-dynamodb-endpoint'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dynamodb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'your_table'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;IndexName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'your_index_name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;KeyConditionExpression&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'your_index_key'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'your_index_value'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Alternatively, use Table.scan() with FilterExpression, which obtains the entire table then filters the result, may often be more expensive than indexing.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>dynamodb</category>
      <category>boto3</category>
    </item>
  </channel>
</rss>
