Using Java Faker lib to populate data on DynamoDB employing AWS-SDK.
As I promise 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 is not very professional to see persisted data in our front-end apps of “sdsdsd” or “qwqwq” in some fields or texts that do not do sense thinking in the functionality context. It is not reasonable also to create test data with exceptional quality 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 components 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 in 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 existing on DynamoDB tables because I want to take control of 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 spect!! The following image shows the 20 locations that our “Java Faker Data Generator” creates:
Note that flags images don’t correspond to the countries shown in the previous image. The importance here is to see the different app’s functionalities and see if user interfaces match what we spect.
Finally, I’ve implemented an improvement in the Company’s page functionality. The app now uses a component called “Ionic Infinite Scroll” to load data based on GrapqhQL tokens. That’s because DynamoDB returns a limited amount of 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 retrieve. We can use this token in the subsequent query operations to get more pending data until the HTTP response does not have a token anymore. This is treated as pagination, and you can find more in this Amplify article.
The great thing about this is that you have now 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.