Data as a Service using ScyllaDB

In today’s data-driven world, efficient data management and accessibility are paramount. Data as a Service (DaaS) and the integration of an API Gateway play pivotal roles in enabling seamless data sharing, secure access, and efficient management. We will explore the implementation of DaaS using ScyllaDB and the advantages of incorporating an API Gateway for managing interactions.

DaaS serves as a solution to selectively and securely share data across diverse platforms and systems.

Why DaaS can help?

Data in silos can limit its potential value across an organization. However, exposing an entire database to various teams can pose governance and security challenges. DaaS resolves this by selectively exposing valuable data via APIs, enabling secure sharing while implementing essential access controls.

Benefits of using DaaS

  • Agility and easy maintainability: Rapid access to data without having knowledge of its storage and reduced maintenance overhead for data consumers.
  • Enhanced Security: Robust authentication and authorization protocols.
  • Scalability and Load Balancing: Efficient resource utilization and high availability.
  • Protocol Abstraction and Versioning: Seamless integration without disruptions.
  • Monitoring and Analytics: Insights for performance optimization and issue resolution.
  • Compliance and Governance: Consistent application of regulatory requirements.

Example using ScyllaDB

  • API Gateway for API management.
  • ScyllaDB for data storage.
  • RESTful Spring Boot application for API development.

Setting Up API Gateway

The implementation of API Gateway will be using Spring Cloud Gateway to route requests to our ScyllaDB instance.

@SpringBootApplication
public class Application {

public static void main(String[] args) {
    new SpringApplicationBuilder(Application.class)
            .profiles("routes")
            .run(args);
}

}

Routing configuration can be established either through Java, or by utilizing properties configuration:

spring:
cloud:
gateway:
routes:

  • id: daas-scylladb
    uri: localhost:8082/
    predicates:
  • Path=/daas/users

The example is using Spring Cloud Gateway, it’s important to note that various alternatives exist for API gateway solutions. While Spring is a robust and widely-used framework, other solutions, such as Netflix Zuul, and those offered by cloud providers like AWS, provide unique features and advantages.

Setting Up API Service

The REST controller responsible for accessing ScyllaDB data will be implemented Spring Web.

@RestController
@RequestMapping("/daas")
public class DaasController {
@Autowired
private UserRepository userRepository;

@GetMapping(path = "/users", produces = "application/json")
public ResponseEntity<List<User>> getUsers() {
    return ResponseEntity.ok(userRepository.findAll());
}

}

This controller is responsible for handling HTTP requests related to ScyllaDB data.

Setting up the Data Access Layer

ScyllaDB data access layer powered by Spring Data Cassandra integration provides a seamless way to interact with ScyllaDB and simplifies our data access logic.

public interface UserRepository extends CassandraRepository<User, Integer> {

}

The UserRepository interface provides basic CRUD operations for interacting with the ScyllaDB database. If required, you have the flexibility to extend the repository by incorporating additional access operations to meet your specific requirements.

The examples presented here can be found in GitHub repository.

ScyllaDB 适合写操作偏多,读操作偏少的情况。这个操作量是相对的,如果你不是很确定你的业务需要哪一种,建议是多考虑多调研多测试,不要盲目的跟风使用 ScyllaDB,一旦使用了对于其他的 database 迁移支持并不友好(如果不能一直长期维护使用下去的话)。维护起来也比较麻烦,同时 ScyllaDB 使用起来可能还会有一些 bug。

Data as a Service (DaaS) has revolutionized data sharing by enabling secure and efficient access to valuable data across organizational boundaries. In the rapidly advancing realm of data management, the incorporation of GraphQL into the framework of Data as a Service (DaaS) stands out as a pivotal advancement and this post digs into the integration of GraphQL into our DaaS infrastructure, highlighting the benefits and implementation details.

GraphQL: A Paradigm in Data Access

GraphQL enhances Data as a Service (DaaS) by making data retrieval more adaptable and efficient. Unlike conventional REST APIs, GraphQL allows clients to specify their data needs clearly, preventing the unnecessary transfer of excess information.

Some benefits of GraphQL in DaaS:

  • Flexible Data Retrieval: Clients define the structure of the response, specifying the data according to their needs, eliminating the need for multiple endpoints, and reducing API complexity.
  • GraphQL’s query language: Allows for complex filtering, sorting, and aggregation operations.
  • Client-side Optimization: GraphQL empowers clients to optimize data fetching based on their specific requirements, improving performance and reducing server load.
  • Versioning and schema changes: GraphQL delivers the explicitly requested data, allowing the addition of new capabilities through the introduction of new types and fields without causing disruptive changes.

Integration with Spring GraphQL

  • API Gateway for API management.
  • RESTful Spring Boot application for API development.
  • GraphQL for Enhanced Data Retrieval.
  • ScyllaDB and MySQL for data storage.

Setting up GraphQL

**Designing the GraphQL Schema
**The schema defines the structure of the data accessible through the API, describing the types of data, their relationships, and the available operations. In this example, the schema includes types for Device and Location, along with the relationship between them.

type Query {
deviceById(id: ID): Device
}

type Device {
id: ID
name: String
location: Location
}

type Location {
id: ID
name: String
city: String
country: String
code: Int
}

**Data Access Layer
**Spring Data JPA is employed to interact with the database, retrieving device and location data based on the provided GraphQL queries.

@GraphQlRepository
public interface DeviceRepository extends Repository<Device, String>, QueryByExampleExecutor {
Device findById(String id);
}j

The Location repository is quite similar:

@GraphQlRepository
public interface LocationRepository extends Repository<Location, String>, QueryByExampleExecutor {
Location findById(String id);
}

**Configuring the GraphQL Controller
**The GraphQL controller serves as the endpoint for handling incoming GraphQL requests. It acts as a bridge between the GraphQL schema and the underlying data access layer, enabling the retrieval and manipulation of the data.

@Controller
public class DeviceController {

private final DeviceRepository deviceRepository;
private final LocationRepository locationRepository;
@Autowired
public DeviceController(DeviceRepository deviceRepository, LocationRepository locationRepository) {
    this.deviceRepository = deviceRepository;
    this.locationRepository = locationRepository;
}

@QueryMapping
public Device deviceById(@Argument String id) {
    return deviceRepository.findById(id);
}

@SchemaMapping(typeName = "Device", field = "location")
public Location location(Device device) {
    return locationRepository.findById(device.getLocationId());
}

}

The deviceById resolver, annotated with @QueryMapping, retrieves a device with the specified identification using theDeviceRepository. The location resolver, annotated with @SchemaMapping, fetches the location associated with the provided device using the LocationRepository.

Configuring the GraphQL API Application

To enable the application to serve GraphQL requests, ensure the following configurations are set:

server.port=8083
logging.level.root=info
spring.graphql.graphiql.enabled=true
spring.graphql.schema.printer.enabled=true
spring.graphql.path=/daas/api/graphql

spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/daas
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true

This property spring.graphql.path specifies the path on which GraphQL requests are received and in this example, the path is set to /daas/api/graphql.

Enabling GraphQL API Route on API Gateway

To enable the GraphQL API application to be accessed through the API gateway, a new route is configured in the API gateway service:

  • id: daas-graphql-api-gateway
    uri: http://localhost:8083
    predicates:
    • Path=/daas/graphql/**
      filters:
    • SetPath=/daas/api/graphql

This route redirects all incoming requests to the GraphQL API application on the port 8083, with the path pattern /daas/graphql/**. The SetPath filter ensures that the request path is updated to /daas/api/graphql before reaching the GraphQL API application. This allows the GraphQL API application to recognize the request path and handle it properly.

Live Demo

Now, let’s put GraphQL into action and watch the integration of GraphQL within a microservices architecture, facilitated by an API Gateway.

With both services running, after the following command is executed:

make run

let’s send a simple GraphQL query to the API gateway:

query deviceDetails {
deviceById(id: “device-1”) {
name
location {
city
country
}
}
}

This query will retrieve detailed information about a specific device, including its name and location information.

The examples presented here can be found in GitHub repository.

Licensed under CC BY-NC-SA 4.0
最后更新于 Jan 06, 2025 05:52 UTC
comments powered by Disqus
Built with Hugo
主题 StackJimmy 设计
Caret Up