0% found this document useful (0 votes)
66 views8 pages

CRUD Weekly Report April Week 3

This document outlines how to perform CRUD operations on Cassandra using Spring Boot. It includes defining a data model with annotations, creating a repository interface that extends CassandraRepository, building a REST API controller with methods for each CRUD operation, and making calls to the Cassandra repository from the controller methods. Configuration properties are also specified to connect to a Cassandra keyspace and contact point. The keyspace and table are then created in Cassandra to store the data model.

Uploaded by

CHARNATH G CSE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views8 pages

CRUD Weekly Report April Week 3

This document outlines how to perform CRUD operations on Cassandra using Spring Boot. It includes defining a data model with annotations, creating a repository interface that extends CassandraRepository, building a REST API controller with methods for each CRUD operation, and making calls to the Cassandra repository from the controller methods. Configuration properties are also specified to connect to a Cassandra keyspace and contact point. The keyspace and table are then created in Cassandra to store the data model.

Uploaded by

CHARNATH G CSE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 8

SPRING BOOT CASSANDRA CRUD OPERATION:-

TECHNOLOGIES USED:-

1. JAVA-17
2. SPRING BOOT
3. CASSANDRA
4. CQLSH
5. MAVEN

In pom.xml we need to add these dependencies

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>

Configure Spring data Cassandra:-

spring.data.cassandra.keyspace-name=chan
spring.data.cassandra.contact-points=127.0.0.1
spring.data.cassandra.port=9042
Define Data Model
model/Tutorial.Java
package com.chan.spring.data.cassandra.model;

import java.util.UUID;

import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;

@Table
public class Tutorial {
@PrimaryKey
private UUID id;

private String title;


private String description;
private boolean published;

public Tutorial() {

public Tutorial(UUID id, String title, String description, boolean published) {


this.id = id;
this.title = title;
this.description = description;
this.published = published;
}

public UUID getId() {


return id;
}

public void setId(UUID id) {


this.id = id;
}

public String getTitle() {


return title;
}

public void setTitle(String title) {


this.title = title;
}

public String getDescription() {


return description;
}
public void setDescription(String description) {
this.description = description;
}

public boolean isPublished() {


return published;
}

public void setPublished(boolean isPublished) {


this.published = isPublished;
}

@Override
public String toString() {
return "Tutorial [id=" + id + ", title=" + title + ", desc=" + description + ", published=" +
published + "]";
}
}
– @Table identifies this model to be persisted to Cassandra as ‘tutorial’ table.
– @PrimaryKey specifies the primary key field of this entity. This field corresponds to the
PRIMARY KEY of the ‘tutorial’ table.

CREATE REPOSITORY INTERFACE:

repository/TutoialRepository.java

package com.chan.spring.data.cassandra.repository;
import java.util.List;
import java.util.UUID;

import org.springframework.data.cassandra.repository.AllowFiltering;
import org.springframework.data.cassandra.repository.CassandraRepository;

import com.bezkoder.spring.data.cassandra.model.Tutorial;

public interface TutorialRepository extends CassandraRepository<Tutorial, UUID> {


@AllowFiltering
List<Tutorial> findByPublished(boolean published);

List<Tutorial> findByTitleContaining(String title);


}
CREATE SPRING REST APIs CONTROLLER:

controller/TutorialController.java

package com.chan.spring.data.cassandra.controller;

...
import com.chan.spring.data.cassandra.model.Tutorial;
import com.chan.spring.data.cassandra.repository.TutorialRepository;

@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api")
public class TutorialController {

@Autowired
TutorialRepository tutorialRepository;

@GetMapping("/tutorials")
public ResponseEntity<List<Tutorial>> getAllTutorials(@RequestParam(required = false) String
title) {

@GetMapping("/tutorials/{id}")
public ResponseEntity<Tutorial> getTutorialById(@PathVariable("id") String id) {

@PostMapping("/tutorials")
public ResponseEntity<Tutorial> createTutorial(@RequestBody Tutorial tutorial) {

@PutMapping("/tutorials/{id}")
public ResponseEntity<Tutorial> updateTutorial(@PathVariable("id") String id, @RequestBody
Tutorial tutorial) {

@DeleteMapping("/tutorials/{id}")
public ResponseEntity<HttpStatus> deleteTutorial(@PathVariable("id") String id) {

@DeleteMapping("/tutorials")
public ResponseEntity<HttpStatus> deleteAllTutorials() {
}

@GetMapping("/tutorials/published")
public ResponseEntity<List<Tutorial>> findByPublished() {

CREATE OPERATION:-

@PostMapping("/tutorials")
public ResponseEntity<Tutorial> createTutorial(@RequestBody Tutorial tutorial) {
try {
Tutorial _tutorial = tutorialRepository.save(new Tutorial(UUIDs.timeBased(), tutorial.getTitle(),
tutorial.getDescription(), false));
return new ResponseEntity<>(_tutorial, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

RETRIEVE OPERATION:-

@GetMapping("/tutorials")
public ResponseEntity<List<Tutorial>> getAllTutorials(@RequestParam(required = false) String
title) {
try {
List<Tutorial> tutorials = new ArrayList<Tutorial>();

if (title == null)
tutorialRepository.findAll().forEach(tutorials::add);
else
tutorialRepository.findByTitleContaining(title).forEach(tutorials::add);

if (tutorials.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

return new ResponseEntity<>(tutorials, HttpStatus.OK);


} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/tutorials/{id}")

public ResponseEntity<Tutorial> getTutorialById(@PathVariable("id") UUID id) {


Optional<Tutorial> tutorialData = tutorialRepository.findById(id);

if (tutorialData.isPresent()) {
return new ResponseEntity<>(tutorialData.get(), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

@GetMapping("/tutorials/published")
public ResponseEntity<List<Tutorial>> findByPublished() {
try {
List<Tutorial> tutorials = tutorialRepository.findByPublished(true);

if (tutorials.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(tutorials, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}

UPDATE OPERATION:-

@PutMapping("/tutorials/{id}")
public ResponseEntity<Tutorial> updateTutorial(@PathVariable("id") UUID id, @RequestBody
Tutorial tutorial) {
Optional<Tutorial> tutorialData = tutorialRepository.findById(id);
if (tutorialData.isPresent()) {
Tutorial _tutorial = tutorialData.get();
_tutorial.setTitle(tutorial.getTitle());
_tutorial.setDescription(tutorial.getDescription());
_tutorial.setPublished(tutorial.isPublished());
return new ResponseEntity<>(tutorialRepository.save(_tutorial), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

DELETE OPERATION:-

@DeleteMapping("/tutorials/{id}")
public ResponseEntity<HttpStatus> deleteTutorial(@PathVariable("id") UUID id) {
try {
tutorialRepository.deleteById(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@DeleteMapping("/tutorials")
public ResponseEntity<HttpStatus> deleteAllTutorials() {
try {
tutorialRepository.deleteAll();
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}

SET UP CASSANDRA DATABASE:-

create keyspace CHAN with replication={'class':'SimpleStrategy', 'replication_factor':1};


create tutorial table in the keyspace

use chan;

CREATE TABLE tutorial(


id timeuuid PRIMARY KEY,
title text,
description text,
published boolean
);

You might also like