Crate scylla_cdc

source ·
Expand description

Async library for consuming Scylla Change Data Capture log, built on top of the Scylla Rust Driver.

Why use a library?

The CDC log format is too complicated to be conveniently used in its raw form. The library enables the user to ignore the intricacies of the log’s internal structure and concentrate on the business logic.

Other documentation

Getting started

The following code will start reading the CDC log from now until forever and will print type of every operation read. To learn in more detail about how to use the library, please refer to the tutorial.

use async_trait::async_trait;
use scylla::SessionBuilder;
use scylla_cdc::consumer::*;
use scylla_cdc::log_reader::CDCLogReaderBuilder;
use std::sync::Arc;

struct TypePrinterConsumer;

#[async_trait]
impl Consumer for TypePrinterConsumer {
    async fn consume_cdc(&mut self, data: CDCRow<'_>) -> anyhow::Result<()> {
        println!("{}", data.operation);
        Ok(())
    }
}

struct TypePrinterConsumerFactory;

#[async_trait]
impl ConsumerFactory for TypePrinterConsumerFactory {
    async fn new_consumer(&self) -> Box<dyn Consumer> {
        Box::new(TypePrinterConsumer)
    }
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let session = Arc::new(
        SessionBuilder::new()
            .known_node("172.17.0.2:9042")
            .build()
            .await?,
    );

    let factory = Arc::new(TypePrinterConsumerFactory);

    let (_, handle) = CDCLogReaderBuilder::new()
        .session(session)
        .keyspace("ks")
        .table_name("t")
        .consumer_factory(factory)
        .build()
        .await
        .expect("Creating the log reader failed!");

    handle.await
}

Modules

  • A module containing types related to CDC internal structure.
  • A module representing the logic behind saving progress.
  • A module representing the logic behind consuming the data.
  • A module containing the logic behind reading and consuming the data.