amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Querying DynamoDB data with Athena

Open jonperryxlm opened this issue 6 years ago • 10 comments

Which Category is your question related to? Custom

What AWS Services are you utilizing? API Lambda Auth

Provide additional details e.g. code snippets I have a requirement to run complex analytics over the data that we are storing in DynamoDB. Specifically, joining together data across Amplify generated tables in a multi-tenant environment... Finding top performing "factors" per account, per team and per user (amongst other more complex requirements). The specifics about how the data connects in Dynamo isn't necessarily important, what I am struggling with is the best way to get my DynamoDB data into place for querying with Athena (and likely in the future with QuickSight and automated analysis).

There are guides out there for how to provide the results to the user via AppSync (https://aws.amazon.com/blogs/mobile/visualizing-big-data-with-aws-appsync-amazon-athena-and-aws-amplify/), but I can't seem to find much out there to help in getting my data to S3.

So this brings me to the question(s), what method would be best, how would I go about doing it and how should I format the data in S3? The options that I am considered are the following...

  1. DynamoStream (@model backed) => Lambda => S3
  2. DynamoStream (@model backed) => Lambda => Firehose => S3
  3. Glue ETL => S3

Has anyone else gone through a similar scenario? Are there any docs out there that I've missed? I have reached the edge of my experience in getting to this point, so before I embark on another learning curve, I thought it would be best to get some advice.

Thanks in advance!


Just as a note, I am not interested in storing pre-calculated metrics at this point as a lot of the analysis will be exploratory at first. So doing calculations in a Lambda resolver or storing post-calculated metrics off the back of a Dynamo stream is a no-no for us right now.

jonperryxlm avatar Jul 21 '20 23:07 jonperryxlm

I've done a custom resolver and a Lambda function to make a GraphQL query, that saves a DynamoDB document and sends off the query to Athena, which saves the results in S3 which then triggers another Lambda that updates the result in a DynamoDB table, which then does a Subscription update push on the DynamoDB document, which my UI reacts on and then loads the CSV Athena produced using Amplify Storage. It works well overall. Glue is used to map fields and handle casing in my field names in the schema which Athena does not support out of the box without Glue.

It's a bit hacky and complex for what it does, especially around access control due to Amplify not yet supporting Athena in any way. It's using the public preview of Athena Query Federation and has been fairly stable but also some random errors. Somewhat slow especially if you hit Athena with several queries in a short time span (which I do because I have a Dashboard with 20+ charts) because Athena queues them and I did not have the time to build in some type of caching of the results because the original implementation took much longer than expected.

So... It's possible, but not much documentation out there and many trial and errors.

houmark avatar Jul 22 '20 05:07 houmark

Wouldn't it be fantastic if we could create a demo project here? I think this usecase is needed more and more often.

The lack of documentation around analysing your data is something that I've been really shocked by when using Amplify. One of the major benefits to deeply integrating with AWS (or other) is the potential big data capabilities. I appreciate that once we get to the big data (and potentially ML) stage, we will need bespoke implementations, but at the exploratory phase, I really expected more help to get started.

Ours is definitely not a unique requirement.

I've done a custom resolver and a Lambda function to make a GraphQL query, that saves a DynamoDB document and sends off the query to Athena, which saves the results in S3 which then triggers another Lambda that updates the result in a DynamoDB table, which then does a Subscription update push on the DynamoDB document, which my UI reacts on and then loads the CSV Athena produced using Amplify Storage. It works well overall. Glue is used to map fields and handle casing in my field names in the schema which Athena does not support out of the box without Glue.

It's a bit hacky and complex for what it does, especially around access control due to Amplify not yet supporting Athena in any way. It's using the public preview of Athena Query Federation and has been fairly stable but also some random errors. Somewhat slow especially if you hit Athena with several queries in a short time span (which I do because I have a Dashboard with 20+ charts) because Athena queues them and I did not have the time to build in some type of caching of the results because the original implementation took much longer than expected.

So... It's possible, but not much documentation out there and many trial and errors.

So is your solution using Dynamo as a data source in Athena? I've had a brief look at Amazon Athena Query Federation already, but struggled to get Athena to connect to my tables using the provided Lambda App (mainly because I couldn't get the lambda app to appear in Athena "connect data source" wizard). I then decided that I must be misunderstanding the use case that it is intended to support. Is the intention that you don't need to move your data to S3 first, but do still need to manage the schema with Glue?

Once Athena is able to query the data, it seems as though your solution is largely similar to the one outlined in https://aws.amazon.com/blogs/mobile/visualizing-big-data-with-aws-appsync-amazon-athena-and-aws-amplify/ ?

Apologies if I've got completely the wrong end of the stick on that!

jonperryxlm avatar Jul 22 '20 11:07 jonperryxlm

That's correct. No S3 data is moved from DynamoDB, S3 is only leveraged to save the result data CSV from Athena which the UI then reads directly.

Setting it up can be some trial and error and the reason why you may not see the tables is due to either field casing and the lack of Glue or some fields in DynamoDB that crashes the Athena DynamoDB connector. Using Glue to connect and removing fields that are not needed in the mapping may help. I cannot be more specific here as this was a fairly long trial and error for me and your situation may be different. The Athena Federation team has been very helpful and responsive on Github though.

houmark avatar Jul 22 '20 22:07 houmark

Thanks for the info @houmark, it's a huge help while I'm trying to piece together lots of disparate info.

I'm currently looking at my option 2 in more detail because we have 3 environments setup (prod, dev and a sandbox) so having everything in cloudformation will be a plus, which I know is achievable with option 1 + 2 (at least the lambda and S3 bucket(s)). Have you got your implementation in cloudformation or is it all setup manually on the console? Is it even possible to manage Glue Catalogs and crawlers via cloudformation?

There are still a couple of things that stick out for me that I'm not sure will be resolvable. I'm assuming that your DynamoDB setup was created using the API category with @model annotations? If so, have you been able to transform your tables in glue to match the names in your graphql schema rather than [tableName]-[apiId]-[envName] as they appear in DynamoDB? It's not a deal-breaker with a Glue Crawler solution, but nice to know if it's possible.

I'm assuming if I push the data to S3 I can basically control the table naming manually and create my Glue Catalog from S3 rather than Dynamo?

I'm a little confused when you say you use Glue to handle "casing" of your field names. I was under the impression that Glue doesn't support uppercase characters in field names as when I tested out crawling my DynamoDB tables all of my columns were mapped as lowercase - createdat instead of createdAt for instance. Again, not a huge deal, but just to make sure I'm understanding you properly.

Again, thanks for the response!

jonperryxlm avatar Jul 22 '20 22:07 jonperryxlm

There's a lot to cover here and frankly, it's been more than six months since I did this and I don't have written down documentation or steps where I hit issues, because I hit issues in all steps and slowly worked through them.

If you look here you can see some of the issues I had and resolutions, which includes the Glue mapping of case sensitive fields. My fields are created in Amplify/AppSync with @model in Schema.graphql and that created issues with mapping to Athena where everything has to be lowercased.

Table names are weird and long, but not a big deal for me.

Access and permissions was another major issue. Getting Athena to write to S3 required manually giving IAM rights to that, so my current solution did include manual stuff outside of Amplify and some manual CustomResources in Amplify. In the end, after looking at a lot of options, this was the one I had the most faith in since all the other ones required many of the same hacks due to Amplify not having any of it supported anyways and my hope was that Amplify eventually would include some automated setup for Athena Query Federation as it seems to be the solution with the most potential for Amplify users, but Athena Query Federation is still in public preview and Amplify seems overloaded with bug fixing, so it's probably another year before that happens, if ever. If it happens, I should be able to refactor my setup and set it all up through Amplify without having to do much to my core code related to this.

houmark avatar Jul 22 '20 23:07 houmark

@houmark

How did you solve the duplicate data problem? If I run the job more than once, the data doubles.

davidbiller avatar Aug 24 '20 09:08 davidbiller

Not sure what you mean with duplicate data? I have not had that problem.

houmark avatar Aug 24 '20 09:08 houmark

@houmark Ah you only use the new connector, right? You don't have the problem there. I run ETL jobs through Glue. Every time I run the job, it takes all the data and saves it to the table.

How is the performance of the connector? I read that at the end of every call is a scan anyway?

davidbiller avatar Aug 24 '20 10:08 davidbiller

@jonperryxlm Apologize for the late response. We don't support ETL solutions out of the box today with the Amplify CLI and this is an interesting use case and I'll mark this as a feature-request for our team to consider. Having said that, we do support the first half of your ask out here which is "DynamoStream (@model backed) => Lambda" integration and then in the Lambda you can choose to perform your desired ETL operation by either publishing the results to S3 for further analysis or Firehose -> S3. For managing it within the infrastructure within the CLI itself have you considered the use of custom stacks from the CLI https://docs.amplify.aws/cli/usage/customcf? Also, please let us know if you have any issues around DynamoStream (@model backed) => Lambda" integration and you can find more info about it out here - https://docs.amplify.aws/cli/usage/lambda-triggers#dynamodb-lambda-triggers

kaustavghosh06 avatar Oct 22 '20 06:10 kaustavghosh06

Closing in favor of tracking existing feature request, https://github.com/aws-amplify/amplify-cli/issues/2949

josefaidt avatar Jun 27 '22 19:06 josefaidt