Fork me on GitHub
#aws
<
2020-02-26
>
mbbaldwin06:02:33

I'm trying to save multiple records to DynamoDB using cognitect-labs/aws-api (as per example found here https://github.com/cognitect-labs/aws-api/blob/master/examples/dynamodb_examples.clj) but while it seems that it's set up the same as the examples I'm clearly doing something wrong. The image shows "user errors" which are described as 400 errors due to poor formatting. My code is similar to this:

(def ddb (aws/client {:api :dynamodb}))

(defn save-stuff []
  (aws/invoke ddb {:op :BatchWriteItem,
                   :request
                     {:RequestItems
                      {"twitter-sentiment"
                        [{"PutRequest"
                           {"Item"
                             {"twitter-id" {:S "1232539310127140864"}, "lang" {:S "en"}}}}
                         {"PutRequest"
                           {"Item"
                             {"twitter-id" {:S "1232539306297823232"}, "lang" {:S "en"}}}}]}}}
Any idea what I'm doing wrong?

dchelimsky14:02:34

@ what is the output of invoking save-stuff? Also, what is the metadata of that output? (meta (save-stuff)).

mbbaldwin15:02:14

@ Oh sorry I should have mentioned that (late night with a sick kid put it out of my mind). There is no console output. The metadata for the fn is:

{:http-request
 {:request-method :post,
  :scheme :https,
  :server-port 443,
  :uri "/",
  :headers
  {"x-amz-date" "20200226T154816Z",
   "x-amz-target" "DynamoDB_20120810.BatchWriteItem",
   "content-type" "application/x-amz-json-1.0",
   "host" "",
   "x-amz-security-token" [really long token],
   "authorization" [authorization-stuff]},
  :body
  #object[java.nio.HeapByteBuffer 0x587a1cfb "java.nio.HeapByteBuffer[pos=0 lim=46 cap=46]"],
  :server-name ""},
 :http-response
 {:status 400,
  :headers
  {"server" "Server",
   "connection" "keep-alive",
   "x-amzn-requestid"
   "KTLRO9VJ2L47DJAV84A3QT7UDFVV4KQNSO5AEMVJF66Q9ASUAAJG",
   "x-amz-crc32" "3101795386",
   "content-length" "179",
   "date" "Wed, 26 Feb 2020 15:48:16 GMT",
   "content-type" "application/x-amz-json-1.0"},
  :body
  #object[.BufferedInputStream 0x5fa23c "[email protected]"]}}

mbbaldwin15:02:07

FYI: I'm also not setting up the tables in the code, and the lang field doesn't exist on the table yet. Not sure if that's pertinent or not.

dchelimsky16:02:06

I tried BatchWriteItem with a missing attribute and got back error information.

mbbaldwin16:02:53

A missing attribute? Since DynamoDB is a schema-less db, shouldn't it not matter what objects you put in it (as long as it has the required id specified for the table)? (Meaning you can add attributes or leave them off as needed.)

dchelimsky16:02:26

If I comment out the "Subject" attribute when creating the "Thread" table in the ddb example (https://github.com/cognitect-labs/aws-api/blob/2e4bca75d51e9557608071d061db985e38dc005d/examples/dynamodb_examples.clj#L44), then I get

{:__type "com.amazon.coral.validate#ValidationException",
  :message "Provided list of item keys contains duplicates",
  :cognitect.anomalies/category :cognitect.anomalies/incorrect}
back from WriteBatchItem. Not clear on why that is yet, but the point is that I'm getting some feedback and you're saying you're not getting any.

mbbaldwin16:02:16

Correct. I'm calling it from AWS Lambda, but I'm still expecting something.

dchelimsky16:02:56

Please submit an issue for this, and include all the dependency information, etc.

mbbaldwin16:02:42

Okay, will do when I'm done with work or during lunch.

mbbaldwin17:02:52

An unrelated question I had that I couldn't find any examples for, when adding a sub-map is the proper syntax to add a vector of attribute maps? Like this:

...
{"PutRequest" {"Item" {"id" {:S id}
                       "x" {:M [{:height {:N "16"}} {:name {:S "John"}}]}}}
...

dchelimsky17:02:37

That's json from examples from AWS. In aws-api, keys like "L" need to be keywords like :L .

mbbaldwin17:02:15

Oh, okay. That thought that was "L" for "List". Yes that helps.

mbbaldwin21:02:27

No, thank you! 🙂