Write and Publish a Tutorial!
Do you have good notes or papers written by you and seeking for a
platform to publish? We provide the platform to publish your tutorials
in your name. If you wish to publish your tutorial in your name to
help the readers, Please contact us by sending an email to
publish@tools4testing.com or publish@java4coding.com The main way that
others learn about your work is through your published tutorials. If
you don’t publish, it will be as if you never did the work. Your notes
can help the readers only when you share it.
Aerospike Java Example using Spring Boot
Create java Project using Maven
In the command prompt execute the following maven command to generate Maven supported Java project named as “AerospikeExampleUsingMavenAndSpringboot”.
mvn archetype:generate -DgroupId=com.java4coding -DartifactId= AerospikeExampleUsingMavenAndSpringboot -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command creates a new maven Java project with the name “AerospikeExampleUsingMavenAndSpringboot”, with complete directory structure.
Import project into Intellij IDE
In Intellij IDE, Choose File –> New –> Module from Existing Sources –>Select pom.xml from newly created maven project, Click OK.
Add dependencies in pom.xml
Add the maven parent, add the below aerospike, sprinboot and other dependencies in pom.xml file.
pom.xml<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java4coding</groupId> <artifactId>AerospikeExampleUsingMavenAndSpringboot</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>AerospikeExampleUsingMavenAndSpringboot</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.3</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.aerospike</groupId> <artifactId>spring-data-aerospike</artifactId> <version>2.4.0.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> </project> |
Configure aerospike connection details in application.properties
Right click on main folder and select New>Directory
Now you select resources and press Enter
Now you create application.properties file inside resources directory and add below property values.
application.properties# aerospike aerospike.host=localhost aerospike.port=3000 aerospike.namespace=demo |
Create aerospike repository, service, controller and spring boot code
AerospikeUserRepository.javapackage com.java4coding.repositories;
import com.java4coding.dto.Student; import org.springframework.data.aerospike.repository.AerospikeRepository;
public interface AerospikeUserRepository extends AerospikeRepository<Student, Integer> { } |
Student.javapackage com.java4coding.dto;
import lombok.AllArgsConstructor; import lombok.Data; import org.springframework.data.aerospike.mapping.Document; import org.springframework.data.annotation.Id;
@Data @Document @AllArgsConstructor public class Student { @Id private int id; private String firstName; private String lastName; private int age; private String gender; } |
StudentService.javapackage com.java4coding.services;
import com.java4coding.dto.Student; import com.java4coding.repositories.AerospikeUserRepository; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service;
import java.util.Optional;
@Service @AllArgsConstructor public class StudentService { AerospikeUserRepository aerospikeUserRepository;
public Optional<Student> readUserById(int id) { return aerospikeUserRepository.findById(id); }
public void addUser(Student user) { aerospikeUserRepository.save(user); }
public void removeUserById(int id) { aerospikeUserRepository.deleteById(id); } } |
AerospikeConfigurationProperties.javapackage com.java4coding.configuration;
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;
@Data @Component @ConfigurationProperties(prefix = "aerospike") public class AerospikeConfigurationProperties { private String host; private int port; private String namespace; } |
AerospikeConfiguration.javapackage com.java4coding.configuration;
import com.aerospike.client.Host; import com.java4coding.repositories.AerospikeUserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.data.aerospike.config.AbstractAerospikeDataConfiguration; import org.springframework.data.aerospike.repository.config.EnableAerospikeRepositories;
import java.util.Collection; import java.util.Collections;
@Configuration @EnableConfigurationProperties(AerospikeConfigurationProperties.class) @EnableAerospikeRepositories(basePackageClasses = AerospikeUserRepository.class) public class AerospikeConfiguration extends AbstractAerospikeDataConfiguration { @Autowired private AerospikeConfigurationProperties aerospikeConfigurationProperties;
@Override protected Collection<Host> getHosts() { return Collections.singleton(new Host(aerospikeConfigurationProperties.getHost(), aerospikeConfigurationProperties.getPort())); }
@Override protected String nameSpace() { return aerospikeConfigurationProperties.getNamespace(); } } |
StudentController.javapackage com.java4coding.controller;
import com.java4coding.dto.Student; import com.java4coding.services.StudentService; import lombok.AllArgsConstructor; import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController @AllArgsConstructor public class StudentController { StudentService userService;
@GetMapping("/readStudent/{id}") public Optional<Student> readUserById(@PathVariable("id") Integer id) { return userService.readUserById(id); }
@PostMapping("/addStudent") public String addUser(@RequestBody Student user) { userService.addUser(user); return "Success"; }
@DeleteMapping("/deleteStudent/{id}") public String deleteUserById(@PathVariable("id") Integer id) { userService.removeUserById(id); return "Success"; } } |
AerospikeDemo.javapackage com.java4coding;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication @ComponentScan("com.java4coding") public class AerospikeDemo { public static void main(String[] args) { SpringApplication.run(AerospikeDemo.class, args); } } |
Intellij Project Structure
Run the application
Make sure aerospike is running.
Make sure the schema is created.
Right click on AerospikeDemo.java and select Run ‘AerospikeDemo.main()’
Spring boot will start and now you can invoke the controller.
Save the below JSON to a file and import it to POSTMAN. If you don’t have Postman installed on your local computer you can download it here: https://www.postman.com/downloads/
AerospikeTest.postman_collection.json{ "info": { "_postman_id": "dd39dca3-8f15-444a-bc54-a555c4dd3af3", "name": "AerospikeTest", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" }, "item": [ { "name": "https://localhost:8080/users", "request": { "method": "POST", "header": [ { "key": "Content-Type", "value": "application/json", "type": "default" } ], "body": { "mode": "raw", "raw": "{\r\n \"id\": 1,\r\n \"firstName\": \"Manu\",\r\n \"lastName\": \"Manjunatha\",\r\n \"age\": 20,\r\n \"gender\":\"Male\"\r\n}" }, "url": { "raw": "https://localhost:8080/addStudent", "protocol": "http", "host": [ "localhost" ], "port": "8080", "path": [ "addStudent" ] } }, "response": [] }, { "name": "https://localhost:8080/users", "request": { "method": "GET", "header": [], "url": { "raw": "https://localhost:8080/readStudent/1", "protocol": "http", "host": [ "localhost" ], "port": "8080", "path": [ "readStudent", "1" ] } }, "response": [] }, { "name": "https://localhost:8080/users", "request": { "method": "DELETE", "header": [], "url": { "raw": "https://localhost:8080/deleteStudent/1", "protocol": "http", "host": [ "localhost" ], "port": "8080", "path": [ "deleteStudent", "1" ] } }, "response": [] } ] } |
Add Student
Open POST request and click on Send.
Read Student
Open GET request and click on Send.
Delete Student
Open DELETE request and click on Send.