Using Local Cassandra DB in Spring Boot Application
To create a Spring Boot application to perform CRUD (Create, Read, Update, Delete) operations in a Cassandra database, 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 Cassandra Connection
Update application.properties
to include the Cassandra connection settings:
spring.data.cassandra.contact-points=127.0.0.1
spring.data.cassandra.port=9042
spring.data.cassandra.keyspace-name=my_keyspace
Replace my_keyspace
with the name of your Cassandra keyspace.
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. You can further customize and extend this application to suit your requirements.
Leave a Comment