Show List

Using Astra Data API to access Cassandra DB Data

To create a Spring Boot application to perform CRUD (Create, Read, Update, Delete) operations in a Cassandra database using the DataStax Astra API, you'll need to follow these steps:

Step 1: Set Up Your Spring Boot Project

Create a new Spring Boot project using your preferred IDE or Spring Initializr. Include the following dependencies:

  • Spring Web
  • Spring Data Cassandra

Step 2: Configure Astra Connection

Sign up for DataStax Astra and create a database. Once you have created your Astra database, retrieve the secure connect bundle.

Update application.properties to include the Astra connection settings:


spring.data.cassandra.cloud.secure-connect-bundle=/path/to/secure-connect-bundle.zip spring.data.cassandra.cloud.username=<your-username> spring.data.cassandra.cloud.password=<your-password> spring.data.cassandra.cloud.keyspace-name=<your-keyspace>

Replace <your-username>, <your-password>, and <your-keyspace> with your Astra database credentials and keyspace name.

Step 3: Create an Entity Class

Create a Java class to represent your data model. This class will map to a table in Cassandra.


import org.springframework.data.annotation.Id; import org.springframework.data.cassandra.core.mapping.PrimaryKey; import org.springframework.data.cassandra.core.mapping.Table; @Table public class User { @Id @PrimaryKey private String id; private String name; private String email; // Getters and setters }

Step 4: Create a Repository Interface

Create a Spring Data repository interface to perform CRUD operations on the User entity.


import org.springframework.data.cassandra.repository.CassandraRepository; public interface UserRepository extends CassandraRepository<User, String> { }

Step 5: Implement CRUD Operations

Create a service class to implement CRUD operations using the repository.


import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; @Service public class UserService { @Autowired private UserRepository userRepository; public List<User> getAllUsers() { return userRepository.findAll(); } public Optional<User> getUserById(String id) { return userRepository.findById(id); } public User createUser(User user) { return userRepository.save(user); } public User updateUser(User user) { return userRepository.save(user); } public void deleteUser(String id) { userRepository.deleteById(id); } }

Step 6: Create REST Controller

Create a REST controller to expose CRUD endpoints.


import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping public List<User> getAllUsers() { return userService.getAllUsers(); } @GetMapping("/{id}") public User getUserById(@PathVariable String id) { return userService.getUserById(id) .orElseThrow(() -> new RuntimeException("User not found")); } @PostMapping public User createUser(@RequestBody User user) { return userService.createUser(user); } @PutMapping("/{id}") public User updateUser(@PathVariable String id, @RequestBody User user) { user.setId(id); return userService.updateUser(user); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable String id) { userService.deleteUser(id); } }

Step 7: Run the Application

Run your Spring Boot application and test the CRUD operations using a REST client like Postman or curl.

That's it! You have now created a Spring Boot application to perform CRUD operations in a Cassandra database using the DataStax Astra API. You can further customize and extend this application to suit your requirements.


    Leave a Comment


  • captcha text