Using Java Faker lib to populate data on DynamoDB employing AWS-SDK.
As promised in my previous post, it is time to automate the process of persisting faking data on DynamoDB. This task is essential when working on our initial web pages, and we need some testing data to see what our user interfaces look like. Besides, it could be more professional to see trash data persisted data in our front-end apps in fields or texts that do not do sense thinking in the functionality context. It is also not reasonable to create exceptional quality test data because you need to consume much of your time on this task. That’s why the Java Faker library comes to our rescue.
I’ve created a Java project called “java-faker-dynamodb-example” that you can clone from my GitHub account. This project uses the JDK 17 and the AWS-SDK version 2, as you will see in the POM file:
Then, we will use the “dynamodb” and “javafaker” dependencies for our project purpose:
I created a generic class called “ServiceGeneric” that contains reusable code for DynamoDB integrations between different services that persist faker data on AWS:
As I mentioned at the beginning of this article, I will use the DynamoDB tables shown in my previous article for this tutorial. Those classes are using annotations from the aws-sdk to map the tables on AWS:
The Services are another key Java component that I use to persist the data on DynamoDB tables. You can find them in the “src/main/java/com/hiperium/java/faker/dynamodb/example/service”. These services use the Java Faker library to generate data and then persist it on DynamoDB:
Finally, thinking of data generation automation, I’ve created a top Java main class that reads user inputs (in the terminal or console) about the quantity of data that the user needs to generate. I put limits on the amount of data to not overload DynamoDB with unnecessary data:
The program asks you for the AWS profile name configured on your local machine. I implemented this because we can have different AWS profiles configured on our computers. Another theme to consider is that the program deletes all previous data on DynamoDB tables because I want to control duplicates.
Now it’s time to build and deploy our Ionic app to see how it is presenting the newly generated faker data:
# git clone https://github.com/aosolorzano/ionic-amplify-graphql-example.git
# cd ionic-amplify-graphql-example
# ionic build
# ionic serve
And that’s working as we expected!! The following image shows the 20 locations that our “Java Faker Data Generator” creates:
Note that the flag images don’t correspond to the countries shown in the previous picture. It’s essential to see the different apps’ functionalities and whether user interfaces match what we see.
Finally, I’ve implemented an improvement in the Company’s page functionality. The app now uses an “Ionic Infinite Scroll” component to load data based on GrapqhQL tokens. DynamoDB returns limited data due to a query or scan operation. For these cases, the AppSync service returns a token in the HTTP response to inform our app that more data is to be retrieved. We can use this token in the subsequent query operations to get more pending data until the HTTP response no longer has a token. This is treated as pagination; you can find more in this Amplify article.
The great thing about this is that you now have a starting point to integrate your apps with some AWS services with the help of Amplify, no matter if you are using a different framework or libraries like React, Vue, etc. Furthermore, you can clone and improve my project code by adapting it to your needs.
Thanks for reading, and I will see you in my next post.