Upstash offers serverless Kafka topics. In this blog post, we are going to demonstrate how to publish to and read from a Kafka topic with only a few clicks using Upstash. The complete project code can be found at here
We are going to use an AWS Lambda function to periodically invoke Twitter search api. The function is going to look for tweets containing a keyword we choose and publish those tweets to Upstash's serverless Kafka offering. Once the tweets are stored in Kafka, you can process them with multiple consumers or copy them to a data store using a connector.
Creating the project
Let us start by creating a simple maven project. We will use Twittered Java library for Twitter API access, Kafka-clients for Kafka, Log4j for logging utility and AWS Lambda for serverless compute service. Here are our dependencies,
Create Twitter Developer Account
First, we need to create user credentials to use Twitter API. Sign up for a developer account at Twitter Developer Portal. Sign in to developer console. Give a name to your app. We will use bearer token for credentials.
Twitter Client Connection and Tweet Search
TwitterProducer class is responsible for searching Twitter publishing tweets to Kafka.
We are going to schedule our lambda function to run every 15 minutes. So let us search only tweets posted in the last 15 minutes.
Kafka Producer
Now we want to publish these tweets to a Kafka topic. We need to sign up Upstash console here. Lets create Kafka cluster in Upstash. You can find instructions here. Upstash Console already gives us the code to connect to the cluster. I just copied below connection piece off Upstash Console. We define bootstrap servers, Kafka username and Kafka password as environment variables.
Feed the tweets into the Kafka producer.
Setting up with AWS Lambda
AWS can trigger user defined functions based on external events or periodically. We will just schedule our function to run every 15 seconds. We need to provide a class implementing AWS Lambda's RequestHandler interface. Our TweetStreamApp class implements RequestHandler interface. All we need to do is trigger our TwitterProducer object in handleRequest method.
We have all the parts that we need for our project. Let's build a jar archive containing all the dependencies. Simply invoke mvn package.
We need an AWS account. Create AWS Lambda function in AWS UI. We are going to make some configuration. Configure "Handler" parameter as tweetStream.TweetStreamApp::handleRequest.
Add environment variables under configuration part.
Add a schedule rule in Amazon EventBridge to schedule our function every 15 minutes.
Add a EventBridge trigger for our lambda function. Pick the schedule rule that we created in the previous step.
Our function will be triggered every 15 minutes now. We can see the logs in CloudWatch.
Let's check Upstash console and verify that our Kafka topic is receiving messages.
Lets write a simple Kafka consumer to verify that we can consume all these tweets.
It is working :)
Conclusion
We wrote a periodic job to search Twitter and save relevant tweets to Kafka entirely serverless. Management of our Kafka cluster is handled by Upstash. We only copied credentials from Upstash Console and started publishing records.