Manipulating Geo Coordinate Data Using DAO Design Pattern in Jedis
Learn how to manipulate geo coordinate data using the DAO pattern without knowing the low-level API through the Jedis Java client application.
Join the DZone community and get the full member experience.Join For Free
In this article, we are going to learn how to add geocoordinate data and retrieve it using the DAO design pattern. The DAO pattern is implemented as a layer between the client application and the database.
The data being stored in the Redis database is modeled as a domain object (POJO class). It will have only getter and setter methods. The client application knows only the domain object and high-level API.
- Eclipse (any version) with Maven capabilities
- Java 8+
- Redis and Jedis
Installing Redis Server on Windows
- Click on the link: https://github.com/dmajkic/redis/downloads
- Download the redis-2.4.5-win32-win64.zip file.
- Unzip the file and go to the 64bit folder. There, you can find redis-server.exe.
- To start the Redis server, execute the redis-server.exe file.
Installing Eclipse-IDE on Windows
- Click on the link: https://www.eclipse.org/downloads/download.php?file=/oomph/epp/2020-09/R/eclipse-inst-jre-win64.exe
- Download the eclipse-inst-jre-win64.exe file and run the Eclipse installer.
3. Select Eclipse IDE for Eclipse committers and install.
Creating a Maven Project in Eclipse IDE
- Open the Eclipse IDE.
- Go to File > New > Project.
3. Go to Maven > Maven Project and click Next.
4. Select your workspace location and click Next.
5. Select the quick start Maven archetype and click Next.
6. Enter Group Id, Artifact Id, and package name.
- Group Id: Fill a groupId for the project of your choice.
- Artifact Id: Fill artifactId for the project of your choice.
- Package: Java package structure of your choice
7. The above process will create a project structure like below:
8. Create packages like com.example.demo, com.example.demo.model and com.example.demo.dao under src/test/java folder
9. Place the Coordinate.java and Site.java file in the com.example.demo.model package.
10. Add the GeoQuery.java, SiteDao.java, SiteDaoRedisImpl.java, SiteGeoDao.java, SiteGeoDaoRedisImpl.java, and SiteGeoDaoRedisImplTest.java file in the com.example.demo.dao package.
11. Add HostPort.java, KeyHelper.java, JedisDaoTestBase.java, MetricUnit.java, RedisSchema.java, and TaskKeyManager.java in the com.example.demo package.
11. Replace the pom.xml with the below content:
Running the Code
1. Build the Maven project and run the test case as shown below.
2. Check the test result.
Now, we’ve learned how to manipulate geo coordinate data using the DAO pattern without knowing the low-level API through the Jedis Java client application.
Further, this use case can be adapted according to your requirements.
Feel free to ask any questions.
Opinions expressed by DZone contributors are their own.