#1
This guide shows how to integrate Apache Derby with Apache Kafka in a Spring Boot app to store and process streaming data.

1. Add Dependencies

In pom.xml:
<dependency>
  <groupId>org.apache.derby</groupId>
  <artifactId>derby</artifactId>
  <scope>runtime</scope>
</dependency>

<dependency>
  <groupId>org.springframework.kafka</groupId>
  <artifactId>spring-kafka</artifactId>
</dependency>

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

2. Configure Derby

In application.properties:
spring.datasource.url=jdbc:derby:/app/data/kafkaDB;create=true
spring.datasource.driver-class-name=org.apache.derby.jdbc.EmbeddedDriver
spring.jpa.hibernate.ddl-auto=update

3. Configure Kafka

spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=derby-group
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer

4. Create Entity

import jakarta.persistence.*;

@Entity
public class MessageLog {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String topic;
    private String content;

    // getters and setters
}

5. Repository

import org.springframework.data.jpa.repository.JpaRepository;

public interface MessageLogRepository extends JpaRepository<MessageLog, Long> {
}

6. Kafka Listener

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class KafkaMessageListener {
    private final MessageLogRepository repo;

    public KafkaMessageListener(MessageLogRepository repo) {
        this.repo = repo;
    }

    @KafkaListener(topics = "demo-topic", groupId = "derby-group")
    public void listen(String message) {
        MessageLog log = new MessageLog();
        log.setTopic("demo-topic");
        log.setContent(message);
        repo.save(log);
    }
}

7. Test Integration

  1. Start Kafka broker.
  2. Run Spring Boot app.
  3. Produce message:
kafka-console-producer.sh --topic demo-topic --bootstrap-server localhost:9092
Type a message, it will be stored in Derby automatically.

image quote pre code