Yes, It's easy to build a Fluvio connector in Rust.
2024-09-08
In today’s fast-paced world, real-time data is no longer a luxury—it’s a necessity. Whether you're monitoring live stock prices, analyzing social media trends, or syncing spreadsheets for instant insights, having data at your fingertips can make all the difference. That’s where Fluvio comes in, a platform designed to streamline your data journey by offering a powerful, yet easy-to-use, infrastructure for building and managing data pipelines.
But here’s the real magic of Fluvio: It gives you the tools to not only move data between systems but also build your very own custom connectors! Imagine you’re working with Google Sheets, and you want your data to flow seamlessly from a Fluvio topic into a spreadsheet in real-time. Manually exporting data sounds tedious, right? What if you could build an outbound connector that does all this for you automatically?
Sounds fun? It is! In this guide, we’ll walk through the process of building your own Google Sheets outbound connector. This isn’t just another tech tutorial—by the end of this, you’ll have a functional connector and a deeper understanding of how Fluvio can integrate with just about anything.
Why Build a Connector?
If you’ve ever dealt with the challenge of moving data between systems, you know how time-consuming and prone to error it can be. Fluvio’s connectors are built to make this painless. You can import data with inbound connectors or export it with outbound connectors, depending on your needs. And the beauty is that both work in a similar way—the only difference is which direction your data is flowing in relation to a Fluvio topic.
Imagine the possibilities:
Automatically stream real-time data from Fluvio to Google Sheets for analysis or reporting.
Set up an outbound connector to sync Fluvio data back into tools like Google Sheets for easy collaboration.
Use Fluvio as a central hub for all your real-time data needs, and effortlessly extend it with your own connectors!
Let’s Dive In
We’ll use Google Sheets as our example, but the principles you’ll learn can apply to virtually any data destination. Whether you want to push data to a cloud service, a database, or even a custom application, Fluvio connectors can handle it. And once you get the hang of it, the possibilities are endless.
In the next sections, we’ll cover how to set up a Google Sheets outbound connector, configure it for your specific needs, and show how to stream data directly from a Fluvio topic to your spreadsheet. It’s a fun way to get started with Fluvio and unleash your creativity as a data engineer.
Ready to get your hands dirty and build something awesome? Let’s jump right in and start connecting your data with the world of real-time streaming!
For best developer experience use WSL or linux I am assuming that you are working on a linux machine.
Cargo.toml - It is just like package.json for rust.
main.rs - It is the entry point of the program.
config.rs - It will have the connector parameters provided in sample-config.yaml.
sample-config.yaml - It is a sample configuration to test our connector.
Now we will install required crate for the project.
You can use cargo add [name]
but for simplicity and consistency copy this dependency section to your cargo.toml
Main package to highlight is google-sheets4. It will help us to connect google spreadsheet.
Let's write some code
First thing is to setup config inside config.rs let's declare some variables for google sheets like private_key, client_email and token_url.
Here thing to note is the #[connector(config, name = "sheet")] in this name = 'sheet' is the section in which you add these secrets you will understand it in next section.
Now setup sample-config.yaml
Here in this we will add secrets in this config file open this and add the required code
This file is almost self-explanatory. In this Important is to not copy and paste this code snippet in type their should be you project connector name and see sheet we wrote this block by sheet name because we coded it in config.rs I tell you in previous section.
Create a secrets.txt file in root folder and include this following data:
Create a file sink.rs inside src
add this code inside this file
Don't react it is very easy just understand the core function.
Create a struct here it is SheetsSink inside which we store data on initial connector run. We are saving ServiceAccountKey.
In this code block we are getting config like private_key and others and creating a ServiceAccountKey and set it to Self.
After that we are implementing SheetsSink for the native Sink provided by fluvio. It requires a generic type of the struct that has data which you want to save. Here we created one named Playload importing from main.rs. We will see in next section.
We will create a async function named connect inside which we created auth variable saving authentication thing for google spreadsheet and create a hub with that auth this part is required for the package google_sheets4. You can see their docs for more information.
After that we have created a unfold variable which is resolving records that are sent on topic in unfold we are taking two parameter hub and a payload. At this I think you are familiar them both.
And executing a function to append the values in google spreadsheet and handling errors if there is any. then we are cleaning everything at the end with Ok().
It's time to touch main.rs entrypoint
In this file we need to understand only couple of things first add this code inside it.
First thing is Payload struct
This struct contain important things like values, range and spreadsheet_id and everything is self explanatory.
Then we have a function start which is the entrypoint gives us config and comsumer stream inside this we are handling retries with backoff function on need to understand this for now it just optimize connector.
The main code block is this
In this we are connecting to our own sink that we created with config provided and handling error if their is any. After this resetting backoff after connection.
Then In last block below we are looping the stream in which we parsed Payload by serde_json and executing that with sink.send(payload). Handling some errors.
Finally build connector and test
It's time to build the connector and test it.
Build connector by this command
make sure running in root directory
If facing any error then use this command
change x86-64 with your system's architecture
Test it with sample-config.yaml and secrets.txt
Run fluvio producer and produce some sample data and see it in google spreadsheet.
send sample data like
replace spreadsheet_id with your owns.
If you face any permission error like this:
refer to this fix.
Add your client_email from google console in the spreadsheet's editor section or see Fix On StackOverflow