{
"$type": "site.standard.document",
"canonicalUrl": "https://numergent.com/2016-01/Clojure-and-DynamoDB-with-Faraday-part-2.html",
"path": "/2016-01/Clojure-and-DynamoDB-with-Faraday-part-2.html",
"publishedAt": "2016-01-07T08:06:35.000Z",
"site": "at://did:plc:cf6futaebyc2k4wgzsr4v42k/site.standard.publication/3mp2ewx43js2g",
"tags": [
"aws",
"clojure",
"document databases",
"dynamodb",
"faraday",
"nosql"
],
"textContent": "The story so far\n\nOn Part 1 we went over the basic operations - creating a table, checking its status, getting data in and performing a simple retrieval.\n\nWe'll now look into various ways of retrieving items, including querying, scanning, and using projections to get only a few properties.\n\nThis assumes you've already completed part 1, since we'll be using the data we added. As a reminder, I'm following the Javascript examples on basic DynamoDB operations, since they are the ones where the data set-up will more closely match Clojure. You may want to follow along for extra explanations and for comparison purposes.\n\n\n\nStep 4: Read an Item Using Its Primary Key\n\nThe next step on DynamoDB's Getting Started is to retrieve items by their key. \n\nRead an item using get-item\n\nWe already skipped ahead a bit on the last part and saw how to use get-item. To refresh our memories, get-item receives a table and a map of the key to retrieve (using attribute and value).\n\nNotice we need to use both the values for the key, and if your key is a string, it'll be case-sensitive.\n\nIt returns the item as a map, including all attributes.\n\nRetrieve a subset of attributes\n\nDynamoDB also allows us to retrieve only a few attributes. To retrieve only the album title and year:\n\nWhich gets us a smaller map.\n\nRetrieve a subset of attributes using a projection expression\n\nWe used a legacy parameter up there instead of an expression, though. Expressions are a lot more flexible than just sending an attribute list, but have some restrictions.\n\nMainly, when using expressions:\n\n You can't have attribute names with dashes, and\n You can't use reserved words.\n\nWe are hitting both cases on the previous call. album-title has a dash on the name, and year is a reserved word.\n\nThe way around that is to add a placeholder token on the expression that stands in for the attribute, and then adding the attribute name on the expression attribute values.\n\nThe equivalent call to the previous example, using a projection expression, would be:\n\nThat would seem rather cumbersome if we can simply request the attributes, but the next example will give you a better idea of what you can do with expressions.\n\nRetrieve nested attributes using path notation\n\nYou'll recall that when we created the item, we added a complex attribute :tags which was itself a map.\n\nOn a projection expression, we can use path notation to retrieve only some attributes inside :tags, even down to requesting a specific item inside the :composers vector.\n\nLike before, we'll need to use expression attributes for those elements that have a dash in the name.\n\nThis returns only those attributes we requested, retaining their original nesting.\n\nRead multiple items using batch-get-item\n\nWe can also retrieve multiple items in a single call by their primary key, using batch-get-item. Like we saw on the call to batch-write-item (part 1), we can also requests items from multiple tables by adding a request for each one to the map.\n\nThis results on us just getting the relevant attributes for those elements, organized by table. If a requested attribute isn't preset on the item, there will not be a corresponding key on the return map.\n\nYou can't assume that the items will be returned on the same order as the request, so unlike in the example above you'll want to include the key as well (I left it out to directly reproduce Amazon's example).\n\nStep 5: Query and Scan the Table\n\nWe can also do queries and scans on tables. The syntax for these will be very similar to the one we have been using up until now.\n\nRun a query\n\nWe can query on a table using its partition key.\n\nThis will return all items, sorting by the range key (the song's title).\n\nWe can also filter using key attributes, and ask DynamoDB to return only a few of them. To get only songs for \"The Acme Band\" where the title starts with a character:\n\nFiltering query results\n\nQuerying looks for items based on its primary key (or a fraction of it). We can also use filter expressions to filter out some items from those that match, using even complex expressions and nested values. \n\nOn the following example, we'll need to use both expression attribute values and expression attribute names, since they have the same limitations as the expressions we used for projections, and we can't reference on them attributes with dashes on the name.\n\nInstead of adding the filtering values directly on the filter expression, we used an expression attribute values map - passed via :expr-attr-vals - to provide them.\n\nThis query results on a vector with the requested attributes for the single matching item.\n\nScan the table\n\nFinally, we can do always do a full table scan (which will likely get expensive!).\n\nscan supports filter and projection expressions, just like query, but you don't need to know the primary key in order to do it.\n\nNext steps\n\nThat's it for retrieving, querying and scanning. On part 3 we'll look at maintaining and working with secondary indexes. Until then!",
"title": "Clojure and DynamoDB with Faraday, Part 2"
}